# eslint-plugin-svelte
## ESLint plugin for Svelte using AST
[Live Demo](https://eslint-online-playground.netlify.app/#eslint-plugin-svelte%20with%20typescript) •
[Documentation](https://sveltejs.github.io/eslint-plugin-svelte/) •
[Discord](https://svelte.dev/chat)
## Introduction
`eslint-plugin-svelte` is the official [ESLint](https://eslint.org/) plugin for [Svelte](https://svelte.dev/).\
It leverages the AST generated by [svelte-eslint-parser](https://github.com/sveltejs/svelte-eslint-parser) to provide custom linting for Svelte.\
> [!NOTE]
>
> `eslint-plugin-svelte` and `svelte-eslint-parser` cannot be used alongside [eslint-plugin-svelte3](https://github.com/sveltejs/eslint-plugin-svelte3).
## Installation
### CLI
The recommended way to get started is to use the CLI.
```sh
# new project
npx sv create
# existing project
npx sv add eslint
```
See the [CLI docs](https://svelte.dev/docs/cli/eslint) for more details.
### Manual Setup
```sh
npm install --save-dev svelte eslint eslint-plugin-svelte globals
```
> [!NOTE]
>
> **Requirements:**
>
> - ESLint v8.57.1, v9.0.0, and above
> - Node.js v18.18.0, v20.9.0, v21.1.0 and above
## Usage
Use `eslint.config.js` to configure rules. See [ESLint documentation](https://eslint.org/docs/latest/use/configure/configuration-files-new) for more details.
### Configuration
#### JavaScript project
```js
// eslint.config.js
import svelteConfig from './svelte.config.js';
import { defineConfig } from 'eslint/config';
import globals from 'globals';
import js from '@eslint/js';
import svelte from 'eslint-plugin-svelte';
export default defineConfig([
// ...
js.configs.recommended,
svelte.configs.recommended,
{
languageOptions: {
globals: {
...globals.browser,
// for Sveltekit in non-SPA mode
...globals.node
}
}
},
{
files: ['**/*.svelte', '**/*.svelte.js'],
languageOptions: {
parserOptions: {
// explicitly importing allows for better compatibilty and functionality with rules and other tooling that depend on the config file.
//
// Note: `eslint --cache` will fail with non-serializable properties.
// In those cases, please remove the non-serializable properties.
// svelteConfig: {
// ...svelteConfig,
// kit: {
// ...svelteConfig.kit,
// typescript: undefined
// }
// }
svelteConfig
}
}
},
{
rules: {
// Override or add rule settings here, such as:
// 'svelte/rule-name': 'error'
}
}
]);
```
#### TypeScript project
```shell
npm install --save-dev typescript-eslint
```
```js
// eslint.config.js
import svelteConfig from './svelte.config.js';
import { defineConfig } from 'eslint/config';
import globals from 'globals';
import js from '@eslint/js';
import ts from 'typescript-eslint';
import svelte from 'eslint-plugin-svelte';
export default defineConfig(
js.configs.recommended,
ts.configs.recommended,
svelte.configs.recommended,
{
languageOptions: {
parserOptions: {
// Keep this consistent for TypeScript and Svelte files to avoid project reloads.
extraFileExtensions: ['.svelte']
},
globals: {
...globals.browser,
// for Sveltekit in non-SPA mode
...globals.node
}
}
// ...
},
{
files: ['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js'],
// See more details at: https://typescript-eslint.io/packages/parser/
languageOptions: {
parserOptions: {
projectService: true,
// Specify a parser for each language, if needed:
// parser: {
// ts: ts.parser,
// typescript: ts.parser
// js: espree, // add `import espree from 'espree'`
// },
parser: ts.parser,
// explicitly importing allows for better compatibilty and functionality with rules and other tooling that depend on the config file.
//
// Note: `eslint --cache` will fail with non-serializable properties.
// In those cases, please remove the non-serializable properties.
// svelteConfig: {
// ...svelteConfig,
// kit: {
// ...svelteConfig.kit,
// typescript: undefined
// }
// }
svelteConfig
}
}
},
{
rules: {
// Override or add rule settings here, such as:
// 'svelte/rule-name': 'error'
}
}
);
```
> [!WARNING]
> The TypeScript parser uses a singleton internally, meaning it only respects the options provided during its initial initialization.
> If you try to change the options for a different file or override them later, the parser will ignore the new options, which may lead to errors.
> For more context, see [typescript-eslint/typescript-eslint#6778](https://github.com/typescript-eslint/typescript-eslint/issues/6778).
#### Svelte config inside `vite.config.js`
The Svelte config can also be passed to the `sveltekit()` or `svelte()` plugin in `vite.config.js`; SvelteKit 3 projects do this by default. Use [`@sveltejs/load-config`](https://www.npmjs.com/package/@sveltejs/load-config) to load it:
```js
// eslint.config.js
import { loadConfig } from '@sveltejs/load-config';
const svelteConfig = (await loadConfig('./', { traverse: false }))?.config;
```
### Available Configurations
This plugin provides the following configurations:
- **`svelte.configs.base`** - **Required** for Svelte parsing. Does not include any rules. Ideal for building a custom configurations.
- **`svelte.configs.recommended`** - Extends `base` and includes rules for best practices.
- **`svelte.configs.prettier`** - Extends `base` and disables rules that may conflict with [Prettier](https://prettier.io/). Prettier still needs to be configured to work with Svelte, for example, by using [prettier-plugin-svelte](https://github.com/sveltejs/prettier-plugin-svelte).
- **`svelte.configs.all`** - **Not Recommended** - Extends `base` and includes all rules. Subject to change with every major and minor release. Use at your own risk.
For more details, see [the rule list](https://sveltejs.github.io/eslint-plugin-svelte/rules/) to explore the rules provided by this plugin.
### settings.svelte
You can customize the behavior of this plugin using specific settings.
```js
// eslint.config.js
export default defineConfig([
// ...
{
settings: {
svelte: {
// Specifies an array of rules to ignore reports within the template.
// For example, use this to disable rules in the template that may produce unavoidable false positives.
ignoreWarnings: [
'@typescript-eslint/no-unsafe-assignment',
'@typescript-eslint/no-unsafe-member-access'
],
// Specifies options for Svelte compilation.
// This affects rules that rely on Svelte compilation,
// such as `svelte/valid-compile` and `svelte/no-unused-svelte-ignore`.
// Note that this setting does not impact ESLint’s custom parser.
compileOptions: {
// Specifies options related to PostCSS. You can disable the PostCSS processing by setting it to false.
postcss: {
// Specifies the path to the directory that contains the PostCSS configuration.
configFilePath: './path/to/my/postcss.config.js'
}
},
// Even if settings.svelte.kit is not specified, the rules will attempt to load information from svelte.config.js.
// However, if the default behavior does not work as expected, you should specify settings.svelte.kit explicitly.
// If you are using SvelteKit with a non-default configuration, you need to set the following options.
// The schema is a subset of SvelteKit’s configuration, so refer to the SvelteKit documentation for more details.
// https://svelte.dev/docs/kit/configuration
kit: {
files: {
routes: 'src/routes'
}
}
}
}
}
// ...
]);
```
SvelteKit versions are detected from the installed `@sveltejs/kit` package, falling back to its dependency range in `package.json`. The plugin recognizes SvelteKit 1, 2, and 3, including SvelteKit 3 prereleases. Version-specific rules run only for the versions they support.
For SvelteKit 3 projects using a custom routes directory, set `settings.svelte.kit.files.routes` to match the `files.routes` option passed to the `sveltekit` Vite plugin. SvelteKit 3 no longer uses `svelte.config.js`; see the [official migration guide](https://svelte.dev/docs/kit/migrating-to-sveltekit-3#Configuration).
## Editor Integrations
**Visual Studio Code**\
Install [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).\
Configure `.svelte` files in `.vscode/settings.json`:
## Migration Guide
If you’re migrating from `eslint-plugin-svelte@1` or [`@ota-meshi/eslint-plugin-svelte`](https://www.npmjs.com/package/@ota-meshi/eslint-plugin-svelte), see the [migration guide](https://sveltejs.github.io/eslint-plugin-svelte/migration/).
## Versioning Policy
This project follows [Semantic Versioning](https://semver.org/). Unlike [ESLint’s versioning policy](https://github.com/eslint/eslint#semantic-versioning-policy), new rules may be added to the recommended config in minor releases. If these rules cause unwanted warnings, you can disable them.
## Rules
:wrench: Indicates that the rule is fixable, and using `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the reported problems.\
:bulb: Indicates that some problems reported by the rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).\
:star: Indicates that the rule is included in the `plugin:svelte/recommended` config.
## Possible Errors
These rules relate to possible syntax or logic errors in Svelte code:
| Rule ID | Description | |
|:--------|:------------|:---|
| [svelte/infinite-reactive-loop](https://sveltejs.github.io/eslint-plugin-svelte/rules/infinite-reactive-loop/) | Svelte runtime prevents calling the same reactive statement twice in a microtask. But between different microtask, it doesn't prevent. | :star: |
| [svelte/no-bind-value-on-checkable-inputs](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-bind-value-on-checkable-inputs/) | disallow useless `bind:value` on `` and `` | :bulb: |
| [svelte/no-conflicting-module-names](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-conflicting-module-names/) | disallow a `.svelte` component and a same-named runes module (e.g. `Foo.svelte` and `Foo.svelte.ts`) from coexisting | |
| [svelte/no-dom-manipulating](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dom-manipulating/) | disallow DOM manipulating | :star: |
| [svelte/no-dupe-else-if-blocks](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-else-if-blocks/) | disallow duplicate conditions in `{#if}` / `{:else if}` chains | :star: |
| [svelte/no-dupe-on-directives](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-on-directives/) | disallow duplicate `on:` directives | :star: |
| [svelte/no-dupe-style-properties](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-style-properties/) | disallow duplicate style properties | :star: |
| [svelte/no-dupe-use-directives](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-use-directives/) | disallow duplicate `use:` directives | :star: |
| [svelte/no-nested-style-tag](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-nested-style-tag/) | disallow `