• Stars
    star
    377
  • Rank 109,422 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

An ESLint plugin for Svelte v3 components.

⚠️ Please use eslint-plugin-svelte instead

This 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.

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

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

svelte-scroller

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

template-webpack

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

sites

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

svelte-repl

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

rfcs

RFCs for changes to Svelte
269
star
25

eslint-plugin-svelte

ESLint plugin for Svelte using AST
TypeScript
242
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