sveltejs / prettier-plugin-svelte

Format your svelte components using prettier.
MIT License
714 stars 95 forks source link

Svelte 5: Formatting removes closing snippet-render brackets when parameters contain closing bracket #428

Closed genericFJS closed 4 months ago

genericFJS commented 4 months ago

Describe the bug

When using the new Svelte 5 snippet feature and any parameter in the \@render-block contains a closing bracket ) (for example a parameter that is a function call), any code after the first bracket gets deleted on format (npm run format).

Reproduction

See repo: prettier-snippet-bug Or:

{@render test("a")} {@render test((()=>"a")())}

- Run `npm run format`
- Observe: Last line gets changed to invalid code (all code after first closing bracket is deleted): 
```svelte
{@render test((()}

This always happens when there is a closing bracket inside a parameter (i.e. any function call).

Logs

PS [...]\prettier-snippet-bug> npm run format

> prettier-snippet-bug@0.0.1 format
> prettier --write .

.prettierrc 44ms (unchanged)
jsconfig.json 4ms (unchanged)
package.json 2ms (unchanged)
README.md 23ms (unchanged)
src/app.d.ts 116ms (unchanged)
src/app.html 33ms (unchanged)
src/lib/index.js 3ms (unchanged)
src/routes/+page.svelte 54ms
svelte.config.js 5ms (unchanged)
vite.config.js 3ms (unchanged)

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 7 5700X 8-Core Processor
    Memory: 11.43 GB / 31.93 GB
  Binaries:
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.12.2 - ~\AppData\Roaming\npm\npm.CMD
    pnpm: 8.14.3 - ~\AppData\Local\pnpm\pnpm.EXE
  Browsers:
    Chrome: 121.0.6167.161
    Edge: Chromium (121.0.2277.112)
    Internet Explorer: 11.0.22621.1

Severity

annoyance

genericFJS commented 4 months ago

Thanks, @dummdidumm for tackling this issue. Sadly your fix did not help. Following (manual) test cases still get mangled:

{@render test((()=>"a")())}
{@render test(t())}

Result after running prettier:

{@render test((()}
{@render test(t()}

Can you reopen this ticket?

dummdidumm commented 4 months ago

My bad, I thought this is about the snippet block but it's about the render tag

genericFJS commented 4 months ago

Thanks a lot. That did the trick. I appreciate your work!