plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

PHP8: Inconsistent formatting when using attributes

Open markkirchner opened this issue 3 years ago • 2 comments

Prettier 2.4.1

PHP Plugin 0.17.6

Input:

use JetBrains\PhpStorm\Pure;

class Test
{
    /**
     * Method with an attribute.
     * @param string $foo
     * @return string
     */
    #[Pure]
    public function withAttribute(string $foo): string
    {
        return $foo;
    }

    /**
     * Method without any attributes.
     * @param string $foo
     * @return string
     */
    public function withoutAttribute(string $foo): string
    {
        return $foo;
    }
}

Expected: Code is already correctly formatted => runnning Prettier should not result in any changes.

Result: The opening brace of the method that contains the attribute gets moved, the other one does not.

    public function withAttribute(string $foo): string {
        return $foo;
    }

Additional note: Removing the string $foo parameter from the method signature makes the problem go away. So it seems to be a rather weird combination of factors that trigger the bug.

markkirchner avatar Nov 24 '21 14:11 markkirchner

Same thing is happening here too.

Here's the playground for reference: https://loilo.github.io/prettier-php-playground/#N4IgDgTgpgLjCWUIHkwIPYDsDOIBcok8mMA6vACYwAW+AHAAwA0IMAhgEblW14AsLAK7YoAFU648AMzYAbES2zEA5rKgBFQehhR8M+VBZhqYAGpIlWfCDoA6AIwgWMCG3iyVAYXQBbH2wAFAAkA-BdBQxAOVwBjKABlGABPNWswbAgAWgAmJxBoAEdBeGgA12V-PTkFEGIRCBgytgq2KoMAXxYoCngYdBQ0eCxJUDZsGDaRTtrMMEEJvBAAHgB+YzAAHUwt4SgAAgApWAAhVzqNjYCTRP6fC4DBaABuLa2Y2THsPdEoca3gLZ7IF7AD0ACowYDgXswXsALKwajoCh7ADuvWoezYmCxcAg8A48ygtihwNhAAEwGxXD49uN8ZhlHsACRSdDoUlAinQGCPHH0lScmEgzkAYgA2g9oABdTlzDgeGJ7KSCTAxDA49E0ACCeIJRIAFALGSy2egAJR4OkuQU44EAu3QoE8vmm9kvR17dqvT3gyGemHwxHItEY9DzLGYJK4m2EnTYEkBilUmnWhlM1nsoXc2Cu43KbMiz3yxXK1XqoaasPzXWxw35t0Wq35zkOp3Al0QHGZ9Ae6HezDtEDtIA

The error resides within this block of code: https://github.com/prettier/plugin-php/blob/1c9a8dd06eb5bfd0f1008056d0a027cca2c15c2b/src/printer.js#L1458-L1472

The issue ultimately comes down to the willBreak function that is part of prettier core: https://github.com/prettier/prettier/blob/893f43a599dcaba249f012fecbc28a3dac008f81/src/document/doc-utils.js#L153

Any attribute returns:

{
                "type": "concat",
                "parts": [
                    {
                        "type": "line",
                        "hard": true
                    },
                    {
                        "type": "break-parent"
                    }
                ]
            }

This triggers willBreak to return true. I guess a work around is needed in php prettier for this condition.

tm1000 avatar May 31 '22 18:05 tm1000

@czosel what if we filter out attributes from declaration before sending it to some(willBreak)

tm1000 avatar Jun 02 '22 23:06 tm1000

I am experiencing the same issue. Any updates regarding this?

oliverschwendener avatar Oct 17 '22 10:10 oliverschwendener

@tm1000 Sounds good - PRs welcome! :)

czosel avatar Oct 17 '22 10:10 czosel

I think I have figured this out, we including the attrs as part of the is this a multiline function declaration check.

cseufert avatar Oct 18 '22 04:10 cseufert

Fixed in v0.19.2 :tada:

czosel avatar Nov 02 '22 06:11 czosel