Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-svelte3

sveltejs169.1kMIT4.0.0

An ESLint plugin for Svelte v3 components.

eslint, eslintplugin, svelte, sveltejs

readme

eslint-plugin-svelte3

An ESLint plugin for Svelte v3 components.

Features

  • Compiler errors and warnings are displayed through ESLint
  • Script blocks and template expression tags are linted with existing ESLint rules
  • Svelte scope and stores are respected by unused variable and undefined variable rules
  • Idioms like self-assignment and $: labels are always allowed, regardless of configuration

Requirements

  • Svelte 3.2+
  • ESLint 8+

Installation

Install 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:

module.exports = {
  parserOptions: {
    ecmaVersion: 2019,
    sourceType: 'module'
  },
  env: {
    es6: true,
    browser: true
  },
  plugins: [
    'svelte3'
  ],
  overrides: [
    {
      files: ['*.svelte'],
      processor: 'svelte3/svelte3'
    }
  ],
  rules: {
    // ...
  },
  settings: {
    // ...
  }
};

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.

Installation with TypeScript

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:

module.exports = {
  parser: '@typescript-eslint/parser', // add the TypeScript parser
  plugins: [
    'svelte3',
    '@typescript-eslint' // add the TypeScript plugin
  ],
  overrides: [ // this stays the same
    {
      files: ['*.svelte'],
      processor: 'svelte3/svelte3'
    }
  ],
  rules: {
    // ...
  },
  settings: {
    'svelte3/typescript': () => require('typescript'), // pass the TypeScript package to the Svelte plugin
    // OR
    'svelte3/typescript': true, // load TypeScript as peer dependency
    // ...
  }
};

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.

module.exports = {
  // ...
  parserOptions: { // add these parser options
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json'],
    extraFileExtensions: ['.svelte'],
  },
  extends: [ // then, enable whichever type-aware rules you want to use
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking'
  ],
  // ...
};

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:

<script lang="ts">
  import { writable } from 'svelte/store';

  const store = writable([]);
  $store.length; // incorrect no-unsafe-member-access error

  $: assignment = [];
  assignment.length; // incorrect no-unsafe-member-access error
  // You can work around this by doing
  let another_assignment: string[];
  $: another_assignment = [];
  another_assignment.length; // OK
</script>

Interactions with other plugins

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.

Configuration

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.

Using the CLI

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.

Integrations

See INTEGRATIONS.md for how to use this plugin with your text editor.

License

MIT

changelog

4.0.0

  • Breaking change: ESLint 8+ is now required

3.4.1

  • Support destructuring in {@const} tag

3.4.0

  • Support {@const} tag

3.3.0

  • Add injected globals when there's no instance script when using TypeScript
  • Allow $$Props, $$Slots, $$Events interface/type usage
  • Improve type inference for autosubscribed stores
  • Silence false positives for <svelte:fragment let:x>
  • Use TS 4.5 preserveValueImports if it's available
  • Improve default setting for svelte3/ignore-styles: Now ignores styles if its tag has a lang or type attribute
  • Improve fix range handling

3.2.1

  • Filter no-undef messages about $$Generic

3.2.0

  • Support lazy-loading TypeScript compiler
  • Support non-CommonJS format of ESLint configuration file when using TypeScript by specifying true in configuration
  • Improve logic for finding the correct Linter instance in a workspace with multiple directories
  • Improve filtering of @typescript-eslint/indent and @typescript-eslint/quotes messages like what already happens with indent and quotes
  • Fix erroneous messages when a component only writes to a store

3.1.2

  • Silence some incorrect unsafe-member-access errors - see README for current limitations

3.1.1

  • Fix erroneous errors from two-way binding in TypeScript components
  • Fix erroneous warnings from the removal of imports in TypeScript components

3.1.0

  • Add TypeScript support

3.0.0

  • Breaking change: Node 10+ is now required
    • There are no specific changes yet that will not work on Node 8, but tests will no longer be run on Node 8, and there are no guarantees about it
  • Fix erroneous no-unused-vars for variables that are assigned to in the template, but are only used in the script

2.7.3

  • Fix mishandling of blocks whose last line consists of only the expected indentation

2.7.2

  • Fix regression when using linebreak-style with Windows line endings

2.7.1

  • Named code blocks were in fact a breaking change, sorry!
  • They're now disabled by default, but can be enabled with svelte3/named-blocks

2.7.0

  • Expose the parts of each linted component as separate named code blocks module.js, instance.js, and template.js

2.6.0

  • Add svelte3/compiler setting to allow overriding which instance of the Svelte compiler is used

2.5.0

  • Fix let: handling on regular elements as well
  • Separate then and catch scopes in {#await}

2.4.2

  • Fix handling of the scope created by let: directives

2.4.1

  • Fix attribute parsing edge case in svelte3/ignore-styles callback

2.4.0

  • Respect no-self-assign rule unless self-assignment is to a top-level variable known to the compiler
  • Better handling of identifiers containing unicode characters

2.3.0

  • Respect quotes rule unless inside a template expression which is inside an attribute or directive enclosed in quotes
  • Respect no-unused-expressions rule again (which is now safe thanks to a previous refactor)

2.2.2

  • Stop using inline configuration comments internally, to avoid issues with --no-inline-config and --report-unused-disable-directives

2.2.1

  • Handle then and catch scope in {#await}

2.2.0

  • Enforce semicolon rules in template expressions
  • Fix parsing of template expressions that are object literals
  • Don't produce multiple messages for template expressions wrapped in parentheses

2.1.0

  • Preserve linting messages about labels other than $

2.0.2

  • Actually fix ignoring of rules in the template

2.0.1

  • Disregard eol-last rule
  • Disregard no-unused-expressions rule in the template
  • Fix bug where rules intended to only be ignored in the template were being ignored in the entire file

v2.0.0

  • Require Svelte v3.2+ and ESLint 6+
  • Reworked configuration:
    • svelte3/enabled has been removed in favor of registering a svelte3/svelte3 processor that you need to enable on files
    • svelte3/ignore-warnings now only takes a callback which is passed the warning object
    • svelte3/compiler-options now only takes a compiler options object
    • svelte3/ignore-styles now only takes a preprocessor-style callback
    • svelte3/lint-template has been removed, and template linting is now always enabled

v1.2.3

  • Fix a weird edge case where fixes to problems could be lost in certain cases

v1.2.2

  • Internal improvements to AST walk

v1.2.1

  • Avoid mutating the AST while linting, which can have adverse effects

v1.2.0

  • Pass a second argument to the svelte3/ignore-warnings function that contains the entire warning object
  • Disregard no-labels rule and no-restricted-syntax rule in places where it disallows labels

v1.1.0

  • Experimental support for linting expressions in the template, behind the svelte3/lint-template setting. This feature requires Svelte 3.2.0

v1.0.0

  • Svelte v3 release party!

v0.4.7

  • Fix regression with specifying an array of warnings to ignore

v0.4.6

  • Add svelte3/compiler-options setting to control how the compiler is called during linting

v0.4.5

  • Proper fix for not wiping tag names that begin with <style

v0.4.4

  • With svelte3/ignore-warnings, don't wipe elements whose tag names merely begin with <style
  • The plugin is now published to npm

v0.4.3

  • Better handling for files linted in Windows/CRLF linebreak-style

v0.4.2

  • Work around issues caused by ESLint issues with fixes that replace more of the file than necessary

v0.4.1

  • Make sure fixes for issues at the beginning and end of the scripts do not change text outside of the scripts

v0.4.0

  • Reworked configuration to be more flexible:
    • svelte3/ignore has been renamed to svelte3/ignore-warnings
    • svelte3/extensions has been removed and svelte3/enabled has been added (which works differently but is more powerful)
  • svelte3/ignore-styles has been added as an immediate solution for components with styles written in something other than normal CSS

v0.3.0

  • Support and require at least beta 4

v0.2.3

  • Add svelte3/ignore setting for ignoring specific compiler warnings

v0.2.2

  • Include the position of the end of a compiler error message, when available

v0.2.1

  • Don't warn about export lets with default values and no other reassignments when using prefer-const

v0.2.0

  • Add handling of store auto-subscriptions and other injected variables
  • Require alpha 21

v0.1.0

  • Initial release