Gathering detailed insights and metrics for eslint-plugin-svelte3
Gathering detailed insights and metrics for eslint-plugin-svelte3
Gathering detailed insights and metrics for eslint-plugin-svelte3
Gathering detailed insights and metrics for eslint-plugin-svelte3
npm install eslint-plugin-svelte3
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
373 Stars
193 Commits
44 Forks
15 Watching
1 Branches
24 Contributors
Updated on 02 Sept 2024
JavaScript (81%)
Svelte (18.51%)
TypeScript (0.49%)
Cumulative downloads
Total Downloads
Last day
-33.7%
8,552
Compared to previous day
Last week
0.4%
62,846
Compared to previous week
Last month
27.6%
246,180
Compared to previous month
Last year
-54.6%
2,834,548
Compared to previous year
eslint-plugin-svelte
insteadThis ESLint plugin is deprecated.
eslint-plugin-svelte
is the new official ESLint plugin for Svelte.
This plugin has supported Svelte's development for a long time. However, it does not handle the AST of the template well, which results in false positives/negatives and makes it very difficult to create custom ESLint rules.
To solve this issue, ota-meshi developed svelte-eslint-parser and eslint-plugin-svelte
which has proven to be more reliable and successful. The Svelte team therefore decided to switch the official plugin to eslint-plugin-svelte
.
An ESLint plugin for Svelte v3 components.
$:
labels are always allowed, regardless of configurationInstall the plugin package:
npm install --save-dev eslint-plugin-svelte3
Then add svelte3
to the plugins
array in your .eslintrc.*
, and set svelte3/svelte3
as the processor
for your Svelte components.
For example:
1module.exports = { 2 parserOptions: { 3 ecmaVersion: 2019, 4 sourceType: 'module' 5 }, 6 env: { 7 es6: true, 8 browser: true 9 }, 10 plugins: [ 11 'svelte3' 12 ], 13 overrides: [ 14 { 15 files: ['*.svelte'], 16 processor: 'svelte3/svelte3' 17 } 18 ], 19 rules: { 20 // ... 21 }, 22 settings: { 23 // ... 24 } 25};
By default, this plugin needs to be able to require('svelte/compiler')
. If ESLint, this plugin, and Svelte are all installed locally in your project, this should not be a problem.
If you want to use TypeScript, you'll need a different ESLint configuration. In addition to the Svelte plugin, you also need the ESLint TypeScript parser and plugin. Install typescript
, @typescript-eslint/parser
and @typescript-eslint/eslint-plugin
from npm and then adjust your config like this:
1module.exports = { 2 parser: '@typescript-eslint/parser', // add the TypeScript parser 3 plugins: [ 4 'svelte3', 5 '@typescript-eslint' // add the TypeScript plugin 6 ], 7 overrides: [ // this stays the same 8 { 9 files: ['*.svelte'], 10 processor: 'svelte3/svelte3' 11 } 12 ], 13 rules: { 14 // ... 15 }, 16 settings: { 17 'svelte3/typescript': () => require('typescript'), // pass the TypeScript package to the Svelte plugin 18 // OR 19 'svelte3/typescript': true, // load TypeScript as peer dependency 20 // ... 21 } 22};
If you also want to be able to use type-aware linting rules (which will result in slower linting, because the whole program needs to be compiled and type-checked), then you also need to add some parserOptions
configuration. The values below assume that your ESLint config is at the root of your project next to your tsconfig.json
. For more information, see here.
1module.exports = { 2 // ... 3 parserOptions: { // add these parser options 4 tsconfigRootDir: __dirname, 5 project: ['./tsconfig.json'], 6 extraFileExtensions: ['.svelte'], 7 }, 8 extends: [ // then, enable whichever type-aware rules you want to use 9 'eslint:recommended', 10 'plugin:@typescript-eslint/recommended', 11 'plugin:@typescript-eslint/recommended-requiring-type-checking' 12 ], 13 // ... 14};
There are some limitations to these type-aware rules currently. Specifically, checks in the context of reactive assignments and store subscriptions will report false positives or false negatives, depending on the rule. In the case of reactive assignments, you can work around this by explicitly typing the reactive variable. An example with the no-unsafe-member-access
rule:
1<script lang="ts"> 2 import { writable } from 'svelte/store'; 3 4 const store = writable([]); 5 $store.length; // incorrect no-unsafe-member-access error 6 7 $: assignment = []; 8 assignment.length; // incorrect no-unsafe-member-access error 9 // You can work around this by doing 10 let another_assignment: string[]; 11 $: another_assignment = []; 12 another_assignment.length; // OK 13</script>
Care needs to be taken when using this plugin alongside others. Take a look at this list of things you need to watch out for.
There are a few settings you can use to adjust this plugin's behavior. These go in the settings
object in your ESLint configuration.
Passing a function as a value for a setting (which some of the settings below require) is only possible when using a CommonJS .eslintrc.js
file, and not a JSON or YAML configuration file.
svelte3/ignore-warnings
This setting can be given a function that indicates whether to ignore a warning in the linting. The function will be passed a warning object and should return a boolean. Only warnings from the Svelte compiler itself can be filtered out through this function. Regular ESLint rules are configured/disabled through the corresponding ESLint settings.
The default is to not ignore any warnings.
svelte3/compiler-options
Most compiler options do not affect the validity of compiled components, but a couple of them can. If you are compiling to custom elements, or for some other reason need to control how the plugin compiles the components it's linting, you can use this setting.
This setting can be given an object of compiler options.
The default is to compile with { generate: false }
.
svelte3/ignore-styles
If you're using some sort of preprocessor on the component styles, then it's likely that when this plugin calls the Svelte compiler on your component, it will throw an exception. In a perfect world, this plugin would be able to apply the preprocessor to the component and then use source maps to translate any warnings back to the original source. In the current reality, however, you can instead simply disregard styles written in anything other than standard CSS. You won't get warnings about the styles from the linter, but your application will still use them (of course) and compiler warnings will still appear in your build logs.
This setting can be given a function that accepts an object of attributes on a <style>
tag (like that passed to a Svelte preprocessor) and returns whether to ignore the style block for the purposes of linting.
The default is to ignore styles when the <style>
tag has a lang=
or type=
attribute.
svelte3/named-blocks
When an ESLint processor processes a file, it is able to output named code blocks, which can each have their own linting configuration. When this setting is enabled, the code extracted from <script context='module'>
tag, the <script>
tag, and the template are respectively given the block names module.js
, instance.js
, and template.js
.
This means that to override linting rules in Svelte components, you'd instead have to target **/*.svelte/*.js
. But it also means that you can define an override targeting **/*.svelte/*_template.js
for example, and that configuration will only apply to linting done on the templates in Svelte components.
The default is to not use named code blocks.
svelte3/typescript
If you use TypeScript inside your Svelte components and want ESLint support, you need to set this option. It expects a function returning an instance of the TypeScript package. This probably means doing 'svelte3/typescript': () => require('typescript')
.
To support ESLint configuration files that are not written in CommonJS, this can also be set to true
, which behaves the same as () => require('typescript')
.
For backwards compatibility, it also supports being passed the TypeScript package directly, but this is not generally recommended as it unnecessarily loads the package in some situations.
The default is to not enable TypeScript support.
svelte3/compiler
In some esoteric setups, this plugin might not be able to find the correct instance of the Svelte compiler to use.
This setting can be given the result of require('.../path/to/svelte/compiler')
to indicate which instance should be used in linting the components.
The default is require('svelte/compiler')
from wherever the plugin is installed to.
It's probably a good idea to make sure you can lint from the command line before proceeding with configuring your editor.
Using this with the command line eslint
tool shouldn't require any special actions. Just remember that if you are running eslint
on a directory, you need to pass it the --ext
flag to tell it which nonstandard file extensions you want to lint.
See INTEGRATIONS.md for how to use this plugin with your text editor.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 13/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More