• Stars
    star
    242
  • Rank 160,837 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 3 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ESLint plugin for Svelte using AST

Introduction

eslint-plugin-svelte is the official ESLint plugin for Svelte.
It provides many unique check rules by using the template AST.
You can check on the Online DEMO.

NPM license NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status

type-coverage Conventional Commits Code Style: Prettier changesets

πŸ“› What is this plugin?

ESLint plugin for Svelte.
It provides many unique check rules using the AST generated by svelte-eslint-parser.

❗ Attention

The svelte-eslint-parser and the eslint-plugin-svelte can not be used with the eslint-plugin-svelte3.

Migration Guide

To migrate from eslint-plugin-svelte v1, or @ota-meshi/eslint-plugin-svelte, please refer to the migration guide.

πŸ“– Documentation

See documents.

πŸ’Ώ Installation

npm install --save-dev eslint eslint-plugin-svelte svelte

Requirements

  • ESLint v7.0.0 and above
  • Node.js v14.17.x, v16.x and above

πŸ“– Usage

Configuration

Use .eslintrc.* file to configure rules. See also: https://eslint.org/docs/user-guide/configuring.

Example .eslintrc.js:

module.exports = {
  extends: [
    // add more generic rule sets here, such as:
    // 'eslint:recommended',
    'plugin:svelte/recommended'
  ],
  rules: {
    // override/add rules settings here, such as:
    // 'svelte/rule-name': 'error'
  }
};

This plugin provides configs:

  • plugin:svelte/base ... Configuration to enable correct Svelte parsing.
  • plugin:svelte/recommended ... Above, plus rules to prevent errors or unintended behavior.
  • plugin:svelte/prettier ... Turns off rules that may conflict with Prettier (You still need to configure prettier to work with svelte yourself, for example by using prettier-plugin-svelte.).
  • plugin:svelte/all ... All rules. This configuration is not recommended for production use because it changes with every minor and major version of eslint-plugin-svelte. Use it at your own risk.

See the rule list to get the rules that this plugin provides.

::: warning ❗ Attention

The eslint-plugin-svelte can not be used with the eslint-plugin-svelte3. If you are using eslint-plugin-svelte3 you need to remove it.

  "plugins": [
-   "svelte3"
  ]

:::

Parser Configuration

If you have specified a parser, you need to configure a parser for .svelte.

For example, if you are using the "@babel/eslint-parser", configure it as follows:

module.exports = {
  // ...
  extends: ['plugin:svelte/recommended'],
  // ...
  parser: '@babel/eslint-parser',
  // Add an `overrides` section to add a parser configuration for svelte.
  overrides: [
    {
      files: ['*.svelte'],
      parser: 'svelte-eslint-parser'
    }
    // ...
  ]
  // ...
};

For example, if you are using the "@typescript-eslint/parser", and if you want to use TypeScript in <script> of .svelte, you need to add more parserOptions configuration.

module.exports = {
  // ...
  extends: ['plugin:svelte/recommended'],
  // ...
  parser: '@typescript-eslint/parser',
  parserOptions: {
    // ...
    project: 'path/to/your/tsconfig.json',
    extraFileExtensions: ['.svelte'] // This is a required setting in `@typescript-eslint/parser` v4.24.0.
  },
  overrides: [
    {
      files: ['*.svelte'],
      parser: 'svelte-eslint-parser',
      // Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
      parserOptions: {
        parser: '@typescript-eslint/parser'
      }
    }
    // ...
  ]
  // ...
};

If you have a mix of TypeScript and JavaScript in your project, use a multiple parser configuration.

module.exports = {
  // ...
  overrides: [
    {
      files: ['*.svelte'],
      parser: 'svelte-eslint-parser',
      parserOptions: {
        parser: {
          // Specify a parser for each lang.
          ts: '@typescript-eslint/parser',
          js: 'espree',
          typescript: '@typescript-eslint/parser'
        }
      }
    }
    // ...
  ]
  // ...
};

See also https://github.com/sveltejs/svelte-eslint-parser#readme.

::: warning ❗ Attention

The TypeScript parser uses a singleton internally and it will only use the options given to it when it was first initialized. If trying to change the options for a different file or override, the parser will simply ignore the new options (which may result in an error). See typescript-eslint/typescript-eslint#6778 for some context.

:::

settings.svelte

You can change the behavior of this plugin with some settings.

e.g.

module.exports = {
  // ...
  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'
        }
      }
    }
  }
  // ...
};

settings.svelte.ignoreWarnings

Specifies an array of rules that ignore reports in the template.
For example, set rules on the template that cannot avoid false positives.

settings.svelte.compileOptions

Specifies options for Svelte compile. Effects rules that use Svelte compile. The target rules are svelte/valid-compile and svelte/no-unused-svelte-ignore. Note that it has no effect on ESLint's custom parser.

  • postcss ... Specifies options related to PostCSS. You can disable the PostCSS process by specifying false.
    • configFilePath ... Specifies the path of the directory containing the PostCSS configuration.

settings.svelte.kit

If you use SvelteKit with not default configuration, you need to set below configurations. The schema is subset of SvelteKit's configuration. Therefore please check SvelteKit docs for more details.

e.g.

module.exports = {
  // ...
  settings: {
    svelte: {
      kit: {
        files: {
          routes: 'src/routes'
        }
      }
    }
  }
  // ...
};

Running ESLint from the command line

If you want to run eslint from the command line, make sure you include the .svelte extension using the --ext option or a glob pattern, because ESLint targets only .js files by default.

Examples:

eslint --ext .js,.svelte src
eslint "src/**/*.{js,svelte}"

πŸ’» Editor Integrations

Visual Studio Code

Use the dbaeumer.vscode-eslint extension that Microsoft provides officially.

You have to configure the eslint.validate option of the extension to check .svelte files, because the extension targets only *.js or *.jsx files by default.

Example .vscode/settings.json:

{
  "eslint.validate": ["javascript", "javascriptreact", "svelte"]
}

βœ… Rules

πŸ”§ Indicates that the rule is fixable, and using --fix option on the command line can automatically fix some of the reported problems.
πŸ’‘ Indicates that some problems reported by the rule are manually fixable by editor suggestions.
⭐ 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 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 duplicate on: directives
svelte/no-dupe-style-properties disallow duplicate style properties ⭐
svelte/no-dupe-use-directives disallow duplicate use: directives
svelte/no-dynamic-slot-name disallow dynamic slot name β­πŸ”§
svelte/no-export-load-in-svelte-module-in-kit-pages disallow exporting load functions in *.svelte module in SvelteKit page components.
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-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-unknown-style-directive-property disallow unknown style:property ⭐
svelte/require-store-callbacks-use-set-param store callbacks must use set 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-prop-names-in-kit-pages disallow props other than data or errors in SvelteKit page components.

Security Vulnerability

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 disallow target="_blank" attribute without rel="noopener noreferrer"

Best Practices

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-at-debug-tags disallow the use of {@debug} ⭐
svelte/no-immutable-reactive-statements disallow reactive statements that don't reference reactive values.
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-unused-class-name disallow the use of a class in the template without a corresponding style
svelte/no-unused-svelte-ignore disallow unused svelte-ignore comments ⭐
svelte/no-useless-mustaches disallow unnecessary mustache interpolations πŸ”§
svelte/prefer-destructured-store-props destructure values from object stores for better change tracking & fewer redraws πŸ’‘
svelte/require-each-key require keyed {#each} block
svelte/require-event-dispatcher-types require type parameters for createEventDispatcher
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

Stylistic Issues

These rules relate to style guidelines, and are therefore quite subjective:

Rule ID Description
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-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/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 πŸ”§

Extension Rules

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 or function declarations in nested blocks ⭐
svelte/no-trailing-spaces disallow trailing whitespace at the end of lines πŸ”§

Experimental

⚠️ These rules are considered experimental and may change or be removed in the future:

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

System

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 ⭐

Deprecated

  • ⚠️ 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.

🍻 Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

See also CONTRIBUTING.md

Working With Rules

This plugin uses svelte-eslint-parser for the parser. Check here to find out about AST.

πŸ”’ License

See the LICENSE file for license rights and limitations (MIT).

More Repositories

1

svelte

Cybernetically enhanced web apps
JavaScript
75,642
star
2

kit

web development, streamlined
JavaScript
17,332
star
3

sapper

The next small thing in web development, powered by Svelte
TypeScript
7,031
star
4

realworld

SvelteKit implementation of the RealWorld app
Svelte
2,129
star
5

template

Template for building basic applications with Svelte
JavaScript
1,732
star
6

svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
TypeScript
1,687
star
7

svelte-devtools

A browser extension to inspect Svelte application by extending your browser devtools capabilities
Svelte
1,312
star
8

language-tools

The Svelte Language Server, and official extensions which use it
TypeScript
1,128
star
9

vite-plugin-svelte

Svelte plugin for http://vitejs.dev/
JavaScript
787
star
10

sapper-template

Starter template for Sapper apps
JavaScript
706
star
11

prettier-plugin-svelte

Format your svelte components using prettier.
TypeScript
684
star
12

svelte-virtual-list

A virtual list component for Svelte apps
JavaScript
656
star
13

integrations

Ways to incorporate Svelte into your stack
633
star
14

gl

A (very experimental) project to bring WebGL to Svelte
JavaScript
608
star
15

svelte-loader

Webpack loader for svelte components.
JavaScript
591
star
16

community-legacy

Svelte community meetups, packages, resources, recipes, showcase websites, and more
Svelte
565
star
17

component-template

A base for building shareable Svelte components
JavaScript
551
star
18

rollup-plugin-svelte

Compile Svelte components with Rollup
JavaScript
489
star
19

learn.svelte.dev

A soup-to-nuts interactive tutorial on how to build apps with Svelte
Svelte
440
star
20

eslint-plugin-svelte3

An ESLint plugin for Svelte v3 components.
JavaScript
377
star
21

svelte-scroller

A <Scroller> component for Svelte apps
Svelte
337
star
22

template-webpack

Template for building basic Svelte applications with webpack
JavaScript
301
star
23

sites

Monorepo for the sites in the Svelte ecosystem
Svelte
277
star
24

svelte-repl

The <Repl> component used on the Svelte website
Svelte
273
star
25

rfcs

RFCs for changes to Svelte
269
star
26

sapper-studio

An electron app for building Sapper projects
HTML
220
star
27

v2.svelte.dev

The Svelte v2 website
HTML
209
star
28

svelte-hmr

HMR commons for Svelte 3
JavaScript
201
star
29

site-kit

Svelte
173
star
30

hn.svelte.dev

Hacker News clone built with Svelte and Sapper
Svelte
163
star
31

svelte-todomvc

TodoMVC implemented in Svelte
Svelte
137
star
32

svelte-subdivide

A component for building Blender-style layouts in Svelte apps
JavaScript
129
star
33

svelte-cli

Command line interface for Svelte
JavaScript
104
star
34

gestures

Svelte actions for cross-platform gesture detection
TypeScript
88
star
35

svelte-hackernews

WIP Hacker News clone written in Svelte
JavaScript
78
star
36

svelte-eslint-parser

Svelte parser for ESLint
TypeScript
74
star
37

svelte-custom-elements

Turn Svelte components into web components
JavaScript
49
star
38

svelte-extras

Extra methods for Svelte components
JavaScript
43
star
39

sapper-template-rollup

Starter Rollup template for Sapper apps
JavaScript
40
star
40

svelte-upgrade

Upgrade your Svelte templates for version 2
JavaScript
36
star
41

branding

Logos etc for Svelte and related projects
35
star
42

hn.svelte.technology

Hacker News, built with Sapper
HTML
35
star
43

svelte-transitions

Officially supported transition plugins for Svelte
JavaScript
34
star
44

examples

A collection of Svelte(Kit) examples
33
star
45

sapper-template-webpack

Starter webpack template for Sapper apps
JavaScript
33
star
46

kit-template-default

The default SvelteKit template, generated with create-svelte
Svelte
32
star
47

discord-bot

TypeScript
32
star
48

svelte-atom

Syntax, diagnostics, and other smarts for Svelte in Atom
JavaScript
30
star
49

community

27
star
50

svelte-dbmonster

Svelte implementation of DBMonster
JavaScript
26
star
51

template-custom-element

Template for building basic applications with Svelte and custom elements
HTML
21
star
52

generate-ssr

Server-side rendering for Svelte
JavaScript
20
star
53

kit-sandbox

A sandbox for maintainers
JavaScript
17
star
54

template-store

Demonstrating the use of svelte/store
JavaScript
17
star
55

svelte-transitions-draw

Draw transition plugin for Svelte
JavaScript
16
star
56

api.svelte.dev

The API worker source for https://api.svelte.dev
TypeScript
14
star
57

sapper-legacy.svelte.dev

Old docs site for Sapper
HTML
13
star
58

action-deploy-docs

github action for the svelte org to deploy documentation to the svelte api
TypeScript
12
star
59

svelte-bench

Benchmarks for Svelte
JavaScript
10
star
60

sveltegram

Sapper/Svelte remix of nextgram
JavaScript
8
star
61

svelte-transitions-fade

Fade transition plugin for Svelte
JavaScript
8
star
62

assets

Large static files used on the Svelte website
7
star
63

svelte-transitions-slide

Slide transition plugin for Svelte
JavaScript
5
star
64

svelte-transitions-fly

Fly transition plugin for Svelte
JavaScript
4
star
65

redirects

Redirect old Svelte websites to their shiny new equivalents
JavaScript
3
star