spatie / laravel-blade-comments

Add debug comments to your rendered output
https://freek.dev/2500-a-laravel-package-to-quickly-see-which-html-is-rendered-by-which-blade-view
MIT License
162 stars 9 forks source link

[Bug]: Function calls in @include causes exceptions with the IncludeCommenter #12

Closed bjhijmans closed 1 year ago

bjhijmans commented 1 year ago

What happened?

I have the following include:

@include('components.copy-to-clipboard', ['text' => $token, 'tooltip' => __('Copy API Token'), 'toast' => 'Token copied to clipboard',])

Due to the use of __() the IncludeCommenter broke the view compilation. In the compiled view I see:

<!-- Start include: components.copy-to-clipboard', ['text' => $token, 'tooltip' => __('Copy API Token --><?php echo $__env->make('components.copy-to-clipboard', ['text' => $token, 'tooltip' => __('Copy API Token')<!-- End include: components.copy-to-clipboard', ['text' => $token, 'tooltip' => __('Copy API Token -->, 'toast' => 'Token copied to clipboard',], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

and I get the following syntax error:

syntax error, unexpected token "include", expecting "->" or "?->" or "{" or "["

It looks like it sees the closing parenthesis of the translation function and decided that was the end of the include. But it's not. Replacing the __() call with a plain string fixes the issue, confirming that that is indeed the cause.

How to reproduce the bug

See above

Package Version

1.0.3

PHP Version

8.1.x

Laravel Version

10.13.0

Which operating systems does with happen with?

Linux

Notes

No response

timvandijck commented 1 year ago

The Regex for the IncludeCommenter should probably be improved to cover this.

If there's a Regex wizzard in the room I would be happy to accept a PR.

timvandijck commented 1 year ago

FYI: a blacklist for includes was added in release 1.1, this should enable you to have comments break code in some of these very specific cases.