npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-eslint-parser

v0.43.0

Published

Svelte parser for ESLint

Downloads

1,683,175

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 parser option into your ESLint Config file.
  2. Use glob patterns or --ext .svelte CLI option.

ESLint Config (eslint.config.js)

import js from "@eslint/js";
import svelteParser from "svelte-eslint-parser";
export default [
  js.configs.recommended,
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      parser: svelteParser,
    },
  },
];

ESLint Config (.eslintrc.*)

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

CLI

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

{
  "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 in eslint.config.js:

import tsParser from "@typescript-eslint/parser";
export default [
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        parser: tsParser,
      },
    },
  },
];

For example in .eslintrc.*:

{
  "parser": "svelte-eslint-parser",
  "parserOptions": {
    "parser": "@typescript-eslint/parser"
  }
}

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.

For example in eslint.config.js:

import tsParser from "@typescript-eslint/parser";
export default [
  // ...
  {
    // ...
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        // ...
        project: "path/to/your/tsconfig.json",
        extraFileExtensions: [".svelte"], // This is a required setting in `@typescript-eslint/parser` v4.24.0.
      },
    },
  },
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      parser: svelteParser,
      // Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
      parserOptions: {
        parser: tsParser,
      },
    },
    // ...
  },
];

For example in .eslintrc.*:

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.

For example in eslint.config.js:

import tsParser from "@typescript-eslint/parser";
import espree from "espree";
export default [
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        parser: {
          ts: tsParser,
          js: espree,
          typescript: tsParser,
        },
      },
    },
  },
];

For example in .eslintrc.*:

{
  "parser": "svelte-eslint-parser",
  "parserOptions": {
    "parser": {
      "ts": "@typescript-eslint/parser",
      "js": "espree",
      "typescript": "@typescript-eslint/parser"
    }
  }
}

parserOptions.svelteConfig

If you are using eslint.config.js, you can provide a svelte.config.js in the parserOptions.svelteConfig property.

For example:

import svelteConfig from "./svelte.config.js";
export default [
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        svelteConfig: svelteConfig,
      },
    },
  },
];

If parserOptions.svelteConfig is not specified, some config will be statically parsed from the svelte.config.js file.

The .eslintrc.* style configuration cannot load svelte.config.js because it cannot use ESM. We recommend using the eslint.config.js style configuration.

parserOptions.svelteFeatures

You can use parserOptions.svelteFeatures property to specify how to parse related to Svelte features.

For example in eslint.config.js:

export default [
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        svelteFeatures: {
          /* -- Experimental Svelte Features -- */
          /* It may be changed or removed in minor versions without notice. */
          // This option is for Svelte 5. The default value is `true`.
          // If `false`, ESLint will not recognize rune symbols.
          // If not configured this option, The parser will try to read the option from `compilerOptions.runes` from `svelte.config.js`.
          // If `parserOptions.svelteConfig` is not specified and the file cannot be parsed by static analysis, it will behave as `true`.
          runes: true,
          /* -- 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,
        },
      },
    },
  },
];

For example in .eslintrc.*:

{
  "parser": "svelte-eslint-parser",
  "parserOptions": {
    "svelteFeatures": {
      /* -- Experimental Svelte Features -- */
      /* It may be changed or removed in minor versions without notice. */
      // This option is for Svelte 5. The default value is `true`.
      // If `false`, ESLint will not recognize rune symbols.
      // If not configured this option, The parser will try to read the option from `compilerOptions.runes` from `svelte.config.js`.
      // If `parserOptions.svelteConfig` is not specified and the file cannot be parsed by static analysis, it will behave as `true`.
      "runes": true,
      /* -- 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. If you don't want to use Runes, you may need to configure. Please read parserOptions.svelteFeatures for more details.

When using this mode in an ESLint configuration, it is recommended to set it per file pattern as below.

For example in eslint.config.js:

import svelteConfig from "./svelte.config.js";
export default [
  {
    files: ["**/*.svelte", "*.svelte"],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        parser: "...",
        svelteConfig,
        /* ... */
      },
    },
  },
  {
    files: ["**/*.svelte.js", "*.svelte.js"],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        svelteConfig,
        /* ... */
      },
    },
  },
  {
    files: ["**/*.svelte.ts", "*.svelte.ts"],
    languageOptions: {
      parser: svelteParser,
      parserOptions: {
        parser: "...(ts parser)...",
        svelteConfig,
        /* ... */
      },
    },
  },
];

For example in .eslintrc.*:

{
  "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).