sveltejs / prettier-plugin-svelte

Format your svelte components using prettier.
MIT License
745 stars 97 forks source link

prettier-plugin-svelte delete part of snippet types, resulting in broken svelte code #455

Closed lts20050703 closed 2 months ago

lts20050703 commented 3 months ago

Describe the bug

prettier-plugin-svelte delete part of snippet types, resulting in broken code

Reproduction

<script lang="ts">
  const arr: string[] = []
  const obj: { key: string } = {}
</script>
{#snippet test1(arg: typeof arr[number])}
{/snippet}
{#snippet test2(arg: typeof obj["key"])}
{/snippet}

will first format to

<script lang="ts">
  const arr: string[] = []
  const obj: { key: string } = {}
</script>
{#snippet test1(arg: (typeof arr)[number])}
{/snippet}
{#snippet test2(arg: (typeof obj)["key"])}
{/snippet}

then finally format to this broken svelte code

<script lang="ts">
  const arr: string[] = []
  const obj: { key: string } = {}
</script>
{#snippet test1(arg: (typeof arr)}
{/snippet}
{#snippet test2(arg: (typeof obj)}
{/snippet}

System Info

  System:
    OS: Linux 6.5 KDE neon 6.0 6.0
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 5.88 GB / 15.40 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.5.1 - ~/.local/share/nvm/v22.5.1/bin/node
    Yarn: 1.22.22 - ~/.local/share/nvm/v22.5.1/bin/yarn
    npm: 10.8.2 - ~/.local/share/nvm/v22.5.1/bin/npm
    pnpm: 9.4.0 - ~/.local/share/nvm/v22.5.1/bin/pnpm
    bun: 1.1.16 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 127.1.68.134
  npmPackages:
    svelte: ^5.0.0-next.206 => 5.0.0-next.206

Severity

annoyance

jonathonadams commented 3 months ago

Similarly

{#snippet foo({ onselect }: { onselect: (value: string) => void})}
    <p>...</p>
{/snippet}

Gets formatted to

{#snippet foo({ onselect }: { onselect: (value: string)}
      <p>...</p>
{/snippet}
mattlehrer commented 1 month ago

I am facing this problem with this snippet:

{#snippet variableIcon(type: (typeof variableTypes)[number])}
...
{/snippet}

It gets formatted as {#snippet variableIcon(type: (typeof variableTypes)}

Changing to this fixed it:

<script>
...
type IconTypes = (typeof variableTypes)[number];
</script>
{#snippet variableIcon(type: IconTypes)}
...
{/snippet}
dummdidumm commented 1 month ago

Apologies for the delay, this was fixed a while ago but only published just now.