Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork53
ESLint plugin for Svelte using AST
License
sveltejs/eslint-plugin-svelte
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
eslint-plugin-svelte
is the officialESLint plugin forSvelte.
It leverages the AST generated bysvelte-eslint-parser to provide custom linting for Svelte.
Note thateslint-plugin-svelte
andsvelte-eslint-parser
cannot be used alongsideeslint-plugin-svelte3.
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
Use theeslint.config.js
file to configure rules. For more details, see theESLint documentation.
// eslint.config.jsimportjsfrom'@eslint/js';importsveltefrom'eslint-plugin-svelte';importglobalsfrom'globals';importsvelteConfigfrom'./svelte.config.js';/**@type {import('eslint').Linter.Config[]} */exportdefault[js.configs.recommended, ...svelte.configs.recommended,{languageOptions:{globals:{ ...globals.browser, ...globals.node// Add this if you are using SvelteKit in non-SPA mode}}},{files:['**/*.svelte','**/*.svelte.js'],languageOptions:{parserOptions:{// We recommend importing and specifying svelte.config.js.// By doing so, some rules in eslint-plugin-svelte will automatically read the configuration and adjust their behavior accordingly.// While certain Svelte settings may be statically loaded from svelte.config.js even if you don’t specify it,// explicitly specifying it ensures better compatibility and functionality. svelteConfig}}},{rules:{// Override or add rule settings here, such as:// 'svelte/rule-name': 'error'}}];
npm install --save-dev typescript-eslint
// eslint.config.jsimportjsfrom'@eslint/js';importsveltefrom'eslint-plugin-svelte';importglobalsfrom'globals';importtsfrom'typescript-eslint';importsvelteConfigfrom'./svelte.config.js';exportdefaultts.config(js.configs.recommended, ...ts.configs.recommended, ...svelte.configs.recommended,{languageOptions:{globals:{ ...globals.browser, ...globals.node}}},{files:['**/*.svelte','**/*.svelte.ts','**/*.svelte.js'],// See more details at: https://typescript-eslint.io/packages/parser/languageOptions:{parserOptions:{projectService:true,extraFileExtensions:['.svelte'],// Add support for additional file extensions, such as .svelteparser:ts.parser,// Specify a parser for each language, if needed:// parser: {// ts: ts.parser,// js: espree, // Use espree for .js files (add: import espree from 'espree')// typescript: ts.parser// },// We recommend importing and specifying svelte.config.js.// By doing so, some rules in eslint-plugin-svelte will automatically read the configuration and adjust their behavior accordingly.// While certain Svelte settings may be statically loaded from svelte.config.js even if you don’t specify it,// explicitly specifying it ensures better compatibility and functionality. 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, seetypescript-eslint/typescript-eslint#6778.
This plugin provides the following configurations:
eslintPluginSvelte.configs.base
... Enables correct Svelte parsing.eslintPluginSvelte.configs.recommended
... Includesbase
configuration, plus rules to prevent errors or unintended behavior.eslintPluginSvelte.configs.prettier
... Disables rules that may conflict withPrettier. You still need to configure Prettier to work with Svelte manually, for example, by usingprettier-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 ofeslint-plugin-svelte
. Use at your own risk.
For more details, seethe rule list to explore the rules provided by this plugin.
You can customize the behavior of this plugin using specific settings.
// eslint.config.jsexportdefault[// ...{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/configurationkit:{files:{routes:'src/routes'}}}}}// ...];
Visual Studio Code
Installdbaeumer.vscode-eslint.
Configure.svelte
files in.vscode/settings.json
:
{"eslint.validate": ["javascript","javascriptreact","svelte"]}
If you’re migrating fromeslint-plugin-svelte
v1 or@ota-meshi/eslint-plugin-svelte
, see themigration guide.
This project followsSemantic Versioning. UnlikeESLint’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.
🔧 Indicates that the rule is fixable, and using--fix
option on thecommand line can automatically fix some of the reported problems.
💡 Indicates that some problems reported by the rule are manually fixable by editorsuggestions.
⭐ Indicates that the rule is included in theplugin:svelte/recommended
config.
These rules relate to possible syntax or logic errors in Svelte code:
Rule ID | Description | |
---|---|---|
svelte/infinite-reactive-loop | Svelte runtime prevents calling the same reactive statement twice in a microtask. But between different microtask, it doesn't prevent. | ⭐ |
svelte/no-dom-manipulating | disallow DOM manipulating | ⭐ |
svelte/no-dupe-else-if-blocks | disallow duplicate conditions in{#if} /{:else if} chains | ⭐ |
svelte/no-dupe-on-directives | disallow duplicateon: directives | ⭐ |
svelte/no-dupe-style-properties | disallow duplicate style properties | ⭐ |
svelte/no-dupe-use-directives | disallow duplicateuse: directives | ⭐ |
svelte/no-not-function-handler | disallow use of not function in event handler | ⭐ |
svelte/no-object-in-text-mustaches | disallow objects in text mustache interpolation | ⭐ |
svelte/no-raw-special-elements | Checks for invalid raw HTML elements | ⭐🔧 |
svelte/no-reactive-reassign | disallow reassigning reactive values | ⭐ |
svelte/no-shorthand-style-property-overrides | disallow shorthand style properties that override related longhand properties | ⭐ |
svelte/no-store-async | disallow using async/await inside svelte stores because it causes issues with the auto-unsubscribing features | ⭐ |
svelte/no-top-level-browser-globals | disallow using top-level browser global variables | |
svelte/no-unknown-style-directive-property | disallow unknownstyle:property | ⭐ |
svelte/prefer-svelte-reactivity | disallow using mutable instances of built-in classes where a reactive alternative is provided by svelte/reactivity | ⭐ |
svelte/require-store-callbacks-use-set-param | store callbacks must useset param | 💡 |
svelte/require-store-reactive-access | disallow to use of the store itself as an operand. Need to use $ prefix or get function. | ⭐🔧 |
svelte/valid-compile | disallow warnings when compiling. | |
svelte/valid-style-parse | require valid style element parsing |
These rules relate to security vulnerabilities in Svelte code:
Rule ID | Description | |
---|---|---|
svelte/no-at-html-tags | disallow use of{@html} to prevent XSS attack | ⭐ |
svelte/no-target-blank | disallowtarget="_blank" attribute withoutrel="noopener noreferrer" |
These rules relate to better ways of doing things to help you avoid problems:
Rule ID | Description | |
---|---|---|
svelte/block-lang | disallows the use of languages other than those specified in the configuration for the lang attribute of<script> and<style> blocks. | 💡 |
svelte/button-has-type | disallow usage of button without an explicit type attribute | |
svelte/no-add-event-listener | Warns against the use ofaddEventListener | 💡 |
svelte/no-at-debug-tags | disallow the use of{@debug} | ⭐💡 |
svelte/no-ignored-unsubscribe | disallow ignoring the unsubscribe method returned by thesubscribe() on Svelte stores. | |
svelte/no-immutable-reactive-statements | disallow reactive statements that don't reference reactive values. | ⭐ |
svelte/no-inline-styles | disallow attributes and directives that produce inline styles | |
svelte/no-inspect | Warns against the use of$inspect directive | ⭐ |
svelte/no-reactive-functions | it's not necessary to define functions in reactive statements | ⭐💡 |
svelte/no-reactive-literals | don't assign literal values in reactive statements | ⭐💡 |
svelte/no-svelte-internal | svelte/internal will be removed in Svelte 6. | ⭐ |
svelte/no-unnecessary-state-wrap | Disallow unnecessary $state wrapping of reactive classes | ⭐💡 |
svelte/no-unused-class-name | disallow the use of a class in the template without a corresponding style | |
svelte/no-unused-props | Warns about defined Props properties that are unused | ⭐ |
svelte/no-unused-svelte-ignore | disallow unused svelte-ignore comments | ⭐ |
svelte/no-useless-children-snippet | disallow explicit children snippet where it's not needed | ⭐ |
svelte/no-useless-mustaches | disallow unnecessary mustache interpolations | ⭐🔧 |
svelte/prefer-const | Requireconst declarations for variables that are never reassigned after declared | 🔧 |
svelte/prefer-destructured-store-props | destructure values from object stores for better change tracking & fewer redraws | 💡 |
svelte/prefer-writable-derived | Prefer using writable $derived instead of $state and $effect | ⭐💡 |
svelte/require-each-key | require keyed{#each} block | ⭐ |
svelte/require-event-dispatcher-types | require type parameters forcreateEventDispatcher | ⭐ |
svelte/require-optimized-style-attribute | require style attributes that can be optimized | |
svelte/require-stores-init | require initial value in store | |
svelte/valid-each-key | enforce keys to use variables defined in the{#each} block | ⭐ |
These rules relate to style guidelines, and are therefore quite subjective:
Rule ID | Description | |
---|---|---|
svelte/consistent-selector-style | enforce a consistent style for CSS selectors | |
svelte/derived-has-same-inputs-outputs | derived store should use same variable names between values and callback | 💡 |
svelte/first-attribute-linebreak | enforce the location of first attribute | 🔧 |
svelte/html-closing-bracket-new-line | Require or disallow a line break before tag's closing brackets | 🔧 |
svelte/html-closing-bracket-spacing | require or disallow a space before tag's closing brackets | 🔧 |
svelte/html-quotes | enforce quotes style of HTML attributes | 🔧 |
svelte/html-self-closing | enforce self-closing style | 🔧 |
svelte/indent | enforce consistent indentation | 🔧 |
svelte/max-attributes-per-line | enforce the maximum number of attributes per line | 🔧 |
svelte/mustache-spacing | enforce unified spacing in mustache | 🔧 |
svelte/no-extra-reactive-curlies | disallow wrapping single reactive statements in curly braces | 💡 |
svelte/no-restricted-html-elements | disallow specific HTML elements | |
svelte/no-spaces-around-equal-signs-in-attribute | disallow spaces around equal signs in attribute | 🔧 |
svelte/prefer-class-directive | require class directives instead of ternary expressions | 🔧 |
svelte/prefer-style-directive | require style directives instead of style attribute | 🔧 |
svelte/require-event-prefix | require component event names to start with "on" | |
svelte/shorthand-attribute | enforce use of shorthand syntax in attribute | 🔧 |
svelte/shorthand-directive | enforce use of shorthand syntax in directives | 🔧 |
svelte/sort-attributes | enforce order of attributes | 🔧 |
svelte/spaced-html-comment | enforce consistent spacing after the<!-- and before the--> in a HTML comment | 🔧 |
These rules extend the rules provided by ESLint itself, or other plugins to work well in Svelte:
Rule ID | Description | |
---|---|---|
svelte/no-inner-declarations | disallow variable orfunction declarations in nested blocks | ⭐ |
svelte/no-trailing-spaces | disallow trailing whitespace at the end of lines | 🔧 |
These rules relate to SvelteKit and its best Practices.
Rule ID | Description | |
---|---|---|
svelte/no-export-load-in-svelte-module-in-kit-pages | disallow exporting load functions in*.svelte module in SvelteKit page components. | ⭐ |
svelte/no-navigation-without-base | disallow using navigation (links, goto, pushState, replaceState) without the base path | |
svelte/valid-prop-names-in-kit-pages | disallow props other than data or errors in SvelteKit page components. | ⭐ |
Rule ID | Description | |
---|---|---|
svelte/experimental-require-slot-types | require slot type declaration using the$$Slots interface | |
svelte/experimental-require-strict-events | require the strictEvents attribute on<script> tags |
These rules relate to this plugin works:
Rule ID | Description | |
---|---|---|
svelte/comment-directive | support comment-directives in HTML template | ⭐ |
svelte/system | system rule for working this plugin | ⭐ |
⚠️ We're going to remove deprecated rules in the next major release. Please migrate to successor/new rules.- 😇 We don't fix bugs which are in deprecated rules since we don't have enough resources.
Rule ID | Replaced by |
---|---|
svelte/@typescript-eslint/no-unnecessary-condition | This rule is no longer needed when using svelte-eslint-parser>=v0.19.0. |
svelte/no-dynamic-slot-name | Now Svelte compiler itself throws an compile error. |
svelte/no-goto-without-base | svelte/no-navigation-without-base |
Contributions are welcome! Please open an issue or submit a PR. For more details, seeCONTRIBUTING.md.
Refer tosvelte-eslint-parser for AST details.
SeeLICENSE (MIT).
About
ESLint plugin for Svelte using AST
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.