shufo / blade-formatter

An opinionated blade template formatter for Laravel that respects readability
https://www.npmjs.com/package/blade-formatter
MIT License
441 stars 24 forks source link

[Formatting Bug]: Unnecessary trailing comma on long `@php` inline directive #929

Open fh32000 opened 4 months ago

fh32000 commented 4 months ago

Version

1.41.1

Template before formatting

<html>
  <head></head>
  <body>
    @php($notification = App\Models\Notification::whereBetween('created_at', [auth('seller')->user()->created_at, Carbon::now()])->where('sent_to', 'seller')->whereDoesntHave('notificationSeenBy')->count())
  </body>
</html>

Template after formatting

<html>
    <head></head>
    <body>
        @php(
    $notification = App\Models\Notification::whereBetween('created_at', [auth('seller')->user()->created_at, Carbon::now()])->where('sent_to', 'seller')->whereDoesntHave('notificationSeenBy')->count(),
)
    </body>
</html>

Expected Behaviour

Relevant log output The error encountered during runtime due to the added comma results in a PHP syntax error:

PHP Parse error:  syntax error, unexpected ',', expecting ')' in /path/to/file.blade.php on line 1

Additional Context

Laravel Version: v10.48.9 Issue occurs in a development environment using Laravel Blade files, specifically impacting PHP expressions wrapped in @php directives. Steps to Reproduce

Use a Blade file with a complex @php directive containing multiple chained method calls. Format the file using the blade-formatter with the specified configuration. Observe the introduction of a comma at the end of the PHP expression, leading to syntax errors.

Relevant log output

node_modules/.bin/blade-formatter  resources/views/app.blade.php 

<html>
    <head></head>
    <body>
        @php(
    $notification = App\Models\Notification::whereBetween('created_at', [auth('seller')->user()->created_at, Carbon::now()])->where('sent_to', 'seller')->whereDoesntHave('notificationSeenBy')->count(),
)
    </body>
</html>
github-actions[bot] commented 2 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

shufo commented 1 month ago

@fh32000 I'd recommend you to use @php ~ @endphp directive for long line:

<html>
    <head></head>
    <body>
    @php
    $notification = App\Models\Notification::whereBetween('created_at', [auth('seller')->user()->created_at, Carbon::now()])->where('sent_to', 'seller')->whereDoesntHave('notificationSeenBy')->count()
    @endphp
    </body>
</html>

would formatting to

<html>

<head></head>

<body>
    @php
        $notification = App\Models\Notification::whereBetween('created_at', [
            auth('seller')->user()->created_at,
            Carbon::now()
        ])
            ->where('sent_to', 'seller')
            ->whereDoesntHave('notificationSeenBy')
            ->count();
    @endphp
</body>

</html>

Or use the --no-trailing-comma-php option for work around.

➜  ./bin/blade-formatter.js test.blade.php --no-trailing-comma-php
<html>

<head></head>

<body>
    @php(
    $notification = App\Models\Notification::whereBetween('created_at', [auth('seller')->user()->created_at, Carbon::now()])->where('sent_to', 'seller')->whereDoesntHave('notificationSeenBy')->count()
)
</body>

But I think this case should be fixed as it cause syntax error. Thanks.