svelte-eslint-parser
Svelte parser for ESLint
Last updated 4 years ago by ota-meshi .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install svelte-eslint-parser 
SYNC missed versions from official npm registry.

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 DEMO • Discord

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: 2024,
        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,
      },
    },
  },
];

Using JSDoc types in JavaScript

The configuration above also supports JSDoc type annotations in JavaScript <script> blocks (with no lang, lang="js", or lang="javascript") and .svelte.js files. Configure parserOptions.project or parserOptions.projectService to enable type-aware lint rules.

<script>
  /** @param {number} value */
  function increment(value) {
    return value + 1;
  }
</script>

{increment(1)}

When using multiple parsers, assign @typescript-eslint/parser to the js entry as well as ts to enable JSDoc type information in ordinary <script> blocks. JavaScript configured to use Espree continues to use Espree.

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,
        },
      },
    },
  },
];

Experimental

ts.sys.readFile hook for type-aware Svelte lint

⚠️ Experimental. Opt-in only; behavior may change or be removed.

Speeds up type-aware lint of .svelte files. Your ESLint config must already list .svelte via parserOptions.extraFileExtensions.

SVELTE_ESLINT_PARSER_EXPERIMENTAL_TS_SYS_HOOK=1 \
  eslint --no-cache --concurrency auto .

When enabled, the hook also resolves .svelte import specifiers to the imported component's virtual module, so type-aware rules see a component's real prop, event, and slot types across files. import Foo from "./Foo.svelte" is resolved through a virtual Foo.svelte.ts companion that the hook serves from the same translation — no tsconfig changes are required. If a real Foo.svelte.ts (a Svelte runes module) exists on disk, it always takes precedence and the hook leaves it untouched. In that case component prop types are not available for that component, because plain TypeScript resolution picks the module instead of the component. To avoid this name collision, enable the svelte/no-conflicting-module-names rule from eslint-plugin-svelte (v3.22.0 or later), which reports a Foo.svelte and Foo.svelte.ts pair.

Component typing currently requires a TypeScript <script> and supports Svelte 3, 4, and 5. Props come from $props() annotations or destructuring in runes mode, and from $$Props or exported let bindings in legacy mode. Defaulted props are optional. Explicit $$Events and $$Slots declarations supply event and slot types; otherwise those types remain permissive.

This is not a full replacement for Svelte's generated component declarations. Unannotated props without defaults use any, rest props keep the prop type open, and generic parameters are represented by their constraints rather than preserving a generic component signature. Instance exports and slot types inferred from template markup are not synthesized. JavaScript-only components do not receive this TypeScript translation.

Caveat: rules that read raw TypeScript diagnostics (program.getSemanticDiagnostics() and friends) report positions inside the parser's virtual shim — a pre-existing property of type-aware Svelte lint, not specific to the hook.


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.

Current Tags

  • 1.10.1                                ...           latest (a month ago)
  • 1.0.0-next.13                                ...           next (2 years ago)

151 Versions

  • 1.10.1                                ...           a month ago
  • 1.10.0                                ...           a month ago
  • 1.9.0                                ...           2 months ago
  • 1.8.1                                ...           3 months ago
  • 1.8.0                                ...           6 months ago
  • 1.7.1                                ...           6 months ago
  • 1.7.0                                ...           6 months ago
  • 1.6.1                                ...           7 months ago
  • 1.6.0                                ...           9 months ago
  • 1.5.1                                ...           9 months ago
  • 1.5.0                                ...           9 months ago
  • 1.4.1                                ...           a year ago
  • 1.4.0                                ...           a year ago
  • 1.3.3                                ...           a year ago
  • 1.3.2                                ...           a year ago
  • 1.3.1                                ...           a year ago
  • 1.3.0                                ...           a year ago
  • 1.2.0                                ...           2 years ago
  • 1.1.3                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 1.0.0-next.13                                ...           2 years ago
  • 1.0.0-next.12                                ...           2 years ago
  • 1.0.0-next.11                                ...           2 years ago
  • 1.0.0-next.10                                ...           2 years ago
  • 1.0.0-next.9                                ...           2 years ago
  • 1.0.0-next.8                                ...           2 years ago
  • 1.0.0-next.7                                ...           2 years ago
  • 1.0.0-next.6                                ...           2 years ago
  • 1.0.0-next.5                                ...           2 years ago
  • 1.0.0-next.4                                ...           2 years ago
  • 1.0.0-next.3                                ...           2 years ago
  • 1.0.0-next.2                                ...           2 years ago
  • 1.0.0-next.1                                ...           2 years ago
  • 1.0.0-next.0                                ...           2 years ago
  • 0.43.0                                ...           2 years ago
  • 0.42.0                                ...           2 years ago
  • 0.41.1                                ...           2 years ago
  • 0.41.0                                ...           2 years ago
  • 0.40.0                                ...           2 years ago
  • 0.39.2                                ...           2 years ago
  • 0.39.1                                ...           2 years ago
  • 0.39.0                                ...           2 years ago
  • 0.38.0                                ...           2 years ago
  • 0.37.0                                ...           2 years ago
  • 0.36.0                                ...           3 years ago
  • 0.35.0                                ...           3 years ago
  • 0.34.1                                ...           3 years ago
  • 0.34.0                                ...           3 years ago
  • 0.34.0-next.12                                ...           3 years ago
  • 0.34.0-next.11                                ...           3 years ago
  • 0.34.0-next.10                                ...           3 years ago
  • 0.34.0-next.9                                ...           3 years ago
  • 0.34.0-next.8                                ...           3 years ago
  • 0.34.0-next.7                                ...           3 years ago
  • 0.34.0-next.6                                ...           3 years ago
  • 0.34.0-next.5                                ...           3 years ago
  • 0.34.0-next.4                                ...           3 years ago
  • 0.34.0-next.3                                ...           3 years ago
  • 0.34.0-next.2                                ...           3 years ago
  • 0.34.0-next.1                                ...           3 years ago
  • 0.34.0-next.0                                ...           3 years ago
  • 0.33.1                                ...           3 years ago
  • 0.33.0                                ...           3 years ago
  • 0.32.2                                ...           3 years ago
  • 0.32.1                                ...           3 years ago
  • 0.32.0                                ...           3 years ago
  • 0.31.0                                ...           3 years ago
  • 0.30.0                                ...           3 years ago
  • 0.29.0                                ...           4 years ago
  • 0.28.0                                ...           4 years ago
  • 0.27.0                                ...           4 years ago
  • 0.26.1                                ...           4 years ago
  • 0.26.0                                ...           4 years ago
  • 0.25.1                                ...           4 years ago
  • 0.25.0                                ...           4 years ago
  • 0.24.2                                ...           4 years ago
  • 0.24.1                                ...           4 years ago
  • 0.24.0                                ...           4 years ago
  • 0.23.0                                ...           4 years ago
  • 0.22.4                                ...           4 years ago
  • 0.22.3                                ...           4 years ago
  • 0.22.2                                ...           4 years ago
  • 0.22.1                                ...           4 years ago
  • 0.22.0                                ...           4 years ago
  • 0.21.0                                ...           4 years ago
  • 0.20.0                                ...           4 years ago
  • 0.19.3                                ...           4 years ago
  • 0.19.2                                ...           4 years ago
  • 0.19.1                                ...           4 years ago
  • 0.19.0                                ...           4 years ago
  • 0.18.4                                ...           4 years ago
  • 0.18.3                                ...           4 years ago
  • 0.18.2                                ...           4 years ago
  • 0.18.1                                ...           4 years ago
  • 0.18.0                                ...           4 years ago
  • 0.17.0                                ...           4 years ago
  • 0.16.6                                ...           4 years ago
  • 0.16.5                                ...           4 years ago
  • 0.16.4                                ...           4 years ago
  • 0.16.3                                ...           4 years ago
  • 0.16.2                                ...           5 years ago
  • 0.16.1                                ...           5 years ago
  • 0.16.0                                ...           5 years ago
  • 0.15.0                                ...           5 years ago
  • 0.14.0                                ...           5 years ago
  • 0.13.0                                ...           5 years ago
  • 0.12.0                                ...           5 years ago
  • 0.11.0                                ...           5 years ago
  • 0.10.0                                ...           5 years ago
  • 0.9.2                                ...           5 years ago
  • 0.9.1                                ...           5 years ago
  • 0.9.0                                ...           5 years ago
  • 0.8.0                                ...           5 years ago
  • 0.7.0                                ...           5 years ago
  • 0.6.1                                ...           5 years ago
  • 0.6.0                                ...           5 years ago
  • 0.5.0                                ...           5 years ago
  • 0.4.6                                ...           5 years ago
  • 0.4.5                                ...           5 years ago
  • 0.4.4                                ...           5 years ago
  • 0.4.3                                ...           5 years ago
  • 0.4.2                                ...           5 years ago
  • 0.4.1                                ...           5 years ago
  • 0.4.0                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.19                                ...           6 years ago
  • 0.0.18                                ...           6 years ago
  • 0.0.17                                ...           6 years ago
  • 0.0.16                                ...           6 years ago
  • 0.0.15                                ...           6 years ago
  • 0.0.14                                ...           6 years ago
  • 0.0.13                                ...           6 years ago
  • 0.0.12                                ...           6 years ago
  • 0.0.11                                ...           6 years ago
  • 0.0.10                                ...           6 years ago
  • 0.0.9                                ...           6 years ago
  • 0.0.8                                ...           6 years ago
  • 0.0.7                                ...           6 years ago
  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Total 620
Today 0
This Week 151
This Month 472
Last Day 2
Last Week 53
Last Month 148
Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE