zackad / prettier-plugin-twig

Code formatting plugin for Prettier to handle Twig templates
Apache License 2.0
30 stars 2 forks source link

Plugin error when using Twig `cva` function and spread operator #16

Open gremo opened 3 months ago

gremo commented 3 months ago

First, thanks for your work, much appreciated!

The plugin will not work, at least, with Twig Components using cva function:

{% props theme = null, text = null %}

<div
    {{ attributes.defaults({
        class: cva({
            base: "alert shadow-md",
            variants: {
                theme: {
                    info: "alert-info",
                    success: "alert-success",
                    warning: "alert-warning",
                    error: "alert-error",
                },
            },
        }).apply({ theme }),
    }) }}
>
</div>

Output:

root@6ec48a6a90a1:/var/www/html# node_modules/.bin/prettier -c templates/test.html.twig 
Checking formatting...
templates/test.html.twig
[error] templates/test.html.twig: Error: ERROR: Invalid Token
[error]   13 |                 },
[error]   14 |             },
[error] > 15 |         }).apply({ theme }),
[error]      |                          ^
[error]   16 |     }) }}
[error]   17 | >
[error]   18 | </div>
[error] 
[error] Expected : but found } instead.
[error]     at TokenStream.error (/var/www/html/node_modules/melody-parser/lib/index.js:1362:22)
[error]     at TokenStream.expect (/var/www/html/node_modules/melody-parser/lib/index.js:1340:14)
[error]     at Parser.matchMap (/var/www/html/node_modules/melody-parser/lib/index.js:1113:20)
[error]     at Parser.matchPrimaryExpression (/var/www/html/node_modules/melody-parser/lib/index.js:974:33)
[error]     at Parser.getPrimary (/var/www/html/node_modules/melody-parser/lib/index.js:935:21)
[error]     at Parser.matchExpression (/var/www/html/node_modules/melody-parser/lib/index.js:885:25)
[error]     at Parser.matchArguments (/var/www/html/node_modules/melody-parser/lib/index.js:1249:32)
[error]     at Parser.matchSubscriptExpression (/var/www/html/node_modules/melody-parser/lib/index.js:1172:70)
[error]     at Parser.matchPostfixExpression (/var/www/html/node_modules/melody-parser/lib/index.js:1136:29)
[error]     at Parser.matchPrimaryExpression (/var/www/html/node_modules/melody-parser/lib/index.js:988:21)
All matched files use Prettier code style!

Another error occurs also with spread operator:

<twig:Alert {{ ...attributes }} />
root@6ec48a6a90a1:/var/www/html# node_modules/.bin/prettier -c templates/test.html.twig 
Checking formatting...
templates/test.html.twig
[error] templates/test.html.twig: Error: ERROR: Unexpected token "operator" of value ".."
[error] > 1 | <twig:Alert {{ ...attributes }} />
[error]     |                ^
[error]   2 | 
[error]     at TokenStream.error (/var/www/html/node_modules/melody-parser/lib/index.js:1362:22)
[error]     at Parser.error (/var/www/html/node_modules/melody-parser/lib/index.js:830:21)
[error]     at Parser.matchPrimaryExpression (/var/www/html/node_modules/melody-parser/lib/index.js:976:26)
[error]     at Parser.getPrimary (/var/www/html/node_modules/melody-parser/lib/index.js:935:21)
[error]     at Parser.matchExpression (/var/www/html/node_modules/melody-parser/lib/index.js:885:25)
[error]     at Parser.matchAttributes (/var/www/html/node_modules/melody-parser/lib/index.js:815:46)
[error]     at Parser.matchElement (/var/www/html/node_modules/melody-parser/lib/index.js:726:14)
[error]     at Parser.parse (/var/www/html/node_modules/melody-parser/lib/index.js:627:32)
[error]     at Object.parse (/var/www/html/node_modules/@zackad/prettier-plugin-twig-melody/src/parser.js:84:24)
[error]     at parse4 (file:///var/www/html/node_modules/prettier/index.mjs:22117:24)
All matched files use Prettier code style!

Are these easy to fix?

zackad commented 2 months ago

Unfortunately it is not an easy fix. As you can see from the log, the error come from melody-parser package that has been abandoned.

I'm working on consolidating all external dependencies into this repo directly. Until that can be done, I'm afraid this issue won't be fixed.