sveltejs / prettier-plugin-svelte

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

Formatting the `<script>` tags inside svelte files #438

Closed unovil closed 3 weeks ago

unovil commented 2 months ago

When I run npm run format to format the project files, the svelte files get processed however the typescript <script> tags in those files remain there without being formatted according to the override in the *.ts part of the .prettierrc file. Adding the embeddedLanguageFormatting option changed nothing. (Note that the *.ts override did work in all typescript files, but affected none of the <script> tags in svelte files)

Here is my .prettierrc file as of now:

{
  "plugins": ["prettier-plugin-svelte"],
  "overrides": [
    {
      "files": "*.svelte",
      "options": {
        "parser": "svelte",
        "htmlWhitespaceSensitivity": "ignore",
        "embeddedLanguageFormatting": "auto"
      }
    },
    {
      "files": "*.ts",
      "options": {
        "parser": "typescript",
        "semi": false,
        "quoteProps": "consistent",
        "singleQuote": false,
        "bracketSpacing": true,
        "arrowParens": "always",
        "trailingComma": "none",
        "endOfLine": "auto"
      }
    }
  ]
}

Is there a way to enforce formatting in the <script> tags?

dummdidumm commented 3 weeks ago

The embeddedLanguageFormatting is unrelated here, and as such there's no way to force script tags to be formatted according to options in that other overrides entry. You can however add the desired options to your svelte override.

{
  "plugins": ["prettier-plugin-svelte"],
  "overrides": [
    {
      "files": "*.svelte",
      "options": {
        "parser": "svelte",
        "htmlWhitespaceSensitivity": "ignore",
        "semi": false,
        "quoteProps": "consistent",
        "singleQuote": false,
        "bracketSpacing": true,
        "arrowParens": "always",
        "trailingComma": "none",
        "endOfLine": "auto"
      }
    },
    {
      "files": "*.ts",
      "options": {
        "parser": "typescript",
        "semi": false,
        "quoteProps": "consistent",
        "singleQuote": false,
        "bracketSpacing": true,
        "arrowParens": "always",
        "trailingComma": "none",
        "endOfLine": "auto"
      }
    }
  ]
}

You could also just add them at the top level in case they're the same across all your overrides

{
  "plugins": ["prettier-plugin-svelte"],
  "overrides": [
    {
      "files": "*.svelte",
      "options": {
        "parser": "svelte"
      }
    },
    {
      "files": "*.ts",
      "options": {
        "parser": "typescript"
      }
    }
  ],
  "htmlWhitespaceSensitivity": "ignore",
  "semi": false,
  "quoteProps": "consistent",
  "singleQuote": false,
  "bracketSpacing": true,
  "arrowParens": "always",
  "trailingComma": "none",
  "endOfLine": "auto"
}