shufo / prettier-plugin-blade

Format your blade template using Prettier
https://www.npmjs.com/package/@shufo/prettier-plugin-blade
MIT License
325 stars 8 forks source link

[Formatting Bug]: not compatible with `trailingCommaPHP` nor `phpVersion` from `@prettier/plugin-php` #231

Closed nelson6e65 closed 12 months ago

nelson6e65 commented 1 year ago

Version

1.8.13 | 1.11.1

Template before formatting

      {!! Form::select(
          'refundType',
          REFUND_TYPES,
          $data->refundType ?? null,
          [
              'class' => 'form-control refundType',
              'required' => 'required',
          ] + ($refundId ? ['disabled' => 'true'] : [])
      ) !!}

Template after formatting

      {!! Form::select(
          'refundType',
          REFUND_TYPES,
          $data->refundType ?? null,
          [
              'class' => 'form-control refundType',
              'required' => 'required',
          ] + ($refundId ? ['disabled' => 'true'] : []),
      ) !!}

Expected Behaviour

It should not add the trailing comma for PHP 7.2:

      {!! Form::select(
          'refundType',
          REFUND_TYPES,
          $data->refundType ?? null,
          [
              'class' => 'form-control refundType',
              'required' => 'required',
          ] + ($refundId ? ['disabled' => 'true'] : [])
      ) !!}

Relevant log output

      <?php echo Form::select(
          'refundType',
          REFUND_TYPES,
          $data->refundType ?? null,
          [
              'class' => 'form-control refundType',
              'required' => 'required',
          ] + ($refundId ? ['disabled' => 'true'] : []),
      ); ?>

"Parse error: syntax error, unexpected ')'

nelson6e65 commented 1 year ago

.prettierrc.json

{
  "printWidth": 120,
  "tabWidth": 2,
  "trailingComma": "es5",
  "semi": true,
  "singleQuote": true,
  "arrowParens": "always",

  "overrides": [
    {
      "files": ["*.php"],
      "options": {
        "phpVersion": "7.2",
        "tabWidth": 4,
        "printWidth": 120
      }
    },
    {
      "files": ["*.blade.php"],
      "options": {
        "phpVersion": "7.2",
        "trailingCommaPHP": false,
        "tabWidth": 2,
        "printWidth": 120,
        "parser": "blade",
        "sortHtmlAttributes": "vuejs",
        "wrapAttributes": "force-expand-multiline"
      }
    }
  ],
  "plugins": ["@prettier/plugin-php", "@shufo/prettier-plugin-blade"]
}
nelson6e65 commented 1 year ago

It happens even in a @php directive:

      @php
        echo Form::select(
            'refundType',
            REFUND_TYPES,
            $data->refundType ?? null,
            [
                'class' => 'form-control refundType',
                'required' => 'required',
            ] + ($refundId ? ['disabled' => 'true'] : []),
        );
      @endphp

In a normal PHP file, the trailing comma it's removed:

    public function test()
    {
        $refundId = null;

        echo Form::select(
            'refundType',
            REFUND_TYPES,
            null,
            [
                'class' => 'form-control refundType',
                'required' => 'required',
            ] + ($refundId ? ['disabled' => 'true'] : [])
        );
    }
shufo commented 1 year ago

Thanks @nelson6e65. Maybe we should support trailingCommaPHP and respect phpVersion for compatibility.

shufo commented 12 months ago

Added --trailing-comma-php option at https://github.com/shufo/prettier-plugin-blade/releases/tag/v1.12.0. You can disable trailing comma by setting like

.prettierrc.json

"options": {
~~
  "trailingCommaPHP": false,
}
nelson6e65 commented 12 months ago

Thanks a lot! 🎉

And 1.13 with phpVersion?

shufo commented 12 months ago

@nelson6e65

And 1.13 with phpVersion?

Yes, this package will respect phpVersion option if exists.