Stillat / blade-parser-typescript

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

Multiline echo with chaining #20

Closed zepfietje closed 1 year ago

zepfietje commented 1 year ago

Often long chains inside {{ }} are put on multiple lines. However, after formatting it's all put on a single line.

Input

<label
    {{
        $attributes
            ->whereDoesntStartWith('wire:model')
            ->class(['flex gap-3 text-sm'])
    }}
>
</label>

Output

<label
    {{ $attributes->whereDoesntStartWith('wire:model')->class(['flex gap-3 text-sm']) }}
></label>

Expected input to remain unchanged or be something like:

<label {{
    $attributes
        ->whereDoesntStartWith('wire:model')
        ->class(['flex gap-3 text-sm'])
}}>
</label>
JohnathonKoster commented 1 year ago

The expected behavior of these things seems opposite to that of https://github.com/Stillat/blade-parser-typescript/issues/13

I'll see what I can do with the internal prettier stuff, though 🤔

zepfietje commented 1 year ago

Yeah it's tricky. Have a look at the Filament codebase if you like: https://github.com/filamentphp/filament. We have a lot of places where we really need to put things on multiple lines.

JohnathonKoster commented 1 year ago

💡 I suppose I could check if the {{ and }} were intentionally put on separate lines and swap that behavior internally (this also has the added benefit if you intentionally put them on a single line it stays that way)

zepfietje commented 1 year ago

Interesting idea! Let me know if you want me to test anything.

JohnathonKoster commented 1 year ago

I've got the behavior much much better with this new concept (this new behavior will also maintain the current behavior of #13 ). For this input:

<label
    {{
        $attributes
            ->whereDoesntStartWith('wire:model')
            ->class(['flex gap-3 text-sm'])
    }}
>
</label>

it will produce:

<label
    {{ 
        $attributes
            ->whereDoesntStartWith("wire:model")
            ->class(["flex gap-3 text-sm"])
     }}
></label>

and for the following:

<div {{ $attributes->class([
    'some classes',
    match ($someCondition) {
        true => 'more classes foo bar baz',
        default => 'even more classes foo bar baz',
    },
]) }} more attributes class="one two three" something="else">
</div>

it produces

<div
    {{ 
        $attributes->class([
            "some classes",
            match ($someCondition) {
                true => "more classes foo bar baz",
                default => "even more classes foo bar baz"
            },
        ])
     }}
    more
    attributes
    class="one two three"
    something="else"
></div>

It will place the {{ and }} on its own line and indent the contents relative to that (keeping the start/end inline with those was becoming a nightmare).

If that behavior looks good to you I'll tag a new version 🙂

zepfietje commented 1 year ago

Wow that looks perfect, John! Tag that version, then I'll test it on my codebases.

We should be pretty close to a perfect formatter now!! 😄

JohnathonKoster commented 1 year ago

This is now available in release 1.1.4