Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

svelte-eslint-parser

sveltejs2.2mMIT1.1.3TypeScript support: included

Svelte parser for ESLint

svelte, sveltejs, eslint, parser

readme

NPM license NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status Coverage Status

svelte-eslint-parser

Svelte parser for ESLint.

Live DEMODiscord

Motivation

The svelte-eslint-parser aims to make it easy to create your own ESLint rules for Svelte.

eslint-plugin-svelte is an ESLint plugin built upon this parser, and it already implements some rules.

ESLint Plugins Using svelte-eslint-parser

eslint-plugin-svelte

ESLint plugin for Svelte.
Provides a variety of template-based checks using the Svelte AST.

@intlify/eslint-plugin-svelte

ESLint plugin for internationalization (i18n) in Svelte applications, offering helpful i18n-related rules.


Installation

npm install --save-dev eslint svelte-eslint-parser

Usage

ESLint Config (eslint.config.js)

import js from "@eslint/js";
import svelteParser from "svelte-eslint-parser";

export default [
  js.configs.recommended,
  {
    files: [
      "**/*.svelte",
      "*.svelte",
      // Need to specify the file extension for Svelte 5 with rune symbols
      "**/*.svelte.js",
      "*.svelte.js",
      "**/*.svelte.ts",
      "*.svelte.ts",
    ],
    languageOptions: {
      parser: svelteParser,
    },
  },
];

CLI

eslint "src/**/*.{js,svelte}"

Options

The parserOptions for this parser generally match what espree—ESLint's default parser—supports.

For example:

import svelteParser from "svelte-eslint-parser";

export default [
  // ...
  {
    files: [
      // Set .svelte/.js/.ts files. See above for more details.
    ],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        sourceType: "module",
        ecmaVersion: 2021,
        ecmaFeatures: {
          globalReturn: false,
          impliedStrict: false,
          jsx: false,
        },
      },
    },
  },
];

parserOptions.parser

Use the parserOptions.parser property to define a custom parser for <script> tags. Any additional parser options (besides the parser itself) are passed along to the specified parser.

import tsParser from "@typescript-eslint/parser";

export default [
  {
    files: [
      // Set .svelte/.js/.ts files. See above for more details.
    ],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        parser: tsParser,
      },
    },
  },
];

Using TypeScript in <script>

If you use @typescript-eslint/parser for TypeScript within <script> of .svelte files, additional configuration is needed. For example:

import tsParser from "@typescript-eslint/parser";

export default [
  // Other config for non-Svelte files
  {
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        project: "path/to/your/tsconfig.json",
        extraFileExtensions: [".svelte"],
      },
    },
  },
  // Svelte config
  {
    files: [
      // Set .svelte/.js/.ts files. See above for more details.
    ],
    languageOptions: {
      parser: svelteParser,
      // Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
      parserOptions: {
        parser: tsParser,
      },
    },
  },
];

Multiple parsers

To switch parsers for each language, provide an object:

import tsParser from "@typescript-eslint/parser";
import espree from "espree";

export default [
  {
    files: [
      // Set .svelte/.js/.ts files. See above for more details.
    ],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        parser: {
          ts: tsParser,
          js: espree,
          typescript: tsParser,
        },
      },
    },
  },
];

parserOptions.svelteConfig

If you use eslint.config.js, you can specify a svelte.config.js file via parserOptions.svelteConfig.

import svelteConfig from "./svelte.config.js";

export default [
  {
    files: [
      // Set .svelte/.js/.ts files. See above for more details.
    ],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        svelteConfig,
      },
    },
  },
];

If parserOptions.svelteConfig is not set, the parser attempts to statically read some config from svelte.config.js.

parserOptions.svelteFeatures

You can configure how Svelte-specific features are parsed via parserOptions.svelteFeatures.

For example:

export default [
  {
    files: [
      // Set .svelte/.js/.ts files. See above for more details.
    ],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        svelteFeatures: {
          // This is for Svelte 5. The default is true.
          // If false, ESLint won't recognize rune symbols.
          // If not specified, the parser tries to read compilerOptions.runes from `svelte.config.js`.
          // If `parserOptions.svelteConfig` is not given and static analysis fails, it defaults to true.
          runes: true,
        },
      },
    },
  },
];

Editor Integrations

Visual Studio Code

Use the dbaeumer.vscode-eslint extension provided by Microsoft.

By default, it only targets *.js and *.jsx, so you need to configure .svelte file support. For example, in .vscode/settings.json:

{
  "eslint.validate": ["javascript", "javascriptreact", "svelte"]
}

Usage for Custom Rules / Plugins

  • See AST.md for the AST specification. You can explore it on the Live DEMO.
  • This parser generates its own ScopeManager. Check the Live DEMO.
  • Several rules are [already implemented] in [eslint-plugin-svelte], and their source code can be a helpful reference.

Contributing

Contributions are welcome! Please open an issue or submit a PR on GitHub.
For internal details, see internal-mechanism.md.


License

See LICENSE (MIT) for rights and limitations.

changelog

svelte-eslint-parser

1.1.3

Patch Changes

  • #704 0436da6 Thanks @mcous! - Strip projectService from TS options when type information not needed

1.1.2

Patch Changes

1.1.1

Patch Changes

  • #692 1c533d8 Thanks @baseballyama! - fix: resolved issue of $props incorrectly detected as store when using variables named after runes like $props and props

1.1.0

Minor Changes

1.0.1

Patch Changes

1.0.0

Major Changes

Minor Changes

Patch Changes

1.0.0-next.13

Minor Changes

  • #645 6ff7516 Thanks @ota-meshi! - feat: improve scoping of snippet declarations acting as slot properties

Patch Changes

1.0.0-next.12

Minor Changes

1.0.0-next.11

Minor Changes

  • #641 89e053a Thanks @ota-meshi! - feat: replace declaration property of SvelteConstTag with declarations property

1.0.0-next.10

Patch Changes

1.0.0-next.9

Patch Changes

1.0.0-next.8

Patch Changes

1.0.0-next.7

Patch Changes

1.0.0-next.6

Minor Changes

1.0.0-next.5

Minor Changes

1.0.0-next.4

Minor Changes

1.0.0-next.3

Patch Changes

1.0.0-next.2

Major Changes

1.0.0-next.1

Major Changes

Minor Changes

Patch Changes

1.0.0-next.0

Major Changes

Minor Changes

0.43.0

Minor Changes

Patch Changes

0.42.0

Minor Changes

Patch Changes

0.41.1

Patch Changes

0.41.0

Minor Changes

0.40.0

Minor Changes

0.39.2

Patch Changes

0.39.1

Patch Changes

0.39.0

Minor Changes

0.38.0

Minor Changes

  • #534 a27c8e9 Thanks @baseballyama! - feat: support Svelte 5.0.0-next.155. (Add $state.is and replace $effect.active with $effect.tracking)

0.37.0

Minor Changes

0.36.0

Minor Changes

0.35.0

Minor Changes

0.34.1

Patch Changes

0.34.0

Minor Changes

Add experimental support for Svelte v5

0.34.0-next.12

Patch Changes

0.34.0-next.11

Minor Changes

0.34.0-next.10

Minor Changes

Patch Changes

0.34.0-next.9

Minor Changes

  • #476 92aeee3 Thanks @ota-meshi! - feat: change AST of {@render} and {#snippet} to match the latest version of svelte v5.

0.34.0-next.8

Patch Changes

0.34.0-next.7

Minor Changes

Patch Changes

0.34.0-next.6

Minor Changes

0.34.0-next.5

Minor Changes

0.34.0-next.4

Patch Changes

0.34.0-next.3

Minor Changes

Patch Changes

  • #434 0ef067b Thanks @ota-meshi! - fix: incorrect location when there is whitespace at the beginning of block

0.34.0-next.2

Minor Changes

0.34.0-next.1

Minor Changes

0.34.0-next.0

Minor Changes

0.33.1

Patch Changes

0.33.0

Minor Changes

Patch Changes

0.32.2

Patch Changes

0.32.1

Patch Changes

0.32.0

Minor Changes

0.31.0

Minor Changes

Patch Changes

0.30.0

Minor Changes

0.29.0

💥 Breaking Changes

Minor Changes

Patch Changes

0.28.0

Minor Changes

0.27.0

Minor Changes

0.26.1

Patch Changes

0.26.0

Minor Changes

0.25.1

Patch Changes

0.25.0

Minor Changes

0.24.2

Patch Changes

0.24.1

Patch Changes

0.24.0

Minor Changes

  • #292 ec061f5 Thanks @ota-meshi! - BREAKING: fix resolve to module scope for top level statements

    This change corrects the result of context.getScope(), but it is a breaking change.

  • #294 14d6e95 Thanks @ota-meshi! - feat: add peerDependenciesMeta to package.json

  • #295 924cd3e Thanks @ota-meshi! - feat: export name property

0.23.0

Minor Changes

0.22.4

Patch Changes

0.22.3

Patch Changes

0.22.2

Patch Changes

0.22.1

Patch Changes

0.22.0

Minor Changes

0.21.0

Minor Changes

0.20.0

Minor Changes

0.19.3

Patch Changes

0.19.2

Patch Changes

0.19.1

Patch Changes

0.19.0

Minor Changes

  • #230 c67a6c1 Thanks @ota-meshi! - fix: change virtual code to provide correct type information for reactive statements

0.18.4

Patch Changes

0.18.3

Patch Changes

0.18.2

Patch Changes

0.18.1

Patch Changes