svelte-eslint-parser

Svelte parser for ESLint

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
svelte-eslint-parser
78160.34.111 days ago3 years agoMinified + gzip package size for svelte-eslint-parser in KB

Readme

svelte-eslint-parser
Svelte parser for ESLint.
You can check it on Online DEMO.
Note that this parser has experimental support for Svelte v5, but may break with new versions of Svelte v5.
NPM license
NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status Coverage Status

โคด๏ธ Motivation

The svelte-eslint-parser aims to make it easy to create your own ESLint rules for Svelte.
The eslint-plugin-svelte is an ESLint plugin that uses the svelte-eslint-parser. I have already implemented some rules.

ESLint Plugins Using svelte-eslint-parser

eslint-plugin-svelte

ESLint plugin for Svelte.
It provides many unique check rules by using the template AST.

@intlify/eslint-plugin-svelte

ESLint plugin for internationalization (i18n) with Svelte.
It provides rules to help internationalization your application created with Svelte.

โ— Attention

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

๐Ÿ’ฟ Installation

npm install --save-dev eslint svelte-eslint-parser

๐Ÿ“– Usage

  1. Write overrides.parser option into your .eslintrc.* file.
  2. Use glob patterns or --ext .svelte CLI option.

{
    "extends": "eslint:recommended",
    "overrides": [
        {
            "files": ["*.svelte"],
            "parser": "svelte-eslint-parser"
        }
    ]
}

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

๐Ÿ”ง Options

parserOptions has the same properties as what espree, the default parser of ESLint, is supporting. For example:
{
    "parser": "svelte-eslint-parser",
    "parserOptions": {
        "sourceType": "module",
        "ecmaVersion": 2021,
        "ecmaFeatures": {
            "globalReturn": false,
            "impliedStrict": false,
            "jsx": false
        }
    }
}

parserOptions.parser

You can use parserOptions.parser property to specify a custom parser to parse <script> tags. Other properties than parser would be given to the specified parser. For example:
{
    "parser": "svelte-eslint-parser",
    "parserOptions": {
        "parser": "@typescript-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 = {
  // ...
  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",
      },
    },
    // ...
  ],
  // ...
}

Multiple parsers

If you want to switch the parser for each lang, specify the object.
{
    "parser": "svelte-eslint-parser",
    "parserOptions": {
        "parser": {
            "ts": "@typescript-eslint/parser",
            "js": "espree",
            "typescript": "@typescript-eslint/parser"
        }
    }
}

Parser Object

When using JavaScript configuration (.eslintrc.js), you can also give the parser object directly.
const tsParser = require("@typescript-eslint/parser")
const espree = require("espree")

module.exports = {
    parser: "svelte-eslint-parser",
    parserOptions: {
        // Single parser
        parser: tsParser,
        // Multiple parser
        parser: {
            js: espree,
            ts: tsParser,
        }
    },
}

parserOptions.svelteFeatures

You can use parserOptions.svelteFeatures property to specify how to parse related to Svelte features. For example:
{
    "parser": "svelte-eslint-parser",
    "parserOptions": {
        "svelteFeatures": {

            /* -- Experimental Svelte Features -- */
            /* It may be changed or removed in minor versions without notice. */
            // Whether to parse the `generics` attribute.
            // See https://github.com/sveltejs/rfcs/pull/38
            "experimentalGenerics": false
        }
    }
}

Runes support

This is an experimental feature. It may be changed or removed in minor versions without notice.
If you install Svelte v5 the parser will be able to parse runes, and will also be able to parse *.js and *.ts files.
When using this mode in an ESLint configuration, it is recommended to set it per file pattern as below.
{
    "overrides": [
        {
            "files": ["*.svelte"],
            "parser": "svelte-eslint-parser",
            "parserOptions": {
                "parser": "...",
                ...
            }
        },
        {
            "files": ["*.svelte.js"],
            "parser": "svelte-eslint-parser",
            "parserOptions": {
                ...
            }
        },
        {
            "files": ["*.svelte.ts"],
            "parser": "svelte-eslint-parser",
            "parserOptions": {
                "parser": "...(ts parser)...",
                ...
            }
        }
    ]
}

:computer: 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"
    ]
}

Usage for Custom Rules / Plugins

:beers: Contributing

Welcome contributing!
Please use GitHub's Issues/PRs.
See also the documentation for the internal mechanism.

:lock: License

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