

Introduction
eslint-plugin-svelte
is the official ESLint plugin for Svelte.
It leverages the AST generated by svelte-eslint-parser to provide custom linting for Svelte.
Note that eslint-plugin-svelte
and svelte-eslint-parser
cannot be used alongside eslint-plugin-svelte3.
Installation
npm install --save-dev svelte eslint eslint-plugin-svelte globals
[!NOTE]
Requirements:
- ESLint v8.57.1, v9.0.0, and above
- Node.js v18.20.4, v20.18.0, v22.10.0, and above
Usage
Use the eslint.config.js
file to configure rules. For more details, see the ESLint documentation.
Configuration
JavaScript project
import js from '@eslint/js';
import svelte from 'eslint-plugin-svelte';
import globals from 'globals';
import svelteConfig from './svelte.config.js';
export default [
js.configs.recommended,
...svelte.configs.recommended,
{
languageOptions: {
globals: {
...globals.browser,
...globals.node
}
}
},
{
files: ['**/*.svelte', '**/*.svelte.js'],
languageOptions: {
parserOptions: {
svelteConfig
}
}
},
{
rules: {
}
}
];
TypeScript project
npm install --save-dev typescript-eslint
import js from '@eslint/js';
import svelte from 'eslint-plugin-svelte';
import globals from 'globals';
import ts from 'typescript-eslint';
import svelteConfig from './svelte.config.js';
export default ts.config(
js.configs.recommended,
...ts.configs.recommended,
...svelte.configs.recommended,
{
languageOptions: {
globals: {
...globals.browser,
...globals.node
}
}
},
{
files: ['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js'],
languageOptions: {
parserOptions: {
projectService: true,
extraFileExtensions: ['.svelte'],
parser: ts.parser,
svelteConfig
}
}
},
{
rules: {
}
}
);
[!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.
Available Configurations
This plugin provides the following configurations:
eslintPluginSvelte.configs.base
... Enables correct Svelte parsing.eslintPluginSvelte.configs.recommended
... Includes base
configuration, plus rules to prevent errors or unintended behavior.eslintPluginSvelte.configs.prettier
... Disables rules that may conflict with Prettier. You still need to configure Prettier to work with Svelte manually, for example, by using prettier-plugin-svelte.eslintPluginSvelte.configs.all
... Includes all available rules. Note: This configuration is not recommended for production use, as it changes with every minor and major version of eslint-plugin-svelte
. Use at your own risk.
For more details, see the rule list to explore the rules provided by this plugin.
settings.svelte
You can customize the behavior of this plugin using specific settings.
export default [
{
settings: {
svelte: {
ignoreWarnings: [
'@typescript-eslint/no-unsafe-assignment',
'@typescript-eslint/no-unsafe-member-access'
],
compileOptions: {
postcss: {
configFilePath: './path/to/my/postcss.config.js'
}
},
kit: {
files: {
routes: 'src/routes'
}
}
}
}
}
];
Editor Integrations
Visual Studio Code
Install dbaeumer.vscode-eslint.
Configure .svelte
files in .vscode/settings.json
:
{
"eslint.validate": ["javascript", "javascriptreact", "svelte"]
}
Migration Guide
If you’re migrating from eslint-plugin-svelte
v1 or @ota-meshi/eslint-plugin-svelte
, see the migration guide.
Versioning Policy
This project follows Semantic Versioning. Unlike ESLint’s 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 can automatically fix some of the reported problems.
:bulb: Indicates that some problems reported by the rule are manually fixable by editor 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:
Security Vulnerability
These rules relate to security vulnerabilities in Svelte code:
Best Practices
These rules relate to better ways of doing things to help you avoid problems:
Stylistic Issues
These rules relate to style guidelines, and are therefore quite subjective:
Extension Rules
These rules extend the rules provided by ESLint itself, or other plugins to work well in Svelte:
SvelteKit
These rules relate to SvelteKit and its best Practices.
Experimental
:warning: These rules are considered experimental and may change or be removed in the future:
System
These rules relate to this plugin works:
Deprecated
- :warning: We're going to remove deprecated rules in the next major release. Please migrate to successor/new rules.
- :innocent: We don't fix bugs which are in deprecated rules since we don't have enough resources.
Contributing
Contributions are welcome! Please open an issue or submit a PR. For more details, see CONTRIBUTING.md.
Refer to svelte-eslint-parser for AST details.
License
See LICENSE (MIT).