Skip to content

SublimeText 4: Prettier parser conflict with Svelte files requires manually specifying Svelte parser #481

@hkjens

Description

@hkjens

Description

When using SublimeText 4: Prettier defaults to HTML parser for .svelte files causing formatting errors with JavaScript/rune code. The issue can temporarily resolve after HTML changes because this triggers a different parser selection.

Root cause: Prettier needs explicit --parser=svelte and --plugin=prettier-plugin-svelte arguments to correctly format Svelte files.

My current workaround: use a wrapper script that enforces correct parser selection based on file extension.

Original issues

When modifying JavaScript code in a Svelte 5 component with runes, Prettier throws a syntax error. However, making any whitespace change in the HTML part makes Prettier work again.

Steps to Reproduce

  1. Start with this minimal example that uses Svelte 5 runes:
<script>
  import { onMount } from 'svelte';

  let someVar = 456;
  let imageData = $state(null);

  onMount(() => {
    if (!imageData) {
      imageData = { loaded: false };
    }
  });

  $effect(() => {
    if (imageData?.loaded) {
      console.log('Loaded!');
    }
  });
</script>

{#if imageData}
  <div
    class="w-[200px]"
    style="height: 200px">
    Content here
  </div>
{/if}
  1. Change 456 to 4567
  2. Try to save/format with Prettier
  3. Observe error
  4. Add a single space anywhere in the HTML part
  5. Try to save/format again - now it works

Error message

[error] test.svelte: SyntaxError: Unexpected token (8:5)
[error]    6 |
[error]    7 |   onMount(() => {
[error] >  8 |     if (!imageData) {
[error]      |     ^
[error]    9 |       imageData = { loaded: false };
[error]   10 |     }
[error]   11 |   });

Expected Behavior

Prettier should format the file without errors, regardless of whether changes are made to the script or HTML sections.

Environment

  • Sublime Text 4, build 4189
  • prettier: 3.4.2
  • prettier-plugin-svelte: 3.3.2
  • prettier-plugin-tailwindcss: 0.6.9
  • Svelte: 5.x

Additional Notes

  • The error message points to an if statement, but the error is triggered by changing a variable declaration elsewhere in the code
  • The error only occurs when modifying the script section
  • Adding any whitespace to the HTML section temporarily fixes the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions