Stillat / blade-parser-typescript

A Laravel Blade parser, compiler, and static analyzer written in TypeScript.
https://stillat.com
MIT License
82 stars 2 forks source link

Parentheses around variable assignment inside `@if` directive toggled on each formatter run #102

Closed bakerkretzmar closed 1 month ago

bakerkretzmar commented 4 months ago

Given the following code, the formatter toggles parentheses around the variable assignment in the @if every time it runs:

@if ($foo = $user->events()->first())
    <p>Test</p>
@endif

So it switches back and forth between these:

@if ($foo = $user->events()->first())
@if (($foo = $user->events()->first()))

It seems to be related to chaining something on after a method call, because this is fine:

@if ($foo = $user->events())

But this also triggers this issue:

@if ($foo = $user->events()->first)

The parentheses aren't necessary, so I would expect it to remove them if they're there, or at least not add them.

JohnathonKoster commented 4 months ago

Thanks for the report. Are you just using the defaults, or have you also configured it to use Pint to format PHP inside directives? Thanks!

bakerkretzmar commented 4 months ago

Not using Pint, pretty much the defaults I think. Here's my Prettier config in case it's helpful but it's mostly not related to Blade:

    {
        "htmlWhitespaceSensitivity": "ignore",
        "printWidth": 140,
        "singleQuote": true,
        "tabWidth": 4,
        "importOrder": [
            "^@(?!/)(.*)$",
            "^@/(.*)$",
            "^[./]"
        ],
        "importOrderSortSpecifiers": true,
        "plugins": [
            "@trivago/prettier-plugin-sort-imports",
            "prettier-plugin-blade",
            "prettier-plugin-tailwindcss"
        ],
        "pluginSearchDirs": false,
        "overrides": [
            {
                "files": "*.yml",
                "options": {
                    "tabWidth": 2
                }
            },
            {
                "files": [
                    "*.blade.php"
                ],
                "options": {
                    "htmlWhitespaceSensitivity": "css",
                    "parser": "blade"
                }
            }
        ]
    }
JohnathonKoster commented 4 months ago

That is very helpful, thank you!

RobertBlaauwendraad commented 1 month ago

Got the same issue for the following statements: @php(($count = WC()->cart->get_cart_contents_count())) @php(($bg = get_field('populated_bg_color') === 'gray' ? ' bg-gray-100 my-2 my-lg-5' : ''))

JohnathonKoster commented 1 month ago

@RobertBlaauwendraad Thanks for those examples. I was able to reproduce with that input; I'll work on a fix (hopefully ready sometime this upcoming weekend) 🙌

JohnathonKoster commented 1 month ago

Fix available in v2.1.17