This commit is contained in:
2026-11-02 12:42:04 +00:00
commit e4e28174bf
5498 changed files with 1735888 additions and 0 deletions

21
node_modules/eslint-plugin-svelte/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Yosuke Ota
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

474
node_modules/eslint-plugin-svelte/README.md generated vendored Normal file
View File

@@ -0,0 +1,474 @@
<!--DOCS_IGNORE_START-->
[![NPM license](https://img.shields.io/npm/l/eslint-plugin-svelte.svg)](https://www.npmjs.com/package/eslint-plugin-svelte)
[![NPM version](https://img.shields.io/npm/v/eslint-plugin-svelte.svg)](https://www.npmjs.com/package/eslint-plugin-svelte)
[![NPM downloads](https://img.shields.io/badge/dynamic/json.svg?label=downloads&colorB=green&suffix=/day&query=$.downloads&uri=https://api.npmjs.org//downloads/point/last-day/eslint-plugin-svelte&maxAge=3600)](http://www.npmtrends.com/eslint-plugin-svelte)
[![NPM downloads](https://img.shields.io/npm/dw/eslint-plugin-svelte.svg)](http://www.npmtrends.com/eslint-plugin-svelte)
[![NPM downloads](https://img.shields.io/npm/dm/eslint-plugin-svelte.svg)](http://www.npmtrends.com/eslint-plugin-svelte)
[![NPM downloads](https://img.shields.io/npm/dy/eslint-plugin-svelte.svg)](http://www.npmtrends.com/eslint-plugin-svelte)
[![NPM downloads](https://img.shields.io/npm/dt/eslint-plugin-svelte.svg)](http://www.npmtrends.com/eslint-plugin-svelte)
[![Build Status](https://github.com/sveltejs/eslint-plugin-svelte/workflows/CI/badge.svg?branch=main)](https://github.com/sveltejs/eslint-plugin-svelte/actions?query=workflow%3ACI)
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fsveltejs%2Feslint-plugin-svelte%2Fmain%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
[![Conventional Commits](https://img.shields.io/badge/conventional%20commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![changesets](https://img.shields.io/badge/maintained%20with-changesets-176de3.svg)](https://github.com/atlassian/changesets)
<div align="center">
# eslint-plugin-svelte
## ESLint plugin for Svelte using AST
[Live Demo](https://eslint-online-playground.netlify.app/#eslint-plugin-svelte%20with%20typescript) •
[Documentation](https://sveltejs.github.io/eslint-plugin-svelte/) •
[Discord](https://svelte.dev/chat)
</div>
<!--DOCS_IGNORE_END-->
## Introduction
`eslint-plugin-svelte` is the official [ESLint](https://eslint.org/) plugin for [Svelte](https://svelte.dev/).\
It leverages the AST generated by [svelte-eslint-parser](https://github.com/sveltejs/svelte-eslint-parser) to provide custom linting for Svelte.\
> [!NOTE]
>
> `eslint-plugin-svelte` and `svelte-eslint-parser` cannot be used alongside [eslint-plugin-svelte3](https://github.com/sveltejs/eslint-plugin-svelte3).
<!--USAGE_SECTION_START-->
<!--USAGE_GUIDE_START-->
## Installation
### CLI
The recommended way to get started is to use the CLI.
```sh
# new project
npx sv create
# existing project
npx sv add eslint
```
See the [CLI docs](https://svelte.dev/docs/cli/eslint) for more details.
### Manual Setup
```sh
npm install --save-dev svelte eslint eslint-plugin-svelte globals
```
> [!NOTE]
>
> **Requirements:**
>
> - ESLint v8.57.1, v9.0.0, and above
> - Node.js v18.18.0, v20.9.0, v21.1.0 and above
## Usage
Use `eslint.config.js` to configure rules. See [ESLint documentation](https://eslint.org/docs/latest/use/configure/configuration-files-new) for more details.
### Configuration
#### JavaScript project
```js
// eslint.config.js
import svelteConfig from './svelte.config.js';
import { defineConfig } from 'eslint/config';
import globals from 'globals';
import js from '@eslint/js';
import svelte from 'eslint-plugin-svelte';
export default defineConfig([
// ...
js.configs.recommended,
svelte.configs.recommended,
{
languageOptions: {
globals: {
...globals.browser,
// for Sveltekit in non-SPA mode
...globals.node
}
}
},
{
files: ['**/*.svelte', '**/*.svelte.js'],
languageOptions: {
parserOptions: {
// explicitly importing allows for better compatibilty and functionality with rules and other tooling that depend on the config file.
//
// Note: `eslint --cache` will fail with non-serializable properties.
// In those cases, please remove the non-serializable properties.
// svelteConfig: {
// ...svelteConfig,
// kit: {
// ...svelteConfig.kit,
// typescript: undefined
// }
// }
svelteConfig
}
}
},
{
rules: {
// Override or add rule settings here, such as:
// 'svelte/rule-name': 'error'
}
}
]);
```
#### TypeScript project
```shell
npm install --save-dev typescript-eslint
```
```js
// eslint.config.js
import svelteConfig from './svelte.config.js';
import { defineConfig } from 'eslint/config';
import globals from 'globals';
import js from '@eslint/js';
import ts from 'typescript-eslint';
import svelte from 'eslint-plugin-svelte';
export default defineConfig(
js.configs.recommended,
ts.configs.recommended,
svelte.configs.recommended,
{
languageOptions: {
parserOptions: {
// Keep this consistent for TypeScript and Svelte files to avoid project reloads.
extraFileExtensions: ['.svelte']
},
globals: {
...globals.browser,
// for Sveltekit in non-SPA mode
...globals.node
}
}
// ...
},
{
files: ['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js'],
// See more details at: https://typescript-eslint.io/packages/parser/
languageOptions: {
parserOptions: {
projectService: true,
// Specify a parser for each language, if needed:
// parser: {
// ts: ts.parser,
// typescript: ts.parser
// js: espree, // add `import espree from 'espree'`
// },
parser: ts.parser,
// explicitly importing allows for better compatibilty and functionality with rules and other tooling that depend on the config file.
//
// Note: `eslint --cache` will fail with non-serializable properties.
// In those cases, please remove the non-serializable properties.
// svelteConfig: {
// ...svelteConfig,
// kit: {
// ...svelteConfig.kit,
// typescript: undefined
// }
// }
svelteConfig
}
}
},
{
rules: {
// Override or add rule settings here, such as:
// 'svelte/rule-name': 'error'
}
}
);
```
> [!WARNING]
> The TypeScript parser uses a singleton internally, meaning it only respects the options provided during its initial initialization.
> If you try to change the options for a different file or override them later, the parser will ignore the new options, which may lead to errors.
> For more context, see [typescript-eslint/typescript-eslint#6778](https://github.com/typescript-eslint/typescript-eslint/issues/6778).
#### Svelte config inside `vite.config.js`
The Svelte config can also be passed to the `sveltekit()` or `svelte()` plugin in `vite.config.js`; SvelteKit 3 projects do this by default. Use [`@sveltejs/load-config`](https://www.npmjs.com/package/@sveltejs/load-config) to load it:
```js
// eslint.config.js
import { loadConfig } from '@sveltejs/load-config';
const svelteConfig = (await loadConfig('./', { traverse: false }))?.config;
```
### Available Configurations
This plugin provides the following configurations:
- **`svelte.configs.base`** - **Required** for Svelte parsing. Does not include any rules. Ideal for building a custom configurations.
- **`svelte.configs.recommended`** - Extends `base` and includes rules for best practices.
- **`svelte.configs.prettier`** - Extends `base` and disables rules that may conflict with [Prettier](https://prettier.io/). Prettier still needs to be configured to work with Svelte, for example, by using [prettier-plugin-svelte](https://github.com/sveltejs/prettier-plugin-svelte).
- **`svelte.configs.all`** - **Not Recommended** - Extends `base` and includes all rules. Subject to change with every major and minor release. Use at your own risk.
For more details, see [the rule list](https://sveltejs.github.io/eslint-plugin-svelte/rules/) to explore the rules provided by this plugin.
### settings.svelte
You can customize the behavior of this plugin using specific settings.
```js
// eslint.config.js
export default defineConfig([
// ...
{
settings: {
svelte: {
// Specifies an array of rules to ignore reports within the template.
// For example, use this to disable rules in the template that may produce unavoidable false positives.
ignoreWarnings: [
'@typescript-eslint/no-unsafe-assignment',
'@typescript-eslint/no-unsafe-member-access'
],
// Specifies options for Svelte compilation.
// This affects rules that rely on Svelte compilation,
// such as `svelte/valid-compile` and `svelte/no-unused-svelte-ignore`.
// Note that this setting does not impact ESLint’s custom parser.
compileOptions: {
// Specifies options related to PostCSS. You can disable the PostCSS processing by setting it to false.
postcss: {
// Specifies the path to the directory that contains the PostCSS configuration.
configFilePath: './path/to/my/postcss.config.js'
}
},
// Even if settings.svelte.kit is not specified, the rules will attempt to load information from svelte.config.js.
// However, if the default behavior does not work as expected, you should specify settings.svelte.kit explicitly.
// If you are using SvelteKit with a non-default configuration, you need to set the following options.
// The schema is a subset of SvelteKit’s configuration, so refer to the SvelteKit documentation for more details.
// https://svelte.dev/docs/kit/configuration
kit: {
files: {
routes: 'src/routes'
}
}
}
}
}
// ...
]);
```
SvelteKit versions are detected from the installed `@sveltejs/kit` package, falling back to its dependency range in `package.json`. The plugin recognizes SvelteKit 1, 2, and 3, including SvelteKit 3 prereleases. Version-specific rules run only for the versions they support.
For SvelteKit 3 projects using a custom routes directory, set `settings.svelte.kit.files.routes` to match the `files.routes` option passed to the `sveltekit` Vite plugin. SvelteKit 3 no longer uses `svelte.config.js`; see the [official migration guide](https://svelte.dev/docs/kit/migrating-to-sveltekit-3#Configuration).
## Editor Integrations
**Visual Studio Code**\
Install [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).\
Configure `.svelte` files in `.vscode/settings.json`:
<!--USAGE_GUIDE_END-->
<!--USAGE_SECTION_END-->
## Migration Guide
If you’re migrating from `eslint-plugin-svelte@1` or [`@ota-meshi/eslint-plugin-svelte`](https://www.npmjs.com/package/@ota-meshi/eslint-plugin-svelte), see the [migration guide](https://sveltejs.github.io/eslint-plugin-svelte/migration/).
## Versioning Policy
This project follows [Semantic Versioning](https://semver.org/). Unlike [ESLint’s versioning policy](https://github.com/eslint/eslint#semantic-versioning-policy), new rules may be added to the recommended config in minor releases. If these rules cause unwanted warnings, you can disable them.
<!--DOCS_IGNORE_END-->
## Rules
<!-- prettier-ignore-start -->
<!--RULES_SECTION_START-->
:wrench: Indicates that the rule is fixable, and using `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the reported problems.\
:bulb: Indicates that some problems reported by the rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).\
:star: Indicates that the rule is included in the `plugin:svelte/recommended` config.
<!--RULES_TABLE_START-->
## Possible Errors
These rules relate to possible syntax or logic errors in Svelte code:
| Rule ID | Description | |
|:--------|:------------|:---|
| [svelte/infinite-reactive-loop](https://sveltejs.github.io/eslint-plugin-svelte/rules/infinite-reactive-loop/) | Svelte runtime prevents calling the same reactive statement twice in a microtask. But between different microtask, it doesn't prevent. | :star: |
| [svelte/no-bind-value-on-checkable-inputs](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-bind-value-on-checkable-inputs/) | disallow useless `bind:value` on `<input type="checkbox">` and `<input type="radio">` | :bulb: |
| [svelte/no-conflicting-module-names](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-conflicting-module-names/) | disallow a `.svelte` component and a same-named runes module (e.g. `Foo.svelte` and `Foo.svelte.ts`) from coexisting | |
| [svelte/no-dom-manipulating](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dom-manipulating/) | disallow DOM manipulating | :star: |
| [svelte/no-dupe-else-if-blocks](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-else-if-blocks/) | disallow duplicate conditions in `{#if}` / `{:else if}` chains | :star: |
| [svelte/no-dupe-on-directives](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-on-directives/) | disallow duplicate `on:` directives | :star: |
| [svelte/no-dupe-style-properties](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-style-properties/) | disallow duplicate style properties | :star: |
| [svelte/no-dupe-use-directives](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-use-directives/) | disallow duplicate `use:` directives | :star: |
| [svelte/no-nested-style-tag](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-nested-style-tag/) | disallow `<style>` elements nested inside other elements or blocks | |
| [svelte/no-not-function-handler](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-not-function-handler/) | disallow use of not function in event handler | :star: |
| [svelte/no-object-in-text-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-object-in-text-mustaches/) | disallow objects in text mustache interpolation | :star: |
| [svelte/no-raw-special-elements](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-raw-special-elements/) | Checks for invalid raw HTML elements | :star::wrench: |
| [svelte/no-reactive-reassign](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-reassign/) | disallow reassigning reactive values | :star: |
| [svelte/no-shorthand-style-property-overrides](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-shorthand-style-property-overrides/) | disallow shorthand style properties that override related longhand properties | :star: |
| [svelte/no-store-async](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-store-async/) | disallow using async/await inside svelte stores because it causes issues with the auto-unsubscribing features | :star: |
| [svelte/no-top-level-browser-globals](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-top-level-browser-globals/) | disallow using top-level browser global variables | |
| [svelte/no-unknown-style-directive-property](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unknown-style-directive-property/) | disallow unknown `style:property` | :star: |
| [svelte/prefer-svelte-reactivity](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-svelte-reactivity/) | disallow using mutable instances of built-in classes where a reactive alternative is provided by svelte/reactivity | :star: |
| [svelte/require-store-callbacks-use-set-param](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-store-callbacks-use-set-param/) | store callbacks must use `set` param | :bulb: |
| [svelte/require-store-reactive-access](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-store-reactive-access/) | disallow to use of the store itself as an operand. Need to use $ prefix or get function. | :star::wrench: |
| [svelte/valid-compile](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-compile/) | disallow warnings when compiling. | |
| [svelte/valid-style-parse](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-style-parse/) | require valid style element parsing | |
## Security Vulnerability
These rules relate to security vulnerabilities in Svelte code:
| Rule ID | Description | |
|:--------|:------------|:---|
| [svelte/no-at-html-tags](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-at-html-tags/) | disallow use of `{@html}` to prevent XSS attack | :star: |
| [svelte/no-target-blank](https://sveltejs.github.io/eslint-plugin-svelte/rules/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](https://sveltejs.github.io/eslint-plugin-svelte/rules/block-lang/) | disallows the use of languages other than those specified in the configuration for the lang attribute of `<script>` and `<style>` blocks. | :bulb: |
| [svelte/button-has-type](https://sveltejs.github.io/eslint-plugin-svelte/rules/button-has-type/) | disallow usage of button without an explicit type attribute | |
| [svelte/no-add-event-listener](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-add-event-listener/) | Warns against the use of `addEventListener` | :bulb: |
| [svelte/no-at-const-tags](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-at-const-tags/) | disallow the use of `{@const}` in favor of `{const ...}` declaration tags | :wrench: |
| [svelte/no-at-debug-tags](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-at-debug-tags/) | disallow the use of `{@debug}` | :star::bulb: |
| [svelte/no-ignored-unsubscribe](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-ignored-unsubscribe/) | disallow ignoring the unsubscribe method returned by the `subscribe()` on Svelte stores. | |
| [svelte/no-immutable-reactive-statements](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-immutable-reactive-statements/) | disallow reactive statements that don't reference reactive values. | :star: |
| [svelte/no-inline-styles](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inline-styles/) | disallow attributes and directives that produce inline styles | |
| [svelte/no-inspect](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inspect/) | Warns against the use of `$inspect` directive | :star: |
| [svelte/no-reactive-functions](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-functions/) | it's not necessary to define functions in reactive statements | :star::bulb: |
| [svelte/no-reactive-literals](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-literals/) | don't assign literal values in reactive statements | :star::bulb: |
| [svelte/no-svelte-internal](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-svelte-internal/) | svelte/internal will be removed in Svelte 6. | :star: |
| [svelte/no-unnecessary-state-wrap](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unnecessary-state-wrap/) | Disallow unnecessary $state wrapping of reactive classes | :star::bulb: |
| [svelte/no-unused-class-name](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-class-name/) | disallow the use of a class in the template without a corresponding style | |
| [svelte/no-unused-props](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-props/) | Warns about defined Props properties that are unused | :star: |
| [svelte/no-unused-svelte-ignore](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/) | disallow unused svelte-ignore comments | :star: |
| [svelte/no-useless-children-snippet](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-children-snippet/) | disallow explicit children snippet where it's not needed | :star: |
| [svelte/no-useless-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/) | disallow unnecessary mustache interpolations | :star::wrench: |
| [svelte/prefer-const](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/) | Require `const` declarations for variables that are never reassigned after declared | :wrench: |
| [svelte/prefer-derived-over-derived-by](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-derived-over-derived-by/) | disallow unnecessary `$derived.by()` when `$derived()` is sufficient | :wrench: |
| [svelte/prefer-destructured-store-props](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
| [svelte/prefer-writable-derived](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-writable-derived/) | Prefer using writable $derived instead of $state and $effect | :star::bulb: |
| [svelte/require-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-each-key/) | require keyed `{#each}` block | :star: |
| [svelte/require-event-dispatcher-types](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-event-dispatcher-types/) | require type parameters for `createEventDispatcher` | :star: |
| [svelte/require-optimized-style-attribute](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-optimized-style-attribute/) | require style attributes that can be optimized | |
| [svelte/require-stores-init](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-stores-init/) | require initial value in store | |
| [svelte/valid-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-each-key/) | enforce keys to use variables defined in the `{#each}` block | :star: |
## Stylistic Issues
These rules relate to style guidelines, and are therefore quite subjective:
| Rule ID | Description | |
|:--------|:------------|:---|
| [svelte/consistent-selector-style](https://sveltejs.github.io/eslint-plugin-svelte/rules/consistent-selector-style/) | enforce a consistent style for CSS selectors | |
| [svelte/derived-has-same-inputs-outputs](https://sveltejs.github.io/eslint-plugin-svelte/rules/derived-has-same-inputs-outputs/) | derived store should use same variable names between values and callback | :bulb: |
| [svelte/first-attribute-linebreak](https://sveltejs.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/) | enforce the location of first attribute | :wrench: |
| [svelte/html-closing-bracket-new-line](https://sveltejs.github.io/eslint-plugin-svelte/rules/html-closing-bracket-new-line/) | Require or disallow a line break before tag's closing brackets | :wrench: |
| [svelte/html-closing-bracket-spacing](https://sveltejs.github.io/eslint-plugin-svelte/rules/html-closing-bracket-spacing/) | require or disallow a space before tag's closing brackets | :wrench: |
| [svelte/html-quotes](https://sveltejs.github.io/eslint-plugin-svelte/rules/html-quotes/) | enforce quotes style of HTML attributes | :wrench: |
| [svelte/html-self-closing](https://sveltejs.github.io/eslint-plugin-svelte/rules/html-self-closing/) | enforce self-closing style | :wrench: |
| [svelte/indent](https://sveltejs.github.io/eslint-plugin-svelte/rules/indent/) | enforce consistent indentation | :wrench: |
| [svelte/max-attributes-per-line](https://sveltejs.github.io/eslint-plugin-svelte/rules/max-attributes-per-line/) | enforce the maximum number of attributes per line | :wrench: |
| [svelte/max-lines-per-block](https://sveltejs.github.io/eslint-plugin-svelte/rules/max-lines-per-block/) | enforce maximum number of lines in svelte component blocks | |
| [svelte/mustache-spacing](https://sveltejs.github.io/eslint-plugin-svelte/rules/mustache-spacing/) | enforce unified spacing in mustache | :wrench: |
| [svelte/no-extra-reactive-curlies](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-extra-reactive-curlies/) | disallow wrapping single reactive statements in curly braces | :bulb: |
| [svelte/no-restricted-html-elements](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-restricted-html-elements/) | disallow specific HTML elements | |
| [svelte/no-spaces-around-equal-signs-in-attribute](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-spaces-around-equal-signs-in-attribute/) | disallow spaces around equal signs in attribute | :wrench: |
| [svelte/prefer-attribute-interpolation](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-attribute-interpolation/) | require attribute interpolation instead of template literals | |
| [svelte/prefer-class-directive](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-class-directive/) | require class directives instead of ternary expressions | :wrench: |
| [svelte/prefer-style-directive](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-style-directive/) | require style directives instead of style attribute | :wrench: |
| [svelte/require-event-prefix](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-event-prefix/) | require component event names to start with "on" | |
| [svelte/shorthand-attribute](https://sveltejs.github.io/eslint-plugin-svelte/rules/shorthand-attribute/) | enforce use of shorthand syntax in attribute | :wrench: |
| [svelte/shorthand-directive](https://sveltejs.github.io/eslint-plugin-svelte/rules/shorthand-directive/) | enforce use of shorthand syntax in directives | :wrench: |
| [svelte/sort-attributes](https://sveltejs.github.io/eslint-plugin-svelte/rules/sort-attributes/) | enforce order of attributes | :wrench: |
| [svelte/spaced-html-comment](https://sveltejs.github.io/eslint-plugin-svelte/rules/spaced-html-comment/) | enforce consistent spacing after the `<!--` and before the `-->` in a HTML comment | :wrench: |
## 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](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inner-declarations/) | disallow variable or `function` declarations in nested blocks | :star: |
| [svelte/no-trailing-spaces](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-trailing-spaces/) | disallow trailing whitespace at the end of lines | :wrench: |
| [svelte/prefer-const](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/) | Require `const` declarations for variables that are never reassigned after declared | :wrench: |
## SvelteKit
These rules relate to SvelteKit and its best Practices.
| Rule ID | Description | |
|:--------|:------------|:---|
| [svelte/no-export-load-in-svelte-module-in-kit-pages](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-export-load-in-svelte-module-in-kit-pages/) | disallow exporting load functions in `*.svelte` module in SvelteKit page components. | :star: |
| [svelte/no-navigation-without-resolve](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/) | disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()` | :star: |
| [svelte/valid-prop-names-in-kit-pages](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-prop-names-in-kit-pages/) | disallow invalid props in SvelteKit route components. | :star: |
## Experimental
:warning: These rules are considered experimental and may change or be removed in the future:
| Rule ID | Description | |
|:--------|:------------|:---|
| [svelte/experimental-require-slot-types](https://sveltejs.github.io/eslint-plugin-svelte/rules/experimental-require-slot-types/) | require slot type declaration using the `$$Slots` interface | |
| [svelte/experimental-require-strict-events](https://sveltejs.github.io/eslint-plugin-svelte/rules/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](https://sveltejs.github.io/eslint-plugin-svelte/rules/comment-directive/) | support comment-directives in HTML template | :star: |
| [svelte/system](https://sveltejs.github.io/eslint-plugin-svelte/rules/system/) | system rule for working this plugin | :star: |
## Deprecated
- :warning: We're going to remove deprecated rules in the next major release. Please migrate to successor/new rules.
- :innocent: 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](https://sveltejs.github.io/eslint-plugin-svelte/rules/@typescript-eslint/no-unnecessary-condition/) | This rule is no longer needed when using svelte-eslint-parser>=v0.19.0. |
| [svelte/no-dynamic-slot-name](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dynamic-slot-name/) | Now Svelte compiler itself throws an compile error. |
| [svelte/no-goto-without-base](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-goto-without-base/) | [svelte/no-navigation-without-resolve](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/) |
| [svelte/no-navigation-without-base](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-base/) | [svelte/no-navigation-without-resolve](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/) |
<!--RULES_TABLE_END-->
<!--RULES_SECTION_END-->
<!-- prettier-ignore-end -->
<!--DOCS_IGNORE_START-->
## Contributing
Contributions are welcome! Please open an issue or submit a PR. For more details, see [CONTRIBUTING.md](./CONTRIBUTING.md).\
Refer to [svelte-eslint-parser](https://github.com/sveltejs/svelte-eslint-parser) for AST details.
<!--DOCS_IGNORE_END-->
## License
See [LICENSE](LICENSE) (MIT).

View File

@@ -0,0 +1,3 @@
import type { Linter } from 'eslint';
declare const config: Linter.Config[];
export default config;

View File

@@ -0,0 +1,16 @@
import { rules } from '../../utils/rules.js';
import base from './base.js';
const config = [
...base,
{
name: 'svelte:all:rules',
rules: Object.fromEntries(rules
.filter((rule) => !rule.meta.deprecated)
.map((rule) => [`svelte/${rule.meta.docs.ruleName}`, 'error'])
.filter(([ruleName]) => ![
// Does not work without options.
'svelte/no-restricted-html-elements'
].includes(ruleName)))
}
];
export default config;

View File

@@ -0,0 +1,4 @@
import type { ESLint, Linter } from 'eslint';
export declare function setPluginObject(plugin: ESLint.Plugin): void;
declare const config: Linter.Config[];
export default config;

View File

@@ -0,0 +1,44 @@
import * as parser from 'svelte-eslint-parser';
let pluginObject = null;
export function setPluginObject(plugin) {
pluginObject = plugin;
}
const config = [
{
name: 'svelte:base:setup-plugin',
plugins: {
get svelte() {
return pluginObject;
}
}
},
{
name: 'svelte:base:setup-for-svelte',
files: ['*.svelte', '**/*.svelte'],
languageOptions: {
parser
},
rules: {
// ESLint core rules known to cause problems with `.svelte`.
'no-inner-declarations': 'off', // The AST generated by svelte-eslint-parser will false positives in it rule because the root node of the script is not the `Program`.
// "no-irregular-whitespace": "off",
// Self assign is one of way to update reactive value in Svelte.
'no-self-assign': 'off',
// eslint-plugin-svelte rules
'svelte/comment-directive': 'error',
'svelte/system': 'error'
},
processor: 'svelte/svelte'
},
{
name: 'svelte:base:setup-for-svelte-script',
files: ['*.svelte.js', '*.svelte.ts', '**/*.svelte.js', '**/*.svelte.ts'],
languageOptions: {
parser
},
rules: {
// eslint-plugin-svelte rules
}
}
];
export default config;

View File

@@ -0,0 +1,3 @@
import type { Linter } from 'eslint';
declare const config: Linter.Config[];
export default config;

View File

@@ -0,0 +1,23 @@
import base from './base.js';
const config = [
...base,
{
name: 'svelte:prettier:turn-off-rules',
rules: {
// eslint-plugin-svelte rules
'svelte/first-attribute-linebreak': 'off',
'svelte/html-closing-bracket-new-line': 'off',
'svelte/html-closing-bracket-spacing': 'off',
'svelte/html-quotes': 'off',
'svelte/html-self-closing': 'off',
'svelte/indent': 'off',
'svelte/max-attributes-per-line': 'off',
'svelte/mustache-spacing': 'off',
'svelte/no-spaces-around-equal-signs-in-attribute': 'off',
'svelte/no-trailing-spaces': 'off',
'svelte/shorthand-attribute': 'off',
'svelte/shorthand-directive': 'off'
}
}
];
export default config;

View File

@@ -0,0 +1,3 @@
import type { Linter } from 'eslint';
declare const config: Linter.Config[];
export default config;

View File

@@ -0,0 +1,48 @@
import base from './base.js';
const config = [
...base,
{
name: 'svelte:recommended:rules',
rules: {
// eslint-plugin-svelte rules
'svelte/comment-directive': 'error',
'svelte/infinite-reactive-loop': 'error',
'svelte/no-at-debug-tags': 'warn',
'svelte/no-at-html-tags': 'error',
'svelte/no-dom-manipulating': 'error',
'svelte/no-dupe-else-if-blocks': 'error',
'svelte/no-dupe-on-directives': 'error',
'svelte/no-dupe-style-properties': 'error',
'svelte/no-dupe-use-directives': 'error',
'svelte/no-export-load-in-svelte-module-in-kit-pages': 'error',
'svelte/no-immutable-reactive-statements': 'error',
'svelte/no-inner-declarations': 'error',
'svelte/no-inspect': 'warn',
'svelte/no-navigation-without-resolve': 'error',
'svelte/no-not-function-handler': 'error',
'svelte/no-object-in-text-mustaches': 'error',
'svelte/no-raw-special-elements': 'error',
'svelte/no-reactive-functions': 'error',
'svelte/no-reactive-literals': 'error',
'svelte/no-reactive-reassign': 'error',
'svelte/no-shorthand-style-property-overrides': 'error',
'svelte/no-store-async': 'error',
'svelte/no-svelte-internal': 'error',
'svelte/no-unknown-style-directive-property': 'error',
'svelte/no-unnecessary-state-wrap': 'error',
'svelte/no-unused-props': 'error',
'svelte/no-unused-svelte-ignore': 'error',
'svelte/no-useless-children-snippet': 'error',
'svelte/no-useless-mustaches': 'error',
'svelte/prefer-svelte-reactivity': 'error',
'svelte/prefer-writable-derived': 'error',
'svelte/require-each-key': 'error',
'svelte/require-event-dispatcher-types': 'error',
'svelte/require-store-reactive-access': 'error',
'svelte/system': 'error',
'svelte/valid-each-key': 'error',
'svelte/valid-prop-names-in-kit-pages': 'error'
}
}
];
export default config;

3
node_modules/eslint-plugin-svelte/lib/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,3 @@
import * as plugin from './main.js';
export * from './main.js';
export default plugin;

5
node_modules/eslint-plugin-svelte/lib/index.js generated vendored Normal file
View File

@@ -0,0 +1,5 @@
import * as plugin from './main.js';
import { setPluginObject } from './configs/flat/base.js';
setPluginObject(plugin);
export * from './main.js';
export default plugin;

22
node_modules/eslint-plugin-svelte/lib/main.d.ts generated vendored Normal file
View File

@@ -0,0 +1,22 @@
import './rule-types.js';
import * as processor from './processor/index.js';
import type { Linter, Rule } from 'eslint';
export declare const configs: {
base: Linter.Config[];
recommended: Linter.Config[];
prettier: Linter.Config[];
all: Linter.Config[];
'flat/base': Linter.Config[];
'flat/recommended': Linter.Config[];
'flat/prettier': Linter.Config[];
'flat/all': Linter.Config[];
};
export declare const rules: Record<string, Rule.RuleModule>;
export declare const meta: {
name: "eslint-plugin-svelte";
version: "3.23.1";
};
export declare const processors: {
'.svelte': typeof processor;
svelte: typeof processor;
};

30
node_modules/eslint-plugin-svelte/lib/main.js generated vendored Normal file
View File

@@ -0,0 +1,30 @@
import './rule-types.js';
import { rules as ruleList } from './utils/rules.js';
import base from './configs/flat/base.js';
import recommended from './configs/flat/recommended.js';
import prettier from './configs/flat/prettier.js';
import all from './configs/flat/all.js';
import * as processor from './processor/index.js';
import * as metaModule from './meta.js';
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion -- ts(2742) Error */
export const configs = {
base: base,
recommended: recommended,
prettier: prettier,
all: all,
// For backward compatibility
'flat/base': base,
'flat/recommended': recommended,
'flat/prettier': prettier,
'flat/all': all
};
/* eslint-enable @typescript-eslint/no-unnecessary-type-assertion -- ts(2742) Error */
export const rules = ruleList.reduce((obj, r) => {
obj[r.meta.docs.ruleName] = r;
return obj;
}, {});
export const meta = { ...metaModule };
export const processors = {
'.svelte': processor,
svelte: processor
};

2
node_modules/eslint-plugin-svelte/lib/meta.d.ts generated vendored Normal file
View File

@@ -0,0 +1,2 @@
export declare const name: "eslint-plugin-svelte";
export declare const version: "3.23.1";

5
node_modules/eslint-plugin-svelte/lib/meta.js generated vendored Normal file
View File

@@ -0,0 +1,5 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
export const name = 'eslint-plugin-svelte';
export const version = '3.23.1';

View File

@@ -0,0 +1,7 @@
import type { Linter } from 'eslint';
export * as meta from '../meta.js';
/** preprocess */
export declare function preprocess(code: string, filename: string): string[];
/** postprocess */
export declare function postprocess([messages]: Linter.LintMessage[][], filename: string): Linter.LintMessage[];
export declare const supportsAutofix = true;

View File

@@ -0,0 +1,29 @@
import { beginShared, terminateShared } from '../shared/index.js';
export * as meta from '../meta.js';
/** preprocess */
export function preprocess(code, filename) {
if (filename) {
beginShared(filename);
}
return [code];
}
/** postprocess */
export function postprocess([messages], filename) {
const shared = terminateShared(filename);
if (shared) {
return filter(messages, shared);
}
return messages;
}
export const supportsAutofix = true;
/** Filter */
function filter(messages, shared) {
if (shared.commentDirectives.length === 0) {
return messages;
}
let filteredMessages = messages;
for (const cd of shared.commentDirectives) {
filteredMessages = cd.filterMessages(filteredMessages);
}
return filteredMessages;
}

683
node_modules/eslint-plugin-svelte/lib/rule-types.d.ts generated vendored Normal file
View File

@@ -0,0 +1,683 @@
import type { Linter } from 'eslint';
declare module 'eslint' {
interface RulesConfig extends RuleOptions {
}
}
export interface RuleOptions {
/**
* disallow conditionals where the type is always truthy or always falsy
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/@typescript-eslint/no-unnecessary-condition/
* @deprecated
*/
'svelte/@typescript-eslint/no-unnecessary-condition'?: Linter.RuleEntry<SvelteTypescriptEslintNoUnnecessaryCondition>;
/**
* disallows the use of languages other than those specified in the configuration for the lang attribute of `<script>` and `<style>` blocks.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/block-lang/
*/
'svelte/block-lang'?: Linter.RuleEntry<SvelteBlockLang>;
/**
* disallow usage of button without an explicit type attribute
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/button-has-type/
*/
'svelte/button-has-type'?: Linter.RuleEntry<SvelteButtonHasType>;
/**
* support comment-directives in HTML template
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/comment-directive/
*/
'svelte/comment-directive'?: Linter.RuleEntry<SvelteCommentDirective>;
/**
* enforce a consistent style for CSS selectors
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/consistent-selector-style/
*/
'svelte/consistent-selector-style'?: Linter.RuleEntry<SvelteConsistentSelectorStyle>;
/**
* derived store should use same variable names between values and callback
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/derived-has-same-inputs-outputs/
*/
'svelte/derived-has-same-inputs-outputs'?: Linter.RuleEntry<[]>;
/**
* require slot type declaration using the `$$Slots` interface
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/experimental-require-slot-types/
*/
'svelte/experimental-require-slot-types'?: Linter.RuleEntry<[]>;
/**
* require the strictEvents attribute on `<script>` tags
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/experimental-require-strict-events/
*/
'svelte/experimental-require-strict-events'?: Linter.RuleEntry<[]>;
/**
* enforce the location of first attribute
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/first-attribute-linebreak/
*/
'svelte/first-attribute-linebreak'?: Linter.RuleEntry<SvelteFirstAttributeLinebreak>;
/**
* Require or disallow a line break before tag's closing brackets
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/html-closing-bracket-new-line/
*/
'svelte/html-closing-bracket-new-line'?: Linter.RuleEntry<SvelteHtmlClosingBracketNewLine>;
/**
* require or disallow a space before tag's closing brackets
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/html-closing-bracket-spacing/
*/
'svelte/html-closing-bracket-spacing'?: Linter.RuleEntry<SvelteHtmlClosingBracketSpacing>;
/**
* enforce quotes style of HTML attributes
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/html-quotes/
*/
'svelte/html-quotes'?: Linter.RuleEntry<SvelteHtmlQuotes>;
/**
* enforce self-closing style
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/html-self-closing/
*/
'svelte/html-self-closing'?: Linter.RuleEntry<SvelteHtmlSelfClosing>;
/**
* enforce consistent indentation
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/indent/
*/
'svelte/indent'?: Linter.RuleEntry<SvelteIndent>;
/**
* Svelte runtime prevents calling the same reactive statement twice in a microtask. But between different microtask, it doesn't prevent.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/infinite-reactive-loop/
*/
'svelte/infinite-reactive-loop'?: Linter.RuleEntry<[]>;
/**
* enforce the maximum number of attributes per line
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/max-attributes-per-line/
*/
'svelte/max-attributes-per-line'?: Linter.RuleEntry<SvelteMaxAttributesPerLine>;
/**
* enforce maximum number of lines in svelte component blocks
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/max-lines-per-block/
*/
'svelte/max-lines-per-block'?: Linter.RuleEntry<SvelteMaxLinesPerBlock>;
/**
* enforce unified spacing in mustache
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/mustache-spacing/
*/
'svelte/mustache-spacing'?: Linter.RuleEntry<SvelteMustacheSpacing>;
/**
* Warns against the use of `addEventListener`
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-add-event-listener/
*/
'svelte/no-add-event-listener'?: Linter.RuleEntry<[]>;
/**
* disallow the use of `{@const}` in favor of `{const ...}` declaration tags
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-at-const-tags/
*/
'svelte/no-at-const-tags'?: Linter.RuleEntry<[]>;
/**
* disallow the use of `{@debug}`
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-at-debug-tags/
*/
'svelte/no-at-debug-tags'?: Linter.RuleEntry<[]>;
/**
* disallow use of `{@html}` to prevent XSS attack
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-at-html-tags/
*/
'svelte/no-at-html-tags'?: Linter.RuleEntry<[]>;
/**
* disallow useless `bind:value` on `<input type="checkbox">` and `<input type="radio">`
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-bind-value-on-checkable-inputs/
*/
'svelte/no-bind-value-on-checkable-inputs'?: Linter.RuleEntry<[]>;
/**
* disallow a `.svelte` component and a same-named runes module (e.g. `Foo.svelte` and `Foo.svelte.ts`) from coexisting
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-conflicting-module-names/
*/
'svelte/no-conflicting-module-names'?: Linter.RuleEntry<[]>;
/**
* disallow DOM manipulating
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dom-manipulating/
*/
'svelte/no-dom-manipulating'?: Linter.RuleEntry<[]>;
/**
* disallow duplicate conditions in `{#if}` / `{:else if}` chains
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-else-if-blocks/
*/
'svelte/no-dupe-else-if-blocks'?: Linter.RuleEntry<[]>;
/**
* disallow duplicate `on:` directives
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-on-directives/
*/
'svelte/no-dupe-on-directives'?: Linter.RuleEntry<[]>;
/**
* disallow duplicate style properties
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-style-properties/
*/
'svelte/no-dupe-style-properties'?: Linter.RuleEntry<[]>;
/**
* disallow duplicate `use:` directives
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-use-directives/
*/
'svelte/no-dupe-use-directives'?: Linter.RuleEntry<[]>;
/**
* disallow dynamic slot name
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dynamic-slot-name/
* @deprecated
*/
'svelte/no-dynamic-slot-name'?: Linter.RuleEntry<[]>;
/**
* disallow exporting load functions in `*.svelte` module in SvelteKit page components.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-export-load-in-svelte-module-in-kit-pages/
*/
'svelte/no-export-load-in-svelte-module-in-kit-pages'?: Linter.RuleEntry<[]>;
/**
* disallow wrapping single reactive statements in curly braces
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-extra-reactive-curlies/
*/
'svelte/no-extra-reactive-curlies'?: Linter.RuleEntry<[]>;
/**
* disallow using goto() without the base path
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-goto-without-base/
* @deprecated
*/
'svelte/no-goto-without-base'?: Linter.RuleEntry<[]>;
/**
* disallow ignoring the unsubscribe method returned by the `subscribe()` on Svelte stores.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-ignored-unsubscribe/
*/
'svelte/no-ignored-unsubscribe'?: Linter.RuleEntry<[]>;
/**
* disallow reactive statements that don't reference reactive values.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-immutable-reactive-statements/
*/
'svelte/no-immutable-reactive-statements'?: Linter.RuleEntry<[]>;
/**
* disallow attributes and directives that produce inline styles
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inline-styles/
*/
'svelte/no-inline-styles'?: Linter.RuleEntry<SvelteNoInlineStyles>;
/**
* disallow variable or `function` declarations in nested blocks
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inner-declarations/
*/
'svelte/no-inner-declarations'?: Linter.RuleEntry<SvelteNoInnerDeclarations>;
/**
* Warns against the use of `$inspect` directive
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inspect/
*/
'svelte/no-inspect'?: Linter.RuleEntry<[]>;
/**
* disallow using navigation (links, goto, pushState, replaceState) without the base path
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-base/
* @deprecated
*/
'svelte/no-navigation-without-base'?: Linter.RuleEntry<SvelteNoNavigationWithoutBase>;
/**
* disallow internal navigation (links, `goto()`, `pushState()`, `replaceState()`) without a `resolve()`
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/
*/
'svelte/no-navigation-without-resolve'?: Linter.RuleEntry<SvelteNoNavigationWithoutResolve>;
/**
* disallow `<style>` elements nested inside other elements or blocks
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-nested-style-tag/
*/
'svelte/no-nested-style-tag'?: Linter.RuleEntry<[]>;
/**
* disallow use of not function in event handler
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-not-function-handler/
*/
'svelte/no-not-function-handler'?: Linter.RuleEntry<[]>;
/**
* disallow objects in text mustache interpolation
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-object-in-text-mustaches/
*/
'svelte/no-object-in-text-mustaches'?: Linter.RuleEntry<[]>;
/**
* Checks for invalid raw HTML elements
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-raw-special-elements/
*/
'svelte/no-raw-special-elements'?: Linter.RuleEntry<[]>;
/**
* it's not necessary to define functions in reactive statements
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-functions/
*/
'svelte/no-reactive-functions'?: Linter.RuleEntry<[]>;
/**
* don't assign literal values in reactive statements
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-literals/
*/
'svelte/no-reactive-literals'?: Linter.RuleEntry<[]>;
/**
* disallow reassigning reactive values
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-reassign/
*/
'svelte/no-reactive-reassign'?: Linter.RuleEntry<SvelteNoReactiveReassign>;
/**
* disallow specific HTML elements
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-restricted-html-elements/
*/
'svelte/no-restricted-html-elements'?: Linter.RuleEntry<SvelteNoRestrictedHtmlElements>;
/**
* disallow shorthand style properties that override related longhand properties
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-shorthand-style-property-overrides/
*/
'svelte/no-shorthand-style-property-overrides'?: Linter.RuleEntry<[]>;
/**
* disallow spaces around equal signs in attribute
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-spaces-around-equal-signs-in-attribute/
*/
'svelte/no-spaces-around-equal-signs-in-attribute'?: Linter.RuleEntry<[]>;
/**
* disallow using async/await inside svelte stores because it causes issues with the auto-unsubscribing features
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-store-async/
*/
'svelte/no-store-async'?: Linter.RuleEntry<[]>;
/**
* svelte/internal will be removed in Svelte 6.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-svelte-internal/
*/
'svelte/no-svelte-internal'?: Linter.RuleEntry<[]>;
/**
* disallow `target="_blank"` attribute without `rel="noopener noreferrer"`
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-target-blank/
*/
'svelte/no-target-blank'?: Linter.RuleEntry<SvelteNoTargetBlank>;
/**
* disallow using top-level browser global variables
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-top-level-browser-globals/
*/
'svelte/no-top-level-browser-globals'?: Linter.RuleEntry<[]>;
/**
* disallow trailing whitespace at the end of lines
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-trailing-spaces/
*/
'svelte/no-trailing-spaces'?: Linter.RuleEntry<SvelteNoTrailingSpaces>;
/**
* disallow unknown `style:property`
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unknown-style-directive-property/
*/
'svelte/no-unknown-style-directive-property'?: Linter.RuleEntry<SvelteNoUnknownStyleDirectiveProperty>;
/**
* Disallow unnecessary $state wrapping of reactive classes
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unnecessary-state-wrap/
*/
'svelte/no-unnecessary-state-wrap'?: Linter.RuleEntry<SvelteNoUnnecessaryStateWrap>;
/**
* disallow the use of a class in the template without a corresponding style
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-class-name/
*/
'svelte/no-unused-class-name'?: Linter.RuleEntry<SvelteNoUnusedClassName>;
/**
* Warns about defined Props properties that are unused
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-props/
*/
'svelte/no-unused-props'?: Linter.RuleEntry<SvelteNoUnusedProps>;
/**
* disallow unused svelte-ignore comments
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/
*/
'svelte/no-unused-svelte-ignore'?: Linter.RuleEntry<[]>;
/**
* disallow explicit children snippet where it's not needed
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-children-snippet/
*/
'svelte/no-useless-children-snippet'?: Linter.RuleEntry<[]>;
/**
* disallow unnecessary mustache interpolations
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/
*/
'svelte/no-useless-mustaches'?: Linter.RuleEntry<SvelteNoUselessMustaches>;
/**
* require attribute interpolation instead of template literals
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-attribute-interpolation/
*/
'svelte/prefer-attribute-interpolation'?: Linter.RuleEntry<[]>;
/**
* require class directives instead of ternary expressions
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-class-directive/
*/
'svelte/prefer-class-directive'?: Linter.RuleEntry<SveltePreferClassDirective>;
/**
* Require `const` declarations for variables that are never reassigned after declared
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/
*/
'svelte/prefer-const'?: Linter.RuleEntry<SveltePreferConst>;
/**
* disallow unnecessary `$derived.by()` when `$derived()` is sufficient
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-derived-over-derived-by/
*/
'svelte/prefer-derived-over-derived-by'?: Linter.RuleEntry<[]>;
/**
* destructure values from object stores for better change tracking & fewer redraws
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/
*/
'svelte/prefer-destructured-store-props'?: Linter.RuleEntry<[]>;
/**
* require style directives instead of style attribute
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-style-directive/
*/
'svelte/prefer-style-directive'?: Linter.RuleEntry<[]>;
/**
* disallow using mutable instances of built-in classes where a reactive alternative is provided by svelte/reactivity
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-svelte-reactivity/
*/
'svelte/prefer-svelte-reactivity'?: Linter.RuleEntry<[]>;
/**
* Prefer using writable $derived instead of $state and $effect
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-writable-derived/
*/
'svelte/prefer-writable-derived'?: Linter.RuleEntry<[]>;
/**
* require keyed `{#each}` block
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/require-each-key/
*/
'svelte/require-each-key'?: Linter.RuleEntry<[]>;
/**
* require type parameters for `createEventDispatcher`
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/require-event-dispatcher-types/
*/
'svelte/require-event-dispatcher-types'?: Linter.RuleEntry<[]>;
/**
* require component event names to start with "on"
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/require-event-prefix/
*/
'svelte/require-event-prefix'?: Linter.RuleEntry<SvelteRequireEventPrefix>;
/**
* require style attributes that can be optimized
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/require-optimized-style-attribute/
*/
'svelte/require-optimized-style-attribute'?: Linter.RuleEntry<[]>;
/**
* store callbacks must use `set` param
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/require-store-callbacks-use-set-param/
*/
'svelte/require-store-callbacks-use-set-param'?: Linter.RuleEntry<[]>;
/**
* disallow to use of the store itself as an operand. Need to use $ prefix or get function.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/require-store-reactive-access/
*/
'svelte/require-store-reactive-access'?: Linter.RuleEntry<[]>;
/**
* require initial value in store
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/require-stores-init/
*/
'svelte/require-stores-init'?: Linter.RuleEntry<[]>;
/**
* enforce use of shorthand syntax in attribute
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/shorthand-attribute/
*/
'svelte/shorthand-attribute'?: Linter.RuleEntry<SvelteShorthandAttribute>;
/**
* enforce use of shorthand syntax in directives
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/shorthand-directive/
*/
'svelte/shorthand-directive'?: Linter.RuleEntry<SvelteShorthandDirective>;
/**
* enforce order of attributes
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/sort-attributes/
*/
'svelte/sort-attributes'?: Linter.RuleEntry<SvelteSortAttributes>;
/**
* enforce consistent spacing after the `<!--` and before the `-->` in a HTML comment
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/spaced-html-comment/
*/
'svelte/spaced-html-comment'?: Linter.RuleEntry<SvelteSpacedHtmlComment>;
/**
* system rule for working this plugin
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/system/
*/
'svelte/system'?: Linter.RuleEntry<[]>;
/**
* disallow warnings when compiling.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-compile/
*/
'svelte/valid-compile'?: Linter.RuleEntry<SvelteValidCompile>;
/**
* enforce keys to use variables defined in the `{#each}` block
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-each-key/
*/
'svelte/valid-each-key'?: Linter.RuleEntry<[]>;
/**
* disallow invalid props in SvelteKit route components.
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-prop-names-in-kit-pages/
*/
'svelte/valid-prop-names-in-kit-pages'?: Linter.RuleEntry<[]>;
/**
* require valid style element parsing
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-style-parse/
*/
'svelte/valid-style-parse'?: Linter.RuleEntry<[]>;
}
type SvelteTypescriptEslintNoUnnecessaryCondition = [] | [
{
allowConstantLoopConditions?: boolean;
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
}
];
type SvelteBlockLang = [] | [
{
enforceScriptPresent?: boolean;
enforceStylePresent?: boolean;
script?: ((string | null) | [(string | null), ...((string | null))[]]);
style?: ((string | null) | [(string | null), ...((string | null))[]]);
}
];
type SvelteButtonHasType = [] | [
{
button?: boolean;
submit?: boolean;
reset?: boolean;
}
];
type SvelteCommentDirective = [] | [
{
reportUnusedDisableDirectives?: boolean;
}
];
type SvelteConsistentSelectorStyle = [] | [
{
checkGlobal?: boolean;
style?: [] | [("class" | "id" | "type")] | [("class" | "id" | "type"), ("class" | "id" | "type")] | [("class" | "id" | "type"), ("class" | "id" | "type"), ("class" | "id" | "type")];
}
];
type SvelteFirstAttributeLinebreak = [] | [
{
multiline?: ("below" | "beside");
singleline?: ("below" | "beside");
}
];
type SvelteHtmlClosingBracketNewLine = [] | [
{
singleline?: ("always" | "never");
multiline?: ("always" | "never");
selfClosingTag?: {
singleline?: ("always" | "never");
multiline?: ("always" | "never");
};
}
];
type SvelteHtmlClosingBracketSpacing = [] | [
{
startTag?: ("always" | "never" | "ignore");
endTag?: ("always" | "never" | "ignore");
selfClosingTag?: ("always" | "never" | "ignore");
}
];
type SvelteHtmlQuotes = [] | [
{
prefer?: ("double" | "single");
dynamic?: {
quoted?: boolean;
avoidInvalidUnquotedInHTML?: boolean;
};
}
];
type SvelteHtmlSelfClosing = [] | [
({
void?: ("never" | "always" | "ignore");
normal?: ("never" | "always" | "ignore");
svg?: ("never" | "always" | "ignore");
math?: ("never" | "always" | "ignore");
component?: ("never" | "always" | "ignore");
svelte?: ("never" | "always" | "ignore");
} | ("all" | "html" | "none"))
];
type SvelteIndent = [] | [
{
indent?: (number | "tab");
indentScript?: boolean;
switchCase?: number;
alignAttributesVertically?: boolean;
ignoredNodes?: (string & {
[k: string]: unknown | undefined;
} & {
[k: string]: unknown | undefined;
})[];
}
];
type SvelteMaxAttributesPerLine = [] | [
{
multiline?: number;
singleline?: number;
}
];
type SvelteMaxLinesPerBlock = [] | [
{
script?: number;
template?: number;
style?: number;
skipBlankLines?: boolean;
skipComments?: boolean;
}
];
type SvelteMustacheSpacing = [] | [
{
textExpressions?: ("never" | "always");
attributesAndProps?: ("never" | "always");
directiveExpressions?: ("never" | "always");
tags?: {
openingBrace?: ("never" | "always");
closingBrace?: ("never" | "always" | "always-after-expression");
};
}
];
type SvelteNoInlineStyles = [] | [
{
allowTransitions?: boolean;
}
];
type SvelteNoInnerDeclarations = [] | [("functions" | "both")] | [
("functions" | "both"),
{
blockScopedFunctions?: ("allow" | "disallow");
}
];
type SvelteNoNavigationWithoutBase = [] | [
{
ignoreGoto?: boolean;
ignoreLinks?: boolean;
ignorePushState?: boolean;
ignoreReplaceState?: boolean;
}
];
type SvelteNoNavigationWithoutResolve = [] | [
{
ignoreGoto?: boolean;
ignoreLinks?: boolean;
ignorePushState?: boolean;
ignoreReplaceState?: boolean;
}
];
type SvelteNoReactiveReassign = [] | [
{
props?: boolean;
}
];
type SvelteNoRestrictedHtmlElements = [
(string | {
elements?: [string, ...(string)[]];
message?: string;
}),
...((string | {
elements?: [string, ...(string)[]];
message?: string;
}))[]
];
type SvelteNoTargetBlank = [] | [
{
allowReferrer?: boolean;
enforceDynamicLinks?: ("always" | "never");
}
];
type SvelteNoTrailingSpaces = [] | [
{
skipBlankLines?: boolean;
ignoreComments?: boolean;
}
];
type SvelteNoUnknownStyleDirectiveProperty = [] | [
{
ignoreProperties?: [string, ...(string)[]];
ignorePrefixed?: boolean;
}
];
type SvelteNoUnnecessaryStateWrap = [] | [
{
additionalReactiveClasses?: string[];
allowReassign?: boolean;
}
];
type SvelteNoUnusedClassName = [] | [
{
allowedClassNames?: string[];
}
];
type SvelteNoUnusedProps = [] | [
{
checkImportedTypes?: boolean;
ignoreTypePatterns?: string[];
ignorePropertyPatterns?: string[];
allowUnusedNestedProperties?: boolean;
}
];
type SvelteNoUselessMustaches = [] | [
{
ignoreIncludesComment?: boolean;
ignoreStringEscape?: boolean;
}
];
type SveltePreferClassDirective = [] | [
{
prefer?: ("always" | "empty");
}
];
type SveltePreferConst = [] | [
{
destructuring?: ("any" | "all");
ignoreReadBeforeAssign?: boolean;
excludedRunes?: string[];
[k: string]: unknown | undefined;
}
];
type SvelteRequireEventPrefix = [] | [
{
checkAsyncFunctions?: boolean;
}
];
type SvelteShorthandAttribute = [] | [
{
prefer?: ("always" | "never");
}
];
type SvelteShorthandDirective = [] | [
{
prefer?: ("always" | "never");
}
];
type SvelteSortAttributes = [] | [
{
order?: (string | [string, ...(string)[]] | {
match: (string | [string, ...(string)[]]);
sort: ("alphabetical" | "ignore");
})[];
alphabetical?: boolean;
}
];
type SvelteSpacedHtmlComment = [] | [("always" | "never")];
type SvelteValidCompile = [] | [
{
ignoreWarnings?: boolean;
}
];
export {};

4
node_modules/eslint-plugin-svelte/lib/rule-types.js generated vendored Normal file
View File

@@ -0,0 +1,4 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
export {};

View File

@@ -0,0 +1,2 @@
declare const _default: import("../../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,587 @@
import { createRule } from '../../utils/index.js';
import { isFalsyType, getConstrainedTypeAtLocation, isTruthyLiteral, isPossiblyFalsyType, isNullishType, isBooleanLiteralType, getTypeScriptTools, isAnyType, isUnknownType, isNeverType, getCallSignaturesOfType, isNullableType, getTypeOfPropertyOfType, getTypeName, isTupleType } from '../../utils/ts-utils/index.js';
/**
* Returns all types of a union type or an array containing `type` itself if it's no union type.
* This method is heavily inspired by tsutils. https://github.com/ajafff/tsutils
* The MIT License (MIT) Copyright (c) 2017 Klaus Meinhardt
* https://github.com/ajafff/tsutils/blob/master/LICENSE
*/
function unionTypeParts(type) {
return [...iterate(type)];
/**
* iterate
*/
function* iterate(t) {
if (t.isUnion()) {
for (const type of t.types) {
yield* iterate(type);
}
}
else {
yield t;
}
}
}
/**
* Check whether the given type can be a falsy type or not.
*/
function isPossiblyFalsy(type, tsTools) {
return (unionTypeParts(type)
// PossiblyFalsy flag includes literal values, so exclude ones that
// are definitely truthy
.filter((t) => !isTruthyLiteral(t, tsTools))
.some((type) => isPossiblyFalsyType(type, tsTools.ts)));
}
/**
* Check whether the given type can be a truthy type or not.
*/
function isPossiblyTruthy(type, tsTools) {
return unionTypeParts(type).some((type) => !isFalsyType(type, tsTools));
}
/**
* Check whether the given type can be a nullish type or not.
*/
function isPossiblyNullish(type, tsTools) {
return isNullableType(type, tsTools.ts);
}
/**
* Check whether the given type is a nullish type or not.
*/
function isAlwaysNullish(type, tsTools) {
return isNullishType(type, tsTools.ts);
}
/**
* Check whether the given type is a literal type or not.
*/
function isLiteral(type, tsTools) {
return (isBooleanLiteralType(type, tsTools.ts) || isNullishType(type, tsTools.ts) || type.isLiteral());
}
export default createRule('@typescript-eslint/no-unnecessary-condition', {
meta: {
docs: {
description: 'disallow conditionals where the type is always truthy or always falsy',
category: 'Extension Rules',
recommended: false,
extensionRule: {
plugin: '@typescript-eslint/eslint-plugin',
url: 'https://typescript-eslint.io/rules/no-unnecessary-condition/'
}
},
schema: [
{
type: 'object',
properties: {
allowConstantLoopConditions: {
description: 'Whether to ignore constant loop conditions, such as `while (true)`.',
type: 'boolean'
},
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: {
description: 'Whether to not error when running with a tsconfig that has strictNullChecks turned.',
type: 'boolean'
}
},
additionalProperties: false
}
],
fixable: 'code',
messages: {
alwaysTruthy: 'Unnecessary conditional, value is always truthy.',
alwaysFalsy: 'Unnecessary conditional, value is always falsy.',
alwaysTruthyFunc: 'This callback should return a conditional, but return is always truthy.',
alwaysFalsyFunc: 'This callback should return a conditional, but return is always falsy.',
neverNullish: 'Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.',
alwaysNullish: 'Unnecessary conditional, left-hand side of `??` operator is always `null` or `undefined`.',
literalBooleanExpression: 'Unnecessary conditional, both sides of the expression are literal values.',
noOverlapBooleanExpression: 'Unnecessary conditional, the types have no overlap.',
never: 'Unnecessary conditional, value is `never`.',
neverOptionalChain: 'Unnecessary optional chain on a non-nullish value.',
noStrictNullCheck: 'This rule requires the `strictNullChecks` compiler option to be turned on to function correctly.'
},
type: 'suggestion',
deprecated: true,
replacedBy: {
note: 'This rule is no longer needed when using svelte-eslint-parser>=v0.19.0.'
}
},
create(context) {
const { allowConstantLoopConditions = false, allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing = false } = (context.options[0] || {});
const tools = getTypeScriptTools(context);
if (!tools) {
return {};
}
const { service, ts } = tools;
const checker = service.program.getTypeChecker();
const sourceCode = context.sourceCode;
const compilerOptions = service.program.getCompilerOptions();
const isStrictNullChecks = compilerOptions.strict
? compilerOptions.strictNullChecks !== false
: compilerOptions.strictNullChecks;
if (!isStrictNullChecks && allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing !== true) {
context.report({
loc: {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 }
},
messageId: 'noStrictNullCheck'
});
}
const mutableVarReferenceIds = [];
const scriptElements = [];
let inSvelteReactiveStatement = false;
// Extract references to mutable variables in the root scope.
for (const scope of [
sourceCode.scopeManager.globalScope,
sourceCode.scopeManager.globalScope?.childScopes.find((scope) => scope.type === 'module')
]) {
if (!scope)
continue;
for (const variable of scope.variables) {
if (variable.defs.some((def) => def.type === 'Variable' && (def.parent.kind === 'var' || def.parent.kind === 'let'))) {
for (const reference of variable.references) {
mutableVarReferenceIds.push(reference.identifier);
}
}
}
}
// Extract <script> ranges.
for (const body of sourceCode.ast.body) {
if (body.type === 'SvelteScriptElement') {
scriptElements.push(body);
}
}
/**
* Checks whether the given expression node is in Svelte reactive scope
* and the variables that make up the given expression node use
* mutable variables declared in component root scope.
*/
function hasSvelteReactiveVar(node) {
const inReactiveScope = inSvelteReactiveStatement ||
(scriptElements.length &&
scriptElements.every((elem) => node.range[1] <= elem.range[0] || elem.range[1] <= node.range[0]));
if (!inReactiveScope) {
// The given expression node is neither in a reactive scope nor in a template scope.
return false;
}
return mutableVarReferenceIds.some((id) => node.range[0] <= id.range[0] && id.range[1] <= node.range[1]);
}
/** Get the TS type from ES Node */
function getNodeType(node) {
const tsNode = service.esTreeNodeToTSNodeMap.get(node);
return tsNode && getConstrainedTypeAtLocation(checker, tsNode);
}
/**
* Check whether the given node is an array type or not.
*/
function nodeIsArrayType(node) {
const nodeType = getNodeType(node);
if (!nodeType) {
return false;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- isArrayType is an internal API of TS.
return checker.isArrayType(nodeType);
}
/**
* Check whether the given node is an tuple type or not.
*/
function nodeIsTupleType(node) {
const nodeType = getNodeType(node);
return Boolean(nodeType && isTupleType(nodeType, ts));
}
/**
* Check whether the given node is an array index signature or not.
*/
function isArrayIndexExpression(node) {
return (
// Is an index signature
node.type === 'MemberExpression' &&
node.computed &&
// ...into an array type
(nodeIsArrayType(node.object) ||
// ... or a tuple type
(nodeIsTupleType(node.object) &&
// Exception: literal index into a tuple - will have a sound type
node.property.type !== 'Literal')));
}
/**
* Checks if a conditional node is necessary:
* if the type of the node is always true or always false, it's not necessary.
*/
function checkNode(node, isUnaryNotArgument = false) {
if (hasSvelteReactiveVar(node)) {
return;
}
// Check if the node is Unary Negation expression and handle it
if (node.type === 'UnaryExpression' && node.operator === '!') {
checkNode(node.argument, true);
return;
}
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (isArrayIndexExpression(node)) {
return;
}
// When checking logical expressions, only check the right side
// as the left side has been checked by checkLogicalExpressionForUnnecessaryConditionals
//
// Unless the node is nullish coalescing, as it's common to use patterns like `nullBool ?? true` to to strict
// boolean checks if we inspect the right here, it'll usually be a constant condition on purpose.
// In this case it's better to inspect the type of the expression as a whole.
if (node.type === 'LogicalExpression' && node.operator !== '??') {
checkNode(node.right);
return;
}
const type = getNodeType(node);
// Conditional is always necessary if it involves:
// `any` or `unknown` or a naked type parameter
if (!type ||
unionTypeParts(type).some((part) => isAnyType(part, ts) || isUnknownType(part, ts) || part.isTypeParameter())) {
return;
}
let messageId = null;
if (unionTypeParts(type).some((part) => isNeverType(part, ts))) {
messageId = 'never';
}
else if (!isPossiblyTruthy(type, tools)) {
messageId = !isUnaryNotArgument ? 'alwaysFalsy' : 'alwaysTruthy';
}
else if (!isPossiblyFalsy(type, tools)) {
messageId = !isUnaryNotArgument ? 'alwaysTruthy' : 'alwaysFalsy';
}
if (messageId) {
context.report({ node, messageId });
}
}
/**
* Checks if a conditional node is necessary from the given lhs of nullish coalescing.
*/
function checkNodeForNullish(node) {
if (hasSvelteReactiveVar(node)) {
return;
}
const type = getNodeType(node);
// Conditional is always necessary if it involves `any` or `unknown`
if (!type || isAnyType(type, ts) || isUnknownType(type, ts)) {
return;
}
let messageId = null;
if (unionTypeParts(type).some((part) => isNeverType(part, ts))) {
messageId = 'never';
}
else if (!isPossiblyNullish(type, tools)) {
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (!isArrayIndexExpression(node) &&
!(node.type === 'ChainExpression' &&
node.expression.type !== 'TSNonNullExpression' &&
optionChainContainsOptionArrayIndex(node.expression))) {
messageId = 'neverNullish';
}
}
else if (isAlwaysNullish(type, tools)) {
messageId = 'alwaysNullish';
}
if (messageId) {
context.report({ node, messageId });
}
}
/**
* Checks that a binary expression is necessarily conditional, reports otherwise.
* If both sides of the binary expression are literal values, it's not a necessary condition.
*
* NOTE: It's also unnecessary if the types that don't overlap at all
* but that case is handled by the Typescript compiler itself.
* Known exceptions:
* * https://github.com/microsoft/TypeScript/issues/32627
* * https://github.com/microsoft/TypeScript/issues/37160 (handled)
*/
const BOOL_OPERATORS = new Set(['<', '>', '<=', '>=', '==', '===', '!=', '!==']);
/**
* Checks if a conditional node is necessary from the given binary expression.
*/
function checkIfBinaryExpressionIsNecessaryConditional(node) {
if (hasSvelteReactiveVar(node)) {
return;
}
if (!BOOL_OPERATORS.has(node.operator)) {
return;
}
const leftType = getNodeType(node.left);
const rightType = getNodeType(node.right);
if (!leftType || !rightType) {
return;
}
if (isLiteral(leftType, tools) && isLiteral(rightType, tools)) {
context.report({ node, messageId: 'literalBooleanExpression' });
return;
}
// Workaround for https://github.com/microsoft/TypeScript/issues/37160
if (isStrictNullChecks) {
const UNDEFINED = ts.TypeFlags.Undefined;
const NULL = ts.TypeFlags.Null;
// eslint-disable-next-line func-style -- ignore
const isComparable = (type, f) => {
let flag = f;
// Allow comparison to `any`, `unknown` or a naked type parameter.
flag |= ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter;
// Allow loose comparison to nullish values.
if (node.operator === '==' || node.operator === '!=') {
flag |= NULL | UNDEFINED;
}
return unionTypeParts(type).some((t) => (t.flags & flag) !== 0);
};
if ((leftType.flags === UNDEFINED && !isComparable(rightType, UNDEFINED)) ||
(rightType.flags === UNDEFINED && !isComparable(leftType, UNDEFINED)) ||
(leftType.flags === NULL && !isComparable(rightType, NULL)) ||
(rightType.flags === NULL && !isComparable(leftType, NULL))) {
context.report({ node, messageId: 'noOverlapBooleanExpression' });
}
}
}
/**
* Checks that a logical expression contains a boolean, reports otherwise.
*/
function checkLogicalExpressionForUnnecessaryConditionals(node) {
if (node.operator === '??') {
checkNodeForNullish(node.left);
return;
}
// Only checks the left side, since the right side might not be "conditional" at all.
// The right side will be checked if the LogicalExpression is used in a conditional context
checkNode(node.left);
}
/**
* Checks that a testable expression of a loop is necessarily conditional, reports otherwise.
*/
function checkIfLoopIsNecessaryConditional(node) {
if (node.test === null) {
// e.g. `for(;;)`
return;
}
/**
* Allow:
* while (true) {}
* for (;true;) {}
* do {} while (true)
*/
if (allowConstantLoopConditions) {
const nodeType = getNodeType(node.test);
if (nodeType &&
isBooleanLiteralType(nodeType, ts) &&
checker.typeToString(nodeType) === 'true')
return;
}
checkNode(node.test);
}
const ARRAY_PREDICATE_FUNCTIONS = new Set(['filter', 'find', 'some', 'every']);
/**
* Checks whether the given call expression is an array method that takes a predicate as an argument.
*/
function isArrayPredicateFunction(node) {
const { callee } = node;
return (
// looks like `something.filter` or `something.find`
callee.type === 'MemberExpression' &&
callee.property.type === 'Identifier' &&
ARRAY_PREDICATE_FUNCTIONS.has(callee.property.name) &&
// and the left-hand side is an array, according to the types
(nodeIsArrayType(callee.object) || nodeIsTupleType(callee.object)));
}
/**
* Checks if a conditional node is necessary from the given call expression.
*/
function checkCallExpression(node) {
// If this is something like arr.filter(x => /*condition*/), check `condition`
if (isArrayPredicateFunction(node) && node.arguments.length) {
const callback = node.arguments[0];
// Inline defined functions
if ((callback.type === 'ArrowFunctionExpression' || callback.type === 'FunctionExpression') &&
callback.body) {
// Two special cases, where we can directly check the node that's returned:
// () => something
if (callback.body.type !== 'BlockStatement') {
checkNode(callback.body);
return;
}
// () => { return something; }
const callbackBody = callback.body.body;
if (callbackBody.length === 1 &&
callbackBody[0].type === 'ReturnStatement' &&
callbackBody[0].argument) {
checkNode(callbackBody[0].argument);
return;
}
// Potential enhancement: could use code-path analysis to check
// any function with a single return statement
// (Value to complexity ratio is dubious however)
}
const nodeType = getNodeType(callback);
if (!nodeType) {
return;
}
// Otherwise just do type analysis on the function as a whole.
const returnTypes = getCallSignaturesOfType(nodeType).map((sig) => sig.getReturnType());
/* istanbul ignore if */ if (returnTypes.length === 0) {
// Not a callable function
return;
}
// Predicate is always necessary if it involves `any` or `unknown`
if (returnTypes.some((t) => isAnyType(t, ts) || isUnknownType(t, ts))) {
return;
}
if (!returnTypes.some((t) => isPossiblyFalsy(t, tools))) {
context.report({
node: callback,
messageId: 'alwaysTruthyFunc'
});
return;
}
if (!returnTypes.some((t) => isPossiblyTruthy(t, tools))) {
context.report({
node: callback,
messageId: 'alwaysFalsyFunc'
});
}
}
}
/**
*
* Recursively searches an optional chain for an array index expression
* Has to search the entire chain, because an array index will "infect" the rest of the types
* Example:
* ```
* [{x: {y: "z"} }][n] // type is {x: {y: "z"}}
* ?.x // type is {y: "z"}
* ?.y // This access is considered "unnecessary" according to the types
* ```
*/
function optionChainContainsOptionArrayIndex(node) {
const lhsNode = node.type === 'CallExpression' ? node.callee : node.object;
if (node.optional && isArrayIndexExpression(lhsNode)) {
return true;
}
if (lhsNode.type === 'MemberExpression' || lhsNode.type === 'CallExpression') {
return optionChainContainsOptionArrayIndex(lhsNode);
}
return false;
}
/**
* Check whether the given property is a nullable or not.
*/
function isNullablePropertyType(objType, propertyType) {
if (propertyType.isUnion()) {
return propertyType.types.some((type) => isNullablePropertyType(objType, type));
}
if (propertyType.isNumberLiteral() || propertyType.isStringLiteral()) {
const propType = getTypeOfPropertyOfType(objType, propertyType.value.toString(), checker);
if (propType) {
return isNullableType(propType, ts);
}
}
const typeName = getTypeName(propertyType, tools);
return Boolean((typeName === 'string' && checker.getIndexInfoOfType(objType, ts.IndexKind.String)) ||
(typeName === 'number' && checker.getIndexInfoOfType(objType, ts.IndexKind.Number)));
}
/**
* Checks whether a member expression is nullable or not regardless of it's previous node.
* Example:
* ```
* // 'bar' is nullable if 'foo' is null.
* // but this function checks regardless of 'foo' type, so returns 'true'.
* declare const foo: { bar : { baz: string } } | null
* foo?.bar;
* ```
*/
function isNullableOriginFromPrev(node) {
const prevType = getNodeType(node.object);
const property = node.property;
if (prevType && prevType.isUnion() && property.type === 'Identifier') {
const isOwnNullable = prevType.types.some((type) => {
if (node.computed) {
const propertyType = getNodeType(node.property);
return Boolean(propertyType && isNullablePropertyType(type, propertyType));
}
const propType = getTypeOfPropertyOfType(type, property.name, checker);
return propType && isNullableType(propType, ts);
});
return !isOwnNullable && isNullableType(prevType, ts);
}
return false;
}
/**
* Checks whether a lhs expression is optionable or not.
*/
function isOptionableExpression(node) {
const type = getNodeType(node);
if (!type) {
return false;
}
const isOwnNullable = node.type === 'MemberExpression' ? !isNullableOriginFromPrev(node) : true;
return (isAnyType(type, ts) ||
isUnknownType(type, ts) ||
(isNullableType(type, ts) && isOwnNullable));
}
/**
* Checks if a conditional node is necessary from the given optional chaining expression.
*/
function checkOptionalChain(node, beforeOperator, fix) {
// We only care if this step in the chain is optional. If just descend
// from an optional chain, then that's fine.
if (!node.optional) {
return;
}
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (optionChainContainsOptionArrayIndex(node)) {
return;
}
const nodeToCheck = node.type === 'CallExpression' ? node.callee : node.object;
if (hasSvelteReactiveVar(nodeToCheck)) {
return;
}
if (isOptionableExpression(nodeToCheck)) {
return;
}
const questionDotOperator = sourceCode.getTokenAfter(beforeOperator, {
includeComments: false,
filter: (token) => token.type === 'Punctuator' && token.value === '?.'
});
context.report({
node,
loc: questionDotOperator.loc,
messageId: 'neverOptionalChain',
fix(fixer) {
return fixer.replaceText(questionDotOperator, fix);
}
});
}
/**
* Checks for optional member expression
*/
function checkOptionalMemberExpression(node) {
checkOptionalChain(node, node.object, node.computed ? '' : '.');
}
/**
* Checks for optional call expression
*/
function checkOptionalCallExpression(node) {
checkOptionalChain(node, node.callee, '');
}
return {
SvelteReactiveStatement: () => (inSvelteReactiveStatement = true),
'SvelteReactiveStatement:exit': () => (inSvelteReactiveStatement = false),
BinaryExpression: checkIfBinaryExpressionIsNecessaryConditional,
CallExpression: checkCallExpression,
ConditionalExpression: (node) => checkNode(node.test),
DoWhileStatement: checkIfLoopIsNecessaryConditional,
ForStatement: checkIfLoopIsNecessaryConditional,
IfStatement: (node) => checkNode(node.test),
LogicalExpression: checkLogicalExpressionForUnnecessaryConditionals,
WhileStatement: checkIfLoopIsNecessaryConditional,
'MemberExpression[optional = true]': checkOptionalMemberExpression,
'CallExpression[optional = true]': checkOptionalCallExpression
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,184 @@
import { createRule } from '../utils/index.js';
import { findAttribute, getLangValue } from '../utils/ast-utils.js';
export default createRule('block-lang', {
meta: {
docs: {
description: 'disallows the use of languages other than those specified in the configuration for the lang attribute of `<script>` and `<style>` blocks.',
category: 'Best Practices',
recommended: false
},
schema: [
{
type: 'object',
properties: {
enforceScriptPresent: {
type: 'boolean'
},
enforceStylePresent: {
type: 'boolean'
},
script: {
oneOf: [
{
type: ['string', 'null']
},
{
type: 'array',
items: {
type: ['string', 'null']
},
minItems: 1
}
]
},
style: {
oneOf: [
{
type: ['string', 'null']
},
{
type: 'array',
items: {
type: ['string', 'null']
},
minItems: 1
}
]
}
},
additionalProperties: false
}
],
messages: {},
type: 'suggestion',
hasSuggestions: true
},
create(context) {
if (!context.sourceCode.parserServices.isSvelte) {
return {};
}
const enforceScriptPresent = context.options[0]?.enforceScriptPresent ?? false;
const enforceStylePresent = context.options[0]?.enforceStylePresent ?? false;
const scriptOption = context.options[0]?.script ?? null;
const allowedScriptLangs = Array.isArray(scriptOption)
? scriptOption
: [scriptOption];
const scriptNodes = [];
const styleOption = context.options[0]?.style ?? null;
const allowedStyleLangs = Array.isArray(styleOption)
? styleOption
: [styleOption];
const styleNodes = [];
return {
SvelteScriptElement(node) {
scriptNodes.push(node);
},
SvelteStyleElement(node) {
styleNodes.push(node);
},
'Program:exit'() {
if (scriptNodes.length === 0 && enforceScriptPresent) {
context.report({
loc: { line: 1, column: 1 },
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(allowedScriptLangs)}.`,
suggest: buildAddLangSuggestions(allowedScriptLangs, 'script', context.sourceCode)
});
}
for (const scriptNode of scriptNodes) {
if (!allowedScriptLangs.includes(getLangValue(scriptNode)?.toLowerCase() ?? null)) {
context.report({
node: scriptNode,
message: `The lang attribute of the <script> block should be ${prettyPrintLangs(allowedScriptLangs)}.`,
suggest: buildReplaceLangSuggestions(allowedScriptLangs, scriptNode)
});
}
}
if (styleNodes.length === 0 && enforceStylePresent) {
const sourceCode = context.sourceCode;
context.report({
loc: { line: 1, column: 1 },
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(allowedStyleLangs)}.`,
suggest: buildAddLangSuggestions(allowedStyleLangs, 'style', sourceCode)
});
}
for (const styleNode of styleNodes) {
if (!allowedStyleLangs.includes(getLangValue(styleNode)?.toLowerCase() ?? null)) {
context.report({
node: styleNode,
message: `The lang attribute of the <style> block should be ${prettyPrintLangs(allowedStyleLangs)}.`,
suggest: buildReplaceLangSuggestions(allowedStyleLangs, styleNode)
});
}
}
}
};
}
});
function buildAddLangSuggestions(langs, tagName, sourceCode) {
return langs
.filter((lang) => lang != null && lang !== '')
.map((lang) => {
return {
desc: `Add a lang attribute to a <${tagName}> block with the value "${lang}".`,
fix: (fixer) => {
const langAttributeText = getLangAttributeText(lang ?? '', true);
return fixer.insertTextAfterRange(tagName === 'script' ? [0, 0] : [sourceCode.text.length, sourceCode.text.length], `<${tagName}${langAttributeText}>\n</${tagName}>\n\n`);
}
};
});
}
function buildReplaceLangSuggestions(langs, node) {
const tagName = node.name.name;
const langAttribute = findAttribute(node, 'lang');
const filteredLangs = langs.filter((lang) => lang != null && lang !== '');
if (filteredLangs.length === 0 && langs.includes(null) && langAttribute !== null) {
return [
{
desc: `Replace a <${tagName}> block with the lang attribute omitted.`,
fix: (fixer) => {
return fixer.remove({
type: langAttribute.type,
range: [langAttribute.range[0] - 1, langAttribute.range[1]]
});
}
}
];
}
return filteredLangs.map((lang) => {
const langAttributeText = getLangAttributeText(lang ?? '', true);
if (langAttribute) {
return {
desc: `Replace a <${tagName}> block with the lang attribute set to "${lang}".`,
fix: (fixer) => {
return fixer.replaceText(langAttribute, langAttributeText.trim());
}
};
}
return {
desc: `Add lang attribute to a <${tagName}> block with the value "${lang}".`,
fix: (fixer) => {
return fixer.insertTextBeforeRange([node.startTag.range[0] + tagName.length + 1, 0], langAttributeText);
}
};
});
}
/**
* Prints the list of allowed languages, with special handling of the `null` option.
*/
function prettyPrintLangs(langs) {
const hasNull = langs.includes(null);
const nonNullLangs = langs.filter((lang) => lang !== null).map((lang) => `"${lang}"`);
if (nonNullLangs.length === 0) {
// No special behavior for `hasNull`, because that can never happen.
return 'omitted';
}
const hasNullText = hasNull ? 'either omitted or ' : '';
const nonNullText = nonNullLangs.length === 1 ? nonNullLangs[0] : `one of ${nonNullLangs.join(', ')}`;
return hasNullText + nonNullText;
}
/**
* Returns the lang attribute text, with special handling of the `null` lang option with respect to the `prependWhitespace` argument.
*/
function getLangAttributeText(lang, prependWhitespace) {
return `${prependWhitespace ? ' ' : ''}lang="${lang}"`;
}

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,110 @@
import { createRule } from '../utils/index.js';
import { findAttribute, findShorthandAttribute, findBindDirective, getStaticAttributeValue } from '../utils/ast-utils.js';
export default createRule('button-has-type', {
meta: {
docs: {
description: 'disallow usage of button without an explicit type attribute',
category: 'Best Practices',
recommended: false
},
schema: [
{
type: 'object',
properties: {
button: {
type: 'boolean'
},
submit: {
type: 'boolean'
},
reset: {
type: 'boolean'
}
},
additionalProperties: false
}
],
messages: {
missingTypeAttribute: 'Missing an explicit type attribute for button.',
invalidTypeAttribute: '{{value}} is an invalid value for button type attribute.',
forbiddenTypeAttribute: '{{value}} is a forbidden value for button type attribute.',
emptyTypeAttribute: 'A value must be set for button type attribute.'
},
type: 'suggestion' // "problem",
},
create(context) {
const configuration = {
button: true,
submit: true,
reset: true,
...(context.options[0] ?? {})
};
/**
* Checks whether given text is known button type
*/
function isButtonType(type) {
return type === 'button' || type === 'submit' || type === 'reset';
}
/**
* Report
*/
function report(node, messageId, data = {}) {
context.report({
node,
messageId,
data
});
}
/**
* Validate attribute
*/
function validateAttribute(attribute) {
if (attribute.value.length === 0) {
report(attribute, 'emptyTypeAttribute');
return;
}
const strValue = getStaticAttributeValue(attribute);
if (strValue == null) {
return;
}
if (!isButtonType(strValue)) {
report(attribute, 'invalidTypeAttribute', { value: strValue });
}
else if (!configuration[strValue]) {
report(attribute, 'forbiddenTypeAttribute', { value: strValue });
}
}
/**
* @param {VDirective} directive
*/
function validateDirective(directive) {
if (!directive.expression) {
report(directive, 'emptyTypeAttribute');
}
}
return {
"SvelteElement[name.name='button'] > SvelteStartTag"(node) {
const typeAttr = findAttribute(node, 'type');
if (typeAttr) {
validateAttribute(typeAttr);
return;
}
const typeDir = findBindDirective(node, 'type');
if (typeDir) {
validateDirective(typeDir);
return;
}
const typeShortAttr = findShorthandAttribute(node, 'type');
if (typeShortAttr) {
return;
}
for (const attr of node.attributes) {
if (attr.type === 'SvelteSpreadAttribute') {
return;
}
}
report(node, 'missingTypeAttribute');
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,197 @@
import { getShared } from '../shared/index.js';
import { createRule } from '../utils/index.js';
// -----------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------
const COMMENT_DIRECTIVE_B = /^\s*(eslint-(?:en|dis)able)(?:\s+|$)/;
const COMMENT_DIRECTIVE_L = /^\s*(eslint-disable(?:-next)?-line)(?:\s+|$)/;
// eslint-disable-next-line func-style -- ignore
const ALL_RULES = () => true;
/**
* Remove the ignored part from a given directive comment and trim it.
* @param {string} value The comment text to strip.
* @returns {string} The stripped text.
*/
function stripDirectiveComment(value) {
return value.split(/\s-{2,}\s/u)[0];
}
export default createRule('comment-directive', {
meta: {
docs: {
description: 'support comment-directives in HTML template',
category: 'System',
recommended: 'base'
},
schema: [
{
type: 'object',
properties: {
reportUnusedDisableDirectives: {
type: 'boolean'
}
},
additionalProperties: false
}
],
messages: {
unused: 'Unused {{kind}} directive (no problems were reported).',
unusedRule: "Unused {{kind}} directive (no problems were reported from '{{rule}}').",
unusedEnable: 'Unused {{kind}} directive (reporting is not suppressed).',
unusedEnableRule: "Unused {{kind}} directive (reporting from '{{rule}}' is not suppressed)."
},
type: 'problem'
},
create(context) {
const shared = getShared(context.filename);
if (!shared)
return {};
const options = context.options[0] || {};
const reportUnusedDisableDirectives = Boolean(options.reportUnusedDisableDirectives);
const directives = shared.newCommentDirectives({
ruleId: 'svelte/comment-directive',
reportUnusedDisableDirectives
});
const sourceCode = context.sourceCode;
/**
* Parse a given comment.
*/
function parse(pattern, comment) {
const text = stripDirectiveComment(comment.value);
const match = pattern.exec(text);
if (match == null) {
return null;
}
const type = match[1];
const rules = [];
const rulesRe = /([^\s,]+)[\s,]*/g;
let startIndex = match[0].length;
rulesRe.lastIndex = startIndex;
let res;
while ((res = rulesRe.exec(text))) {
const ruleId = res[1].trim();
const commentStart = comment.range[0] + 4; /* <!-- */
const start = sourceCode.getLocFromIndex(commentStart + startIndex);
const end = sourceCode.getLocFromIndex(commentStart + startIndex + ruleId.length);
rules.push({
ruleId,
loc: {
start,
end
}
});
startIndex = rulesRe.lastIndex;
}
return { type, rules };
}
/**
* Process a given comment token.
* If the comment is `eslint-disable` or `eslint-enable` then it reports the comment.
*/
function processBlock(directives, comment) {
const parsed = parse(COMMENT_DIRECTIVE_B, comment);
if (parsed != null) {
if (parsed.type === 'eslint-disable') {
if (parsed.rules.length) {
for (const rule of parsed.rules) {
if (reportUnusedDisableDirectives) {
context.report({
loc: rule.loc,
messageId: 'unusedRule',
data: { rule: rule.ruleId, kind: parsed.type }
});
}
directives.disableBlock(comment.loc.end, rule.ruleId, {
loc: rule.loc.start
});
}
}
else {
if (reportUnusedDisableDirectives) {
context.report({
loc: comment.loc,
messageId: 'unused',
data: { kind: parsed.type }
});
}
directives.disableBlock(comment.loc.end, ALL_RULES, {
loc: comment.loc.start
});
}
}
else if (parsed.rules.length) {
for (const rule of parsed.rules) {
if (reportUnusedDisableDirectives) {
context.report({
loc: rule.loc,
messageId: 'unusedEnableRule',
data: { rule: rule.ruleId, kind: parsed.type }
});
}
directives.enableBlock(comment.loc.start, rule.ruleId, {
loc: rule.loc.start
});
}
}
else {
if (reportUnusedDisableDirectives) {
context.report({
loc: comment.loc,
messageId: 'unusedEnable',
data: { kind: parsed.type }
});
}
directives.enableBlock(comment.loc.start, ALL_RULES, {
loc: comment.loc.start
});
}
}
}
/**
* Process a given comment token.
* If the comment is `eslint-disable-line` or `eslint-disable-next-line` then it reports the comment.
*/
function processLine(directives, comment) {
const parsed = parse(COMMENT_DIRECTIVE_L, comment);
if (parsed != null && comment.loc.start.line === comment.loc.end.line) {
const line = comment.loc.start.line + (parsed.type === 'eslint-disable-line' ? 0 : 1);
if (parsed.rules.length) {
for (const rule of parsed.rules) {
if (reportUnusedDisableDirectives) {
context.report({
loc: rule.loc,
messageId: 'unusedRule',
data: { rule: rule.ruleId, kind: parsed.type }
});
}
directives.disableLine(line, rule.ruleId, {
loc: rule.loc.start
});
}
}
else {
if (reportUnusedDisableDirectives) {
context.report({
loc: comment.loc,
messageId: 'unused',
data: { kind: parsed.type }
});
}
directives.disableLine(line, ALL_RULES, {
loc: comment.loc.start
});
}
}
}
return {
SvelteHTMLComment(node) {
processBlock(directives, node);
processLine(directives, node);
},
SvelteScriptElement(node) {
directives.enableBlock(node.startTag.loc.end, ALL_RULES, {
loc: node.loc.start
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,287 @@
import { findClassesInAttribute } from '../utils/ast-utils.js';
import { extractExpressionPrefixLiteral, extractExpressionSuffixLiteral } from '../utils/expression-affixes.js';
import { createRule } from '../utils/index.js';
import { ElementOccurenceCount, elementOccurrenceCount } from '../utils/element-occurences.js';
export default createRule('consistent-selector-style', {
meta: {
docs: {
description: 'enforce a consistent style for CSS selectors',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: false
},
schema: [
{
type: 'object',
properties: {
checkGlobal: {
type: 'boolean'
},
style: {
type: 'array',
items: {
enum: ['class', 'id', 'type']
},
maxItems: 3,
uniqueItems: true
}
},
additionalProperties: false
}
],
messages: {
classShouldBeId: 'Selector should select by ID instead of class',
classShouldBeType: 'Selector should select by element type instead of class',
idShouldBeClass: 'Selector should select by class instead of ID',
idShouldBeType: 'Selector should select by element type instead of ID',
typeShouldBeClass: 'Selector should select by class instead of element type',
typeShouldBeId: 'Selector should select by ID instead of element type'
},
type: 'suggestion'
},
create(context) {
const sourceCode = context.sourceCode;
if (!sourceCode.parserServices.isSvelte ||
sourceCode.parserServices.getStyleSelectorAST === undefined ||
sourceCode.parserServices.styleSelectorNodeLoc === undefined) {
return {};
}
const getStyleSelectorAST = sourceCode.parserServices.getStyleSelectorAST;
const styleSelectorNodeLoc = sourceCode.parserServices.styleSelectorNodeLoc;
const checkGlobal = context.options[0]?.checkGlobal ?? false;
const style = context.options[0]?.style ?? ['type', 'id', 'class'];
const whitelistedClasses = [];
const selections = {
class: {
exact: new Map(),
affixes: new Map(),
universalSelector: false
},
id: {
exact: new Map(),
affixes: new Map(),
universalSelector: false
},
type: new Map()
};
/**
* Checks selectors in a given PostCSS node
*/
function checkSelectorsInPostCSSNode(node) {
if (node.type === 'rule') {
checkSelector(getStyleSelectorAST(node));
}
if ((node.type === 'root' ||
(node.type === 'rule' && (node.selector !== ':global' || checkGlobal)) ||
node.type === 'atrule') &&
node.nodes !== undefined) {
node.nodes.flatMap((node) => checkSelectorsInPostCSSNode(node));
}
}
/**
* Checks an individual selector
*/
function checkSelector(node) {
if (node.type === 'class') {
checkClassSelector(node);
}
if (node.type === 'id') {
checkIdSelector(node);
}
if (node.type === 'tag') {
checkTypeSelector(node);
}
if ((node.type === 'pseudo' && (node.value !== ':global' || checkGlobal)) ||
node.type === 'root' ||
node.type === 'selector') {
node.nodes.flatMap((node) => checkSelector(node));
}
}
/**
* Checks a class selector
*/
function checkClassSelector(node) {
if (selections.class.universalSelector || whitelistedClasses.includes(node.value)) {
return;
}
const selection = matchSelection(selections.class, node.value);
for (const styleValue of style) {
if (styleValue === 'class') {
return;
}
if (styleValue === 'id' && canUseIdSelector(selection.map(([elem]) => elem))) {
context.report({
messageId: 'classShouldBeId',
loc: styleSelectorNodeLoc(node)
});
return;
}
if (styleValue === 'type' && canUseTypeSelector(selection, selections.type)) {
context.report({
messageId: 'classShouldBeType',
loc: styleSelectorNodeLoc(node)
});
return;
}
}
}
/**
* Checks an ID selector
*/
function checkIdSelector(node) {
if (selections.id.universalSelector) {
return;
}
const selection = matchSelection(selections.id, node.value);
for (const styleValue of style) {
if (styleValue === 'class') {
context.report({
messageId: 'idShouldBeClass',
loc: styleSelectorNodeLoc(node)
});
return;
}
if (styleValue === 'id') {
return;
}
if (styleValue === 'type' && canUseTypeSelector(selection, selections.type)) {
context.report({
messageId: 'idShouldBeType',
loc: styleSelectorNodeLoc(node)
});
return;
}
}
}
/**
* Checks a type selector
*/
function checkTypeSelector(node) {
const selection = selections.type.get(node.value) ?? [];
for (const styleValue of style) {
if (styleValue === 'class') {
context.report({
messageId: 'typeShouldBeClass',
loc: styleSelectorNodeLoc(node)
});
return;
}
if (styleValue === 'id' && canUseIdSelector(selection)) {
context.report({
messageId: 'typeShouldBeId',
loc: styleSelectorNodeLoc(node)
});
return;
}
if (styleValue === 'type') {
return;
}
}
}
return {
SvelteElement(node) {
if (node.kind !== 'html') {
return;
}
addToArrayMap(selections.type, node.name.name, node);
for (const attribute of node.startTag.attributes) {
if (attribute.type === 'SvelteDirective' && attribute.kind === 'Class') {
whitelistedClasses.push(attribute.key.name.name);
}
for (const className of findClassesInAttribute(attribute)) {
addToArrayMap(selections.class.exact, className, node);
}
if (attribute.type !== 'SvelteAttribute') {
continue;
}
for (const value of attribute.value) {
if (attribute.key.name === 'class' && value.type === 'SvelteMustacheTag') {
const prefix = extractExpressionPrefixLiteral(context, value.expression);
const suffix = extractExpressionSuffixLiteral(context, value.expression);
if (prefix === null && suffix === null) {
selections.class.universalSelector = true;
}
else {
addToArrayMap(selections.class.affixes, [prefix, suffix], node);
}
}
if (attribute.key.name === 'id') {
if (value.type === 'SvelteLiteral') {
addToArrayMap(selections.id.exact, value.value, node);
}
else if (value.type === 'SvelteMustacheTag') {
const prefix = extractExpressionPrefixLiteral(context, value.expression);
const suffix = extractExpressionSuffixLiteral(context, value.expression);
if (prefix === null && suffix === null) {
selections.id.universalSelector = true;
}
else {
addToArrayMap(selections.id.affixes, [prefix, suffix], node);
}
}
}
}
}
},
'Program:exit'() {
const styleContext = sourceCode.parserServices.getStyleContext();
if (styleContext.status !== 'success' ||
sourceCode.parserServices.getStyleSelectorAST === undefined) {
return;
}
checkSelectorsInPostCSSNode(styleContext.sourceAst);
}
};
}
});
/**
* Helper function to add a value to a Map of arrays
*/
function addToArrayMap(map, key, value) {
map.set(key, (map.get(key) ?? []).concat(value));
}
/**
* Finds all nodes in selections that could be matched by key
*/
function matchSelection(selections, key) {
const selection = (selections.exact.get(key) ?? []).map((elem) => [elem, true]);
selections.affixes.forEach((nodes, [prefix, suffix]) => {
if ((prefix === null || key.startsWith(prefix)) && (suffix === null || key.endsWith(suffix))) {
selection.push(...nodes.map((elem) => [elem, false]));
}
});
return selection;
}
/**
* Checks whether a given selection could be obtained using an ID selector
*/
function canUseIdSelector(selection) {
return (selection.length === 0 ||
(selection.length === 1 &&
elementOccurrenceCount(selection[0]) !== ElementOccurenceCount.ZeroToInf));
}
/**
* Checks whether a given selection could be obtained using a type selector
*/
function canUseTypeSelector(selection, typeSelections) {
const types = new Set(selection.map(([node]) => node.name.name));
if (types.size > 1) {
return false;
}
if (types.size < 1) {
return true;
}
if (selection.some(([elem, exact]) => !exact && elementOccurrenceCount(elem) === ElementOccurenceCount.ZeroToInf)) {
return false;
}
const type = [...types][0];
const typeSelection = typeSelections.get(type);
return (typeSelection !== undefined &&
arrayEquals(typeSelection, selection.map(([elem]) => elem)));
}
/**
* Compares two arrays for item equality
*/
function arrayEquals(array1, array2) {
return array1.length === array2.length && array1.every((e) => array2.includes(e));
}

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,121 @@
import { createRule } from '../utils/index.js';
import { extractStoreReferences } from './reference-helpers/svelte-store.js';
import { findVariableForReplacement } from '../utils/ast-utils.js';
function createFixer(node, variable, name) {
return function* fix(fixer) {
yield fixer.replaceText(node, name);
if (variable) {
for (const ref of variable.references) {
yield fixer.replaceText(ref.identifier, name);
}
}
};
}
export default createRule('derived-has-same-inputs-outputs', {
meta: {
docs: {
description: 'derived store should use same variable names between values and callback',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: false
},
hasSuggestions: true,
schema: [],
messages: {
unexpected: "The argument name should be '{{name}}'.",
renameParam: 'Rename the parameter from {{oldName}} to {{newName}}.'
},
type: 'suggestion'
},
create(context) {
/** check node type */
function isIdentifierOrArrayExpression(node) {
return ['Identifier', 'ArrayExpression'].includes(node.type);
}
/** check node type */
function isFunctionExpression(node) {
return ['ArrowFunctionExpression', 'FunctionExpression'].includes(node.type);
}
/**
* Check for identifier type.
* e.g. derived(a, ($a) => {});
*/
function checkIdentifier(context, args, fn) {
const fnParam = fn.params[0];
if (fnParam.type !== 'Identifier')
return;
const expectedName = `$${args.name}`;
if (expectedName !== fnParam.name) {
const { hasConflict, variable } = findVariableForReplacement(context, fn.body, fnParam.name, expectedName);
context.report({
node: fn,
loc: fnParam.loc,
messageId: 'unexpected',
data: { name: expectedName },
suggest: hasConflict
? undefined
: [
{
messageId: 'renameParam',
data: { oldName: fnParam.name, newName: expectedName },
fix: createFixer(fnParam, variable, expectedName)
}
]
});
}
}
/**
* Check for array type.
* e.g. derived([ a, b ], ([ $a, $b ]) => {})
*/
function checkArrayExpression(context, args, fn) {
const fnParam = fn.params[0];
if (fnParam.type !== 'ArrayPattern')
return;
const argNames = args.elements.map((element) => {
return element && element.type === 'Identifier' ? element.name : null;
});
fnParam.elements.forEach((element, index) => {
const argName = argNames[index];
if (element && element.type === 'Identifier' && argName) {
const expectedName = `$${argName}`;
if (expectedName !== element.name) {
const { hasConflict, variable } = findVariableForReplacement(context, fn.body, element.name, expectedName);
context.report({
node: fn,
loc: element.loc,
messageId: 'unexpected',
data: { name: expectedName },
suggest: hasConflict
? undefined
: [
{
messageId: 'renameParam',
data: { oldName: element.name, newName: expectedName },
fix: createFixer(element, variable, expectedName)
}
]
});
}
}
});
}
return {
Program() {
for (const { node } of extractStoreReferences(context, ['derived'])) {
const [args, fn] = node.arguments;
if (!args || !isIdentifierOrArrayExpression(args))
continue;
if (!fn || !isFunctionExpression(fn))
continue;
if (!fn.params || fn.params.length === 0)
continue;
if (args.type === 'Identifier')
checkIdentifier(context, args, fn);
else
checkArrayExpression(context, args, fn);
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,63 @@
import { createRule } from '../utils/index.js';
import { getLangValue } from '../utils/ast-utils.js';
const SLOTS_TYPE_NAME = '$$Slots';
export default createRule('experimental-require-slot-types', {
meta: {
docs: {
description: 'require slot type declaration using the `$$Slots` interface',
category: 'Experimental',
recommended: false
},
schema: [],
messages: {
missingSlotsInterface: `The component must define the $$Slots interface.`
},
type: 'suggestion',
conditions: [
{
svelteVersions: ['3/4']
},
{
svelteVersions: ['5'],
runes: [false, 'undetermined']
}
]
},
create(context) {
let isTs = false;
let hasSlot = false;
let hasDeclaredSlots = false;
return {
SvelteScriptElement(node) {
const lang = getLangValue(node)?.toLowerCase();
isTs = lang === 'ts' || lang === 'typescript';
},
SvelteElement(node) {
if (node.name.type === 'SvelteName' && node.name.name === 'slot') {
hasSlot = true;
}
},
TSInterfaceDeclaration(node) {
if (node.id.name === SLOTS_TYPE_NAME) {
hasDeclaredSlots = true;
}
},
TSTypeAliasDeclaration(node) {
if (node.id.name === SLOTS_TYPE_NAME) {
hasDeclaredSlots = true;
}
},
'Program:exit'() {
if (isTs && hasSlot && !hasDeclaredSlots) {
context.report({
loc: {
line: 1,
column: 1
},
messageId: 'missingSlotsInterface'
});
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,54 @@
import { createRule } from '../utils/index.js';
import { findAttribute, getLangValue } from '../utils/ast-utils.js';
const EVENTS_TYPE_NAME = '$$Events';
export default createRule('experimental-require-strict-events', {
meta: {
docs: {
description: 'require the strictEvents attribute on `<script>` tags',
category: 'Experimental',
recommended: false
},
schema: [],
messages: {
missingStrictEvents: `The component must have the strictEvents attribute on its <script> tag or it must define the $$Events interface.`
},
type: 'suggestion',
conditions: [
{
svelteVersions: ['3/4']
}
]
},
create(context) {
let isTs = false;
let hasAttribute = false;
let hasDeclaredEvents = false;
let scriptNode;
return {
SvelteScriptElement(node) {
const lang = getLangValue(node)?.toLowerCase();
isTs = lang === 'ts' || lang === 'typescript';
hasAttribute = findAttribute(node, 'strictEvents') !== null;
scriptNode = node;
},
TSInterfaceDeclaration(node) {
if (node.id.name === EVENTS_TYPE_NAME) {
hasDeclaredEvents = true;
}
},
TSTypeAliasDeclaration(node) {
if (node.id.name === EVENTS_TYPE_NAME) {
hasDeclaredEvents = true;
}
},
'Program:exit'() {
if (isTs && !hasAttribute && !hasDeclaredEvents) {
context.report({
node: scriptNode,
messageId: 'missingStrictEvents'
});
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,65 @@
import { createRule } from '../utils/index.js';
export default createRule('first-attribute-linebreak', {
meta: {
docs: {
description: 'enforce the location of first attribute',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: true
},
fixable: 'whitespace',
schema: [
{
type: 'object',
properties: {
multiline: { enum: ['below', 'beside'] },
singleline: { enum: ['below', 'beside'] }
},
additionalProperties: false
}
],
messages: {
expected: 'Expected a linebreak before this attribute.',
unexpected: 'Expected no linebreak before this attribute.'
},
type: 'layout'
},
create(context) {
const multiline = context.options[0]?.multiline || 'below';
const singleline = context.options[0]?.singleline || 'beside';
const sourceCode = context.sourceCode;
/**
* Report attribute
*/
function report(firstAttribute, location) {
context.report({
node: firstAttribute,
messageId: location === 'beside' ? 'unexpected' : 'expected',
fix(fixer) {
const prevToken = sourceCode.getTokenBefore(firstAttribute, {
includeComments: true
});
return fixer.replaceTextRange([prevToken.range[1], firstAttribute.range[0]], location === 'beside' ? ' ' : '\n');
}
});
}
return {
SvelteStartTag(node) {
const firstAttribute = node.attributes[0];
if (!firstAttribute)
return;
const lastAttribute = node.attributes[node.attributes.length - 1];
const location = firstAttribute.loc.start.line === lastAttribute.loc.end.line ? singleline : multiline;
if (location === 'beside') {
if (node.parent.name.loc.end.line === firstAttribute.loc.start.line) {
return;
}
}
else if (node.parent.name.loc.end.line < firstAttribute.loc.start.line) {
return;
}
report(firstAttribute, location);
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,115 @@
import { createRule } from '../utils/index.js';
function getPhrase(lineBreaks) {
switch (lineBreaks) {
case 0: {
return 'no line breaks';
}
case 1: {
return '1 line break';
}
default: {
return `${lineBreaks} line breaks`;
}
}
}
function getExpectedLineBreaks(node, options, type) {
const isSelfClosingTag = node.type === 'SvelteStartTag' && node.selfClosing;
if (isSelfClosingTag && options.selfClosingTag && options.selfClosingTag[type]) {
return options.selfClosingTag[type] === 'always' ? 1 : 0;
}
return options[type] === 'always' ? 1 : 0;
}
function getSelfClosingData(sourceCode, node, options) {
const tokens = sourceCode.getTokens(node);
const closingToken = tokens[tokens.length - 2];
if (closingToken.value !== '/') {
return null;
}
const prevToken = sourceCode.getTokenBefore(closingToken);
const type = node.loc.start.line === prevToken.loc.end.line ? 'singleline' : 'multiline';
const expectedLineBreaks = getExpectedLineBreaks(node, options, type);
const actualLineBreaks = closingToken.loc.start.line - prevToken.loc.end.line;
return { actualLineBreaks, expectedLineBreaks, startToken: prevToken, endToken: closingToken };
}
function getNodeData(sourceCode, node, options) {
const closingToken = sourceCode.getLastToken(node);
if (closingToken.value !== '>') {
return null;
}
const prevToken = sourceCode.getTokenBefore(closingToken);
const type = node.loc.start.line === prevToken.loc.end.line ? 'singleline' : 'multiline';
const expectedLineBreaks = getExpectedLineBreaks(node, options, type);
const actualLineBreaks = closingToken.loc.start.line - prevToken.loc.end.line;
return { actualLineBreaks, expectedLineBreaks, startToken: prevToken, endToken: closingToken };
}
export default createRule('html-closing-bracket-new-line', {
meta: {
docs: {
description: "Require or disallow a line break before tag's closing brackets",
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: true
},
schema: [
{
type: 'object',
properties: {
singleline: { enum: ['always', 'never'] },
multiline: { enum: ['always', 'never'] },
selfClosingTag: {
type: 'object',
properties: {
singleline: { enum: ['always', 'never'] },
multiline: { enum: ['always', 'never'] }
},
additionalProperties: false,
minProperties: 1
}
},
additionalProperties: false
}
],
messages: {
expectedBeforeClosingBracket: 'Expected {{expected}} before closing bracket, but {{actual}} found.'
},
fixable: 'code',
type: 'suggestion'
},
create(context) {
const options = { ...(context.options[0] ?? {}) };
options.singleline ?? (options.singleline = 'never');
options.multiline ?? (options.multiline = 'always');
const sourceCode = context.sourceCode;
return {
'SvelteStartTag, SvelteEndTag'(node) {
const data = node.type === 'SvelteStartTag' && node.selfClosing
? getSelfClosingData(sourceCode, node, options)
: getNodeData(sourceCode, node, options);
if (!data) {
return;
}
const { actualLineBreaks, expectedLineBreaks, startToken, endToken } = data;
if (actualLineBreaks !== expectedLineBreaks) {
// For SvelteEndTag, does not make sense to add a line break, so we only fix if there are extra line breaks
if (node.type === 'SvelteEndTag' && expectedLineBreaks !== 0) {
return;
}
context.report({
node,
loc: { start: startToken.loc.end, end: endToken.loc.start },
messageId: 'expectedBeforeClosingBracket',
data: {
expected: getPhrase(expectedLineBreaks),
actual: getPhrase(actualLineBreaks)
},
fix(fixer) {
const range = [startToken.range[1], endToken.range[0]];
const text = '\n'.repeat(expectedLineBreaks);
return fixer.replaceTextRange(range, text);
}
});
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,96 @@
import { createRule } from '../utils/index.js';
export default createRule('html-closing-bracket-spacing', {
meta: {
docs: {
description: "require or disallow a space before tag's closing brackets",
category: 'Stylistic Issues',
conflictWithPrettier: true,
recommended: false
},
schema: [
{
type: 'object',
properties: {
startTag: {
enum: ['always', 'never', 'ignore']
},
endTag: {
enum: ['always', 'never', 'ignore']
},
selfClosingTag: {
enum: ['always', 'never', 'ignore']
}
},
additionalProperties: false
}
],
messages: {
expectedSpace: "Expected space before '>', but not found.",
unexpectedSpace: "Expected no space before '>', but found."
},
fixable: 'whitespace',
type: 'layout'
},
create(ctx) {
const options = {
startTag: 'never',
endTag: 'never',
selfClosingTag: 'always',
...ctx.options[0]
};
const src = ctx.sourceCode;
/**
* Returns true if string contains newline characters
*/
function containsNewline(string) {
return string.includes('\n');
}
/**
* Report
*/
function report(node, shouldHave) {
const tagSrc = src.getText(node);
const match = /(\s*)\/?>$/.exec(tagSrc);
const end = node.range[1];
const start = node.range[1] - match[0].length;
const loc = {
start: src.getLocFromIndex(start),
end: src.getLocFromIndex(end)
};
ctx.report({
loc,
messageId: shouldHave ? 'expectedSpace' : 'unexpectedSpace',
*fix(fixer) {
if (shouldHave) {
yield fixer.insertTextBeforeRange([start, end], ' ');
}
else {
const spaces = match[1];
yield fixer.removeRange([start, start + spaces.length]);
}
}
});
}
return {
'SvelteStartTag, SvelteEndTag'(node) {
const tagType = node.type === 'SvelteEndTag'
? 'endTag'
: node.selfClosing
? 'selfClosingTag'
: 'startTag';
if (options[tagType] === 'ignore')
return;
const tagSrc = src.getText(node);
const match = /(\s*)\/?>$/.exec(tagSrc);
if (containsNewline(match[1]))
return;
if (options[tagType] === 'always' && !match[1]) {
report(node, true);
}
else if (options[tagType] === 'never' && match[1]) {
report(node, false);
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,165 @@
import { createRule } from '../utils/index.js';
import { getMustacheTokens } from '../utils/ast-utils.js';
import { getAttributeValueQuoteAndRange } from '../utils/ast-utils.js';
const QUOTE_CHARS = {
double: '"',
single: "'"
};
const QUOTE_NAMES = {
double: 'double quotes',
single: 'single quotes',
unquoted: 'unquoted'
};
export default createRule('html-quotes', {
meta: {
docs: {
description: 'enforce quotes style of HTML attributes',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: true
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
prefer: { enum: ['double', 'single'] },
dynamic: {
type: 'object',
properties: {
quoted: { type: 'boolean' },
avoidInvalidUnquotedInHTML: { type: 'boolean' }
},
additionalProperties: false
}
},
additionalProperties: false
}
],
messages: {
expectedEnclosed: 'Expected to be enclosed by quotes.',
expectedEnclosedBy: 'Expected to be enclosed by {{kind}}.',
unexpectedEnclosed: 'Unexpected to be enclosed by any quotes.'
},
type: 'layout' // "problem",
},
create(context) {
const sourceCode = context.sourceCode;
const preferQuote = context.options[0]?.prefer ?? 'double';
const dynamicQuote = context.options[0]?.dynamic?.quoted ? preferQuote : 'unquoted';
const avoidInvalidUnquotedInHTML = Boolean(context.options[0]?.dynamic?.avoidInvalidUnquotedInHTML);
/** Checks whether the given text can remove quotes in HTML. */
function canBeUnquotedInHTML(text) {
return !/[\s"'<=>`]/u.test(text);
}
/** Verify quote */
function verifyQuote(prefer, quoteAndRange) {
if (!quoteAndRange) {
// invalid
return;
}
if (quoteAndRange.quote === prefer) {
// valid
return;
}
let messageId;
let expectedQuote = prefer;
if (quoteAndRange.quote !== 'unquoted') {
if (expectedQuote === 'unquoted') {
messageId = 'unexpectedEnclosed';
}
else {
const contentText = sourceCode.text.slice(quoteAndRange.range[0] + 1, quoteAndRange.range[1] - 1);
const needEscape = contentText.includes(QUOTE_CHARS[expectedQuote]);
if (needEscape) {
// avoid escape
return;
}
messageId = 'expectedEnclosedBy';
}
}
else {
const contentText = sourceCode.text.slice(...quoteAndRange.range);
const needEscapeDoubleQuote = contentText.includes('"');
const needEscapeSingleQuote = contentText.includes("'");
if (needEscapeDoubleQuote && needEscapeSingleQuote) {
// avoid escape
return;
}
if (needEscapeDoubleQuote && expectedQuote === 'double') {
expectedQuote = 'single';
messageId = 'expectedEnclosed';
}
else if (needEscapeSingleQuote && expectedQuote === 'single') {
expectedQuote = 'double';
messageId = 'expectedEnclosed';
}
else {
messageId = 'expectedEnclosedBy';
}
}
context.report({
loc: {
start: sourceCode.getLocFromIndex(quoteAndRange.range[0]),
end: sourceCode.getLocFromIndex(quoteAndRange.range[1])
},
messageId,
data: { kind: QUOTE_NAMES[expectedQuote] },
*fix(fixer) {
if (expectedQuote !== 'unquoted') {
yield fixer.insertTextBeforeRange([quoteAndRange.range[0], quoteAndRange.range[0]], QUOTE_CHARS[expectedQuote]);
}
if (quoteAndRange.quote !== 'unquoted') {
yield fixer.removeRange([quoteAndRange.range[0], quoteAndRange.range[0] + 1]);
yield fixer.removeRange([quoteAndRange.range[1] - 1, quoteAndRange.range[1]]);
}
if (expectedQuote !== 'unquoted') {
yield fixer.insertTextAfterRange([quoteAndRange.range[1], quoteAndRange.range[1]], QUOTE_CHARS[expectedQuote]);
}
}
});
}
/** Verify for standard attribute */
function verifyForValues(attr) {
const quoteAndRange = getAttributeValueQuoteAndRange(attr, sourceCode);
verifyQuote(preferQuote, quoteAndRange);
}
/** Verify for dynamic attribute */
function verifyForDynamicMustacheTag(attr, valueNode) {
const quoteAndRange = getAttributeValueQuoteAndRange(attr, sourceCode);
const text = sourceCode.text.slice(...valueNode.range);
verifyQuote(avoidInvalidUnquotedInHTML && !canBeUnquotedInHTML(text) ? preferQuote : dynamicQuote, quoteAndRange);
}
/** Verify for directive value */
function verifyForDirective(attr) {
const mustacheTokens = getMustacheTokens(attr, sourceCode);
if (!mustacheTokens) {
return;
}
const quoteAndRange = getAttributeValueQuoteAndRange(attr, sourceCode);
const text = sourceCode.text.slice(mustacheTokens.openToken.range[0], mustacheTokens.closeToken.range[1]);
verifyQuote(avoidInvalidUnquotedInHTML && !canBeUnquotedInHTML(text) ? preferQuote : dynamicQuote, quoteAndRange);
}
return {
'SvelteAttribute, SvelteStyleDirective'(node) {
if (node.value.length === 1 && node.value[0].type === 'SvelteMustacheTag') {
verifyForDynamicMustacheTag(node, node.value[0]);
}
else if (node.value.length >= 1) {
verifyForValues(node);
}
},
'SvelteDirective, SvelteSpecialDirective'(node) {
if (node.expression == null) {
return;
}
if (node.key.range[0] <= node.expression.range[0] &&
node.expression.range[1] <= node.key.range[1]) {
// shorthand
return;
}
verifyForDirective(node);
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,193 @@
import { createRule } from '../utils/index.js';
import { getNodeName, isVoidHtmlElement, isSvgElement, isMathMLElement } from '../utils/ast-utils.js';
const TYPE_MESSAGES = {
normal: 'HTML elements',
void: 'HTML void elements',
svg: 'SVG elements',
math: 'MathML elements',
component: 'Svelte custom components',
svelte: 'Svelte special elements'
};
export default createRule('html-self-closing', {
meta: {
docs: {
description: 'enforce self-closing style',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: true
},
type: 'layout',
fixable: 'code',
messages: {
requireClosing: 'Require self-closing on {{type}}.',
disallowClosing: 'Disallow self-closing on {{type}}.'
},
schema: [
{
anyOf: [
{
properties: {
void: {
enum: ['never', 'always', 'ignore']
},
normal: {
enum: ['never', 'always', 'ignore']
},
svg: {
enum: ['never', 'always', 'ignore']
},
math: {
enum: ['never', 'always', 'ignore']
},
component: {
enum: ['never', 'always', 'ignore']
},
svelte: {
enum: ['never', 'always', 'ignore']
}
},
additionalProperties: false
},
{
enum: ['all', 'html', 'none']
}
]
}
]
},
create(context) {
// default
let options = {
void: 'always',
normal: 'never',
svg: 'always',
math: 'never',
component: 'always',
svelte: 'always'
};
const option = context.options?.[0];
switch (option) {
case 'all':
options = {
void: 'always',
normal: 'always',
svg: 'always',
math: 'always',
component: 'always',
svelte: 'always'
};
break;
case 'html':
options = {
void: 'always',
normal: 'never',
svg: 'always',
math: 'never',
component: 'never',
svelte: 'always'
};
break;
case 'none':
options = {
void: 'never',
normal: 'never',
svg: 'never',
math: 'never',
component: 'never',
svelte: 'never'
};
break;
default:
if (typeof option !== 'object' || option === null)
break;
options = {
...options,
...option
};
break;
}
/**
* Get SvelteElement type.
* If element is custom component "component" is returned
* If element is svelte special element such as svelte:self "svelte" is returned
* If element is void element "void" is returned
* otherwise "normal" is returned
*/
function getElementType(node) {
if (node.kind === 'component')
return 'component';
if (node.kind === 'special')
return 'svelte';
if (isVoidHtmlElement(node))
return 'void';
if (isSvgElement(node))
return 'svg';
if (isMathMLElement(node))
return 'math';
return 'normal';
}
/**
* Returns true if element has no children, or has only whitespace text
*/
function isElementEmpty(node) {
if (node.children.length <= 0)
return true;
for (const child of node.children) {
if (child.type !== 'SvelteText')
return false;
if (!/^\s*$/.test(child.value))
return false;
}
return true;
}
/**
* Report
*/
function report(node, shouldBeClosed) {
const elementType = getElementType(node);
context.report({
node,
loc: {
start: context.sourceCode.getLocFromIndex(node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1)),
end: node.loc.end
},
messageId: shouldBeClosed ? 'requireClosing' : 'disallowClosing',
data: {
type: TYPE_MESSAGES[elementType]
},
*fix(fixer) {
if (shouldBeClosed) {
for (const child of node.children) {
yield fixer.removeRange(child.range);
}
yield fixer.insertTextBeforeRange([node.startTag.range[1] - 1, node.startTag.range[1]], '/');
if (node.endTag)
yield fixer.removeRange(node.endTag.range);
}
else {
yield fixer.removeRange([node.startTag.range[1] - 2, node.startTag.range[1] - 1]);
if (!isVoidHtmlElement(node))
yield fixer.insertTextAfter(node, `</${getNodeName(node)}>`);
}
}
});
}
return {
SvelteElement(node) {
if (!isElementEmpty(node))
return;
const elementType = getElementType(node);
const elementTypeOptions = options[elementType];
if (elementTypeOptions === 'ignore')
return;
const shouldBeClosed = elementTypeOptions === 'always';
if (shouldBeClosed && !node.startTag.selfClosing) {
report(node, true);
}
else if (!shouldBeClosed && node.startTag.selfClosing) {
report(node, false);
}
}
};
}
});

View File

@@ -0,0 +1,12 @@
import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
type AnyToken = AST.Token | AST.Comment;
/**
* Check whether the given token is a whitespace.
*/
export declare function isWhitespace(token: AnyToken | TSESTree.Comment | null | undefined): boolean;
/**
* Check whether the given token is a not whitespace.
*/
export declare function isNotWhitespace(token: AnyToken | TSESTree.Comment | null | undefined): boolean;
export {};

View File

@@ -0,0 +1,16 @@
/**
* Check whether the given token is a whitespace.
*/
export function isWhitespace(token) {
return (token != null &&
((token.type === 'HTMLText' && !token.value.trim()) ||
(token.type === 'JSXText' && !token.value.trim())));
}
/**
* Check whether the given token is a not whitespace.
*/
export function isNotWhitespace(token) {
return (token != null &&
(token.type !== 'HTMLText' || Boolean(token.value.trim())) &&
(token.type !== 'JSXText' || Boolean(token.value.trim())));
}

View File

@@ -0,0 +1,39 @@
import type { ASTNode, SourceCode } from '../../types.js';
import type { AST } from 'svelte-eslint-parser';
import type { OffsetContext } from './offset-context.js';
export type AnyToken = AST.Token | AST.Comment;
export type MaybeNode = {
type: string;
range: [number, number];
loc: AST.SourceLocation;
};
export type IndentOptions = {
indentChar: ' ' | '\t';
indentScript: boolean;
indentSize: number;
switchCase: number;
alignAttributesVertically: boolean;
ignoredNodes: string[];
};
export type IndentContext = {
sourceCode: SourceCode;
options: IndentOptions;
offsets: OffsetContext;
};
/**
* Get the first and last tokens of the given node.
* If the node is parenthesized, this gets the outermost parentheses.
* If the node have whitespace at the start and the end, they will be skipped.
*/
export declare function getFirstAndLastTokens(sourceCode: SourceCode, node: ASTNode | AnyToken | MaybeNode, borderOffset?: number): {
firstToken: AST.Token;
lastToken: AST.Token;
};
/**
* Check whether the given node or token is the beginning of a line.
*/
export declare function isBeginningOfLine(sourceCode: SourceCode, node: ASTNode | AnyToken | MaybeNode): boolean;
/**
* Check whether the given node is the beginning of element.
*/
export declare function isBeginningOfElement(node: AST.SvelteText): boolean;

View File

@@ -0,0 +1,65 @@
import { isOpeningParenToken, isClosingParenToken } from '@eslint-community/eslint-utils';
import { isNotWhitespace, isWhitespace } from './ast.js';
/**
* Get the first and last tokens of the given node.
* If the node is parenthesized, this gets the outermost parentheses.
* If the node have whitespace at the start and the end, they will be skipped.
*/
export function getFirstAndLastTokens(sourceCode, node, borderOffset = 0) {
let firstToken = sourceCode.getFirstToken(node);
let lastToken = sourceCode.getLastToken(node);
// Get the outermost left parenthesis if it's parenthesized.
let left, right;
while ((left = sourceCode.getTokenBefore(firstToken)) != null &&
(right = sourceCode.getTokenAfter(lastToken)) != null &&
isOpeningParenToken(left) &&
isClosingParenToken(right) &&
borderOffset <= left.range[0]) {
firstToken = left;
lastToken = right;
}
while (isWhitespace(firstToken) && firstToken.range[0] < lastToken.range[0]) {
firstToken = sourceCode.getTokenAfter(firstToken);
}
while (isWhitespace(lastToken) && firstToken.range[0] < lastToken.range[0]) {
lastToken = sourceCode.getTokenBefore(lastToken);
}
return { firstToken, lastToken };
}
/**
* Check whether the given node or token is the beginning of a line.
*/
export function isBeginningOfLine(sourceCode, node) {
const prevToken = sourceCode.getTokenBefore(node, {
includeComments: false,
filter: isNotWhitespace
});
return !prevToken || prevToken.loc.end.line < node.loc.start.line;
}
/**
* Check whether the given node is the beginning of element.
*/
export function isBeginningOfElement(node) {
if (node.parent.type === 'SvelteElement' ||
node.parent.type === 'SvelteAwaitCatchBlock' ||
node.parent.type === 'SvelteAwaitPendingBlock' ||
node.parent.type === 'SvelteAwaitThenBlock' ||
node.parent.type === 'SvelteEachBlock' ||
node.parent.type === 'SvelteElseBlock' ||
node.parent.type === 'SvelteIfBlock' ||
node.parent.type === 'SvelteKeyBlock' ||
node.parent.type === 'SvelteSnippetBlock' ||
node.parent.type === 'SvelteStyleElement') {
return node.parent.children[0] === node;
}
if (node.parent.type === 'Program') {
return node.parent.body[0] === node;
}
return assertNever(node.parent);
}
/**
* Throws an error when invoked.
*/
function assertNever(value) {
throw new Error(`This part of the code should never be reached but ${value} made it through.`);
}

View File

@@ -0,0 +1,11 @@
import type { IndentContext } from './commons.js';
import type { ESNodeListener } from '../../types-for-node.js';
type NodeListener = ESNodeListener;
/**
* Creates AST event handlers for ES nodes.
*
* @param context The rule context.
* @returns AST event handlers.
*/
export declare function defineVisitor(context: IndentContext): NodeListener;
export {};

View File

@@ -0,0 +1,785 @@
import { getFirstAndLastTokens } from './commons.js';
import { isArrowToken, isClosingBraceToken, isClosingBracketToken, isClosingParenToken, isNotClosingParenToken, isNotOpeningParenToken, isNotSemicolonToken, isOpeningBraceToken, isOpeningBracketToken, isOpeningParenToken, isSemicolonToken } from '@eslint-community/eslint-utils';
import { getParent } from '../../utils/ast-utils.js';
/**
* Creates AST event handlers for ES nodes.
*
* @param context The rule context.
* @returns AST event handlers.
*/
export function defineVisitor(context) {
const { sourceCode, offsets, options } = context;
/**
* Find the head of chaining nodes.
*/
function getChainHeadNode(node) {
let target = node;
let parent = getParent(target);
while (parent &&
(parent.type === 'AssignmentExpression' ||
parent.type === 'AssignmentPattern' ||
parent.type === 'BinaryExpression' ||
parent.type === 'LogicalExpression')) {
const prevToken = sourceCode.getTokenBefore(target);
const nextToken = sourceCode.getTokenAfter(target);
if (prevToken &&
isOpeningParenToken(prevToken) &&
nextToken &&
isClosingParenToken(nextToken)) {
// The chain is broken because it is enclosed in parentheses.
break;
}
target = parent;
parent = getParent(target);
}
return target;
}
const visitor = {
Program(node) {
for (const body of node.body) {
if (body.type === 'SvelteText' && !body.value.trim()) {
continue;
}
offsets.setStartOffsetToken(sourceCode.getFirstToken(body), 0);
}
},
ArrayExpression(node) {
const firstToken = sourceCode.getFirstToken(node);
const rightToken = sourceCode.getTokenAfter(node.elements[node.elements.length - 1] || firstToken, { filter: isClosingBracketToken, includeComments: false });
offsets.setOffsetElementList(node.elements, firstToken, rightToken, 1);
},
ArrayPattern(node) {
visitor.ArrayExpression(node);
},
ArrowFunctionExpression(node) {
const [firstToken, secondToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
const leftToken = node.async ? secondToken : firstToken;
const arrowToken = sourceCode.getTokenBefore(node.body, {
filter: isArrowToken,
includeComments: false
});
if (node.async) {
offsets.setOffsetToken(secondToken, 1, firstToken);
}
if (isOpeningParenToken(leftToken)) {
const rightToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || leftToken, { filter: isClosingParenToken, includeComments: false });
offsets.setOffsetElementList(node.params, leftToken, rightToken, 1);
}
offsets.setOffsetToken(arrowToken, 1, firstToken);
const bodyFirstToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyFirstToken, isOpeningBraceToken(bodyFirstToken) ? 0 : 1, firstToken);
},
AssignmentExpression(node) {
const baseNode = getChainHeadNode(node);
const opToken = sourceCode.getTokenAfter(node.left, {
filter: isNotClosingParenToken,
includeComments: false
});
const baseToken = baseNode.type === 'AssignmentExpression' || baseNode.type === 'AssignmentPattern'
? sourceCode.getFirstToken(baseNode)
: getFirstAndLastTokens(sourceCode, baseNode).firstToken;
const leftToken = getFirstAndLastTokens(sourceCode, node.left).firstToken;
const rightToken = getFirstAndLastTokens(sourceCode, node.right).firstToken;
offsets.setOffsetToken([leftToken === baseToken ? null : leftToken, opToken, rightToken], 1, baseToken);
},
AssignmentPattern(node) {
visitor.AssignmentExpression(node);
},
BinaryExpression(node) {
visitor.AssignmentExpression(node);
},
LogicalExpression(node) {
visitor.AssignmentExpression(node);
},
AwaitExpression(node) {
// `await`, `...`, or UnaryOperator
const firstToken = sourceCode.getFirstToken(node);
const nextToken = sourceCode.getTokenAfter(firstToken);
offsets.setOffsetToken(nextToken, 1, firstToken);
},
RestElement(node) {
visitor.AwaitExpression(node);
},
SpreadElement(node) {
visitor.AwaitExpression(node);
},
UnaryExpression(node) {
visitor.AwaitExpression(node);
},
BlockStatement(node) {
offsets.setOffsetElementList(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), 1);
},
ClassBody(node) {
visitor.BlockStatement(node);
},
BreakStatement(node) {
if (node.label) {
const firstToken = sourceCode.getFirstToken(node);
const nextToken = sourceCode.getTokenAfter(firstToken);
offsets.setOffsetToken(nextToken, 1, firstToken);
}
},
ContinueStatement(node) {
visitor.BreakStatement(node);
},
CallExpression(node) {
const typeArguments = node.typeArguments ??
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Support old typescript-eslint
node.typeParameters;
const firstToken = sourceCode.getFirstToken(node);
const leftParenToken = sourceCode.getTokenAfter(typeArguments || node.callee, {
filter: isOpeningParenToken,
includeComments: false
});
const rightParenToken = sourceCode.getLastToken(node);
if (typeArguments) {
offsets.setOffsetToken(sourceCode.getFirstToken(typeArguments), 1, firstToken);
}
for (const optionalToken of sourceCode.getTokensBetween(sourceCode.getLastToken(typeArguments || node.callee), leftParenToken, { filter: isOptionalToken, includeComments: false })) {
offsets.setOffsetToken(optionalToken, 1, firstToken);
}
offsets.setOffsetToken(leftParenToken, 1, firstToken);
offsets.setOffsetElementList(node.arguments, leftParenToken, rightParenToken, 1);
},
CatchClause(node) {
const catchToken = sourceCode.getFirstToken(node);
if (node.param != null) {
const leftParenToken = sourceCode.getTokenBefore(node.param);
const rightParenToken = sourceCode.getTokenAfter(node.param);
offsets.setOffsetToken(leftParenToken, 1, catchToken);
offsets.setOffsetElementList([node.param], leftParenToken, rightParenToken, 1);
}
const bodyToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyToken, 0, catchToken);
},
ClassDeclaration(node) {
const classToken = sourceCode.getFirstToken(node);
if (node.id != null) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.id), 1, classToken);
}
if (node.superClass != null) {
const extendsToken = sourceCode.getTokenBefore(node.superClass);
const superClassToken = sourceCode.getTokenAfter(extendsToken);
offsets.setOffsetToken(extendsToken, 1, classToken);
offsets.setOffsetToken(superClassToken, 1, extendsToken);
}
const bodyToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyToken, 0, classToken);
},
ClassExpression(node) {
visitor.ClassDeclaration(node);
},
ConditionalExpression(node) {
const questionToken = sourceCode.getTokenAfter(node.test, {
filter: isNotClosingParenToken,
includeComments: false
});
const consequentToken = sourceCode.getTokenAfter(questionToken);
const colonToken = sourceCode.getTokenAfter(node.consequent, {
filter: isNotClosingParenToken,
includeComments: false
});
const alternateToken = sourceCode.getTokenAfter(colonToken);
let baseNode = node;
let parent = getParent(baseNode);
while (parent && parent.type === 'ConditionalExpression' && parent.alternate === baseNode) {
baseNode = parent;
parent = getParent(baseNode);
}
const baseToken = sourceCode.getFirstToken(baseNode);
offsets.setOffsetToken([questionToken, colonToken], 1, baseToken);
offsets.setOffsetToken(consequentToken, 1, questionToken);
offsets.setOffsetToken(alternateToken, 1, colonToken);
},
DoWhileStatement(node) {
const doToken = sourceCode.getFirstToken(node);
const whileToken = sourceCode.getTokenAfter(node.body, {
filter: isNotClosingParenToken,
includeComments: false
});
const leftParenToken = sourceCode.getTokenAfter(whileToken);
const rightParenToken = sourceCode.getTokenAfter(node.test);
const bodyFirstToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyFirstToken, isOpeningBraceToken(bodyFirstToken) ? 0 : 1, doToken);
offsets.setOffsetToken(whileToken, 0, doToken);
offsets.setOffsetToken(leftParenToken, 1, whileToken);
offsets.setOffsetElementList([node.test], leftParenToken, rightParenToken, 1);
},
ExportAllDeclaration(node) {
const exportToken = sourceCode.getFirstToken(node);
const tokens = sourceCode.getTokensBetween(exportToken, node.source);
const fromIndex = tokens.findIndex((t) => t.value === 'from');
const fromToken = tokens[fromIndex];
const beforeTokens = tokens.slice(0, fromIndex);
const afterTokens = [...tokens.slice(fromIndex + 1), sourceCode.getFirstToken(node.source)];
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- type bug?
if (!node.exported) {
// export * from "mod"
offsets.setOffsetToken(beforeTokens, 1, exportToken);
}
else {
// export * as foo from "mod"
const asIndex = beforeTokens.findIndex((t) => t.value === 'as');
offsets.setOffsetToken(beforeTokens.slice(0, asIndex), 1, exportToken);
offsets.setOffsetToken(beforeTokens.slice(asIndex), 1, beforeTokens[asIndex - 1]);
}
offsets.setOffsetToken(fromToken, 0, exportToken);
offsets.setOffsetToken(afterTokens, 1, fromToken);
// assertions
const lastToken = sourceCode.getLastToken(node, {
filter: isNotSemicolonToken,
includeComments: false
});
const assertionTokens = sourceCode.getTokensBetween(node.source, lastToken);
if (assertionTokens.length) {
const assertToken = assertionTokens.shift();
offsets.setOffsetToken(assertToken, 0, exportToken);
const assertionOpen = assertionTokens.shift();
if (assertionOpen) {
offsets.setOffsetToken(assertionOpen, 1, assertToken);
offsets.setOffsetElementList(assertionTokens, assertionOpen, lastToken, 1);
}
}
},
ExportDefaultDeclaration(node) {
const exportToken = sourceCode.getFirstToken(node);
const declarationToken = getFirstAndLastTokens(sourceCode, node.declaration).firstToken;
const defaultTokens = sourceCode.getTokensBetween(exportToken, declarationToken);
offsets.setOffsetToken([...defaultTokens, declarationToken], 1, exportToken);
},
ExportNamedDeclaration(node) {
const exportToken = sourceCode.getFirstToken(node);
if (node.declaration) {
// export var foo = 1;
const declarationToken = sourceCode.getFirstToken(node.declaration);
offsets.setOffsetToken(declarationToken, 1, exportToken);
}
else {
const firstSpecifier = node.specifiers[0];
if (!firstSpecifier || firstSpecifier.type === 'ExportSpecifier') {
// export {foo, bar}; or export {foo, bar} from "mod";
const leftBraceTokens = firstSpecifier
? sourceCode.getTokensBetween(exportToken, firstSpecifier)
: [sourceCode.getTokenAfter(exportToken)];
const rightBraceToken = node.source
? sourceCode.getTokenBefore(node.source, {
filter: isClosingBraceToken,
includeComments: false
})
: sourceCode.getLastToken(node, {
filter: isClosingBraceToken,
includeComments: false
});
offsets.setOffsetToken(leftBraceTokens, 0, exportToken);
offsets.setOffsetElementList(node.specifiers, leftBraceTokens[leftBraceTokens.length - 1], rightBraceToken, 1);
if (node.source) {
const [fromToken, ...tokens] = sourceCode.getTokensBetween(rightBraceToken, node.source);
offsets.setOffsetToken(fromToken, 0, exportToken);
offsets.setOffsetToken([...tokens, sourceCode.getFirstToken(node.source)], 1, fromToken);
// assertions
const lastToken = sourceCode.getLastToken(node, {
filter: isNotSemicolonToken,
includeComments: false
});
const assertionTokens = sourceCode.getTokensBetween(node.source, lastToken);
if (assertionTokens.length) {
const assertToken = assertionTokens.shift();
offsets.setOffsetToken(assertToken, 0, exportToken);
const assertionOpen = assertionTokens.shift();
if (assertionOpen) {
offsets.setOffsetToken(assertionOpen, 1, assertToken);
offsets.setOffsetElementList(assertionTokens, assertionOpen, lastToken, 1);
}
}
}
}
else {
// maybe babel-eslint
}
}
},
ExportSpecifier(node) {
const tokens = sourceCode.getTokens(node);
let firstToken = tokens.shift();
if (firstToken.value === 'type') {
const typeToken = firstToken;
firstToken = tokens.shift();
offsets.setOffsetToken(firstToken, 0, typeToken);
}
offsets.setOffsetToken(tokens, 1, firstToken);
},
ForInStatement(node) {
const forToken = sourceCode.getFirstToken(node);
const awaitToken = (node.type === 'ForOfStatement' && node.await && sourceCode.getTokenAfter(forToken)) ||
null;
const leftParenToken = sourceCode.getTokenAfter(awaitToken || forToken);
const leftToken = sourceCode.getFirstToken(node.left);
const inOrOfToken = sourceCode.getTokenAfter(node.left, {
filter: isNotClosingParenToken,
includeComments: false
});
const rightToken = sourceCode.getTokenAfter(inOrOfToken);
const rightParenToken = sourceCode.getTokenBefore(node.body, {
filter: isNotOpeningParenToken,
includeComments: false
});
if (awaitToken != null) {
offsets.setOffsetToken(awaitToken, 0, forToken);
}
offsets.setOffsetToken(leftParenToken, 1, forToken);
offsets.setOffsetToken(leftToken, 1, leftParenToken);
offsets.setOffsetToken([inOrOfToken, rightToken], 1, leftToken);
offsets.setOffsetToken(rightParenToken, 0, leftParenToken);
const bodyFirstToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyFirstToken, isOpeningBraceToken(bodyFirstToken) ? 0 : 1, forToken);
},
ForOfStatement(node) {
visitor.ForInStatement(node);
},
ForStatement(node) {
const forToken = sourceCode.getFirstToken(node);
const leftParenToken = sourceCode.getTokenAfter(forToken);
const rightParenToken = sourceCode.getTokenBefore(node.body, {
filter: isNotOpeningParenToken,
includeComments: false
});
offsets.setOffsetToken(leftParenToken, 1, forToken);
offsets.setOffsetElementList([node.init, node.test, node.update], leftParenToken, rightParenToken, 1);
const bodyFirstToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyFirstToken, isOpeningBraceToken(bodyFirstToken) ? 0 : 1, forToken);
},
FunctionDeclaration(node) {
const firstToken = sourceCode.getFirstToken(node);
const leftParenToken = sourceCode.getTokenBefore(node.params[0] ||
node.returnType ||
sourceCode.getTokenBefore(node.body), {
filter: isOpeningParenToken,
includeComments: false
});
let bodyBaseToken = null;
if (firstToken.type === 'Punctuator') {
// method
bodyBaseToken = sourceCode.getFirstToken(getParent(node));
}
else {
let tokenOffset = 0;
for (const token of sourceCode.getTokensBetween(firstToken, leftParenToken)) {
if (token.value === '<') {
break;
}
if (token.value === '*' || (node.id && token.range[0] === node.id.range[0])) {
tokenOffset = 1;
}
offsets.setOffsetToken(token, tokenOffset, firstToken);
}
bodyBaseToken = firstToken;
}
const rightParenToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || leftParenToken, { filter: isClosingParenToken, includeComments: false });
offsets.setOffsetToken(leftParenToken, 1, bodyBaseToken);
offsets.setOffsetElementList(node.params, leftParenToken, rightParenToken, 1);
const bodyToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyToken, 0, bodyBaseToken);
},
FunctionExpression(node) {
visitor.FunctionDeclaration(node);
},
IfStatement(node) {
const [ifToken, ifLeftParenToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
const ifRightParenToken = sourceCode.getTokenBefore(node.consequent, {
filter: isClosingParenToken,
includeComments: false
});
offsets.setOffsetToken(ifLeftParenToken, 1, ifToken);
offsets.setOffsetToken(ifRightParenToken, 0, ifLeftParenToken);
const consequentFirstToken = sourceCode.getFirstToken(node.consequent);
offsets.setOffsetToken(consequentFirstToken, isOpeningBraceToken(consequentFirstToken) ? 0 : 1, ifToken);
if (node.alternate != null) {
const elseToken = sourceCode.getTokenAfter(node.consequent, {
filter: isNotClosingParenToken,
includeComments: false
});
offsets.setOffsetToken(elseToken, 0, ifToken);
const alternateFirstToken = sourceCode.getFirstToken(node.alternate);
offsets.setOffsetToken(alternateFirstToken, isOpeningBraceToken(alternateFirstToken) ? 0 : 1, elseToken);
}
},
ImportDeclaration(node) {
const importToken = sourceCode.getFirstToken(node);
const tokens = sourceCode.getTokensBetween(importToken, node.source);
const fromIndex = tokens.map((t) => t.value).lastIndexOf('from');
const { fromToken, beforeTokens, afterTokens } = fromIndex >= 0
? {
fromToken: tokens[fromIndex],
beforeTokens: tokens.slice(0, fromIndex),
afterTokens: [...tokens.slice(fromIndex + 1), sourceCode.getFirstToken(node.source)]
}
: {
fromToken: null,
beforeTokens: [...tokens, sourceCode.getFirstToken(node.source)],
afterTokens: []
};
const namedSpecifiers = [];
for (const specifier of node.specifiers) {
if (specifier.type === 'ImportSpecifier') {
namedSpecifiers.push(specifier);
}
else {
const removeTokens = sourceCode.getTokens(specifier);
removeTokens.shift();
for (const token of removeTokens) {
const i = beforeTokens.indexOf(token);
if (i >= 0) {
beforeTokens.splice(i, 1);
}
}
}
}
if (namedSpecifiers.length) {
const leftBrace = sourceCode.getTokenBefore(namedSpecifiers[0]);
const rightBrace = sourceCode.getTokenAfter(namedSpecifiers[namedSpecifiers.length - 1], {
filter: isClosingBraceToken,
includeComments: false
});
offsets.setOffsetElementList(namedSpecifiers, leftBrace, rightBrace, 1);
for (const token of [...sourceCode.getTokensBetween(leftBrace, rightBrace), rightBrace]) {
const i = beforeTokens.indexOf(token);
if (i >= 0) {
beforeTokens.splice(i, 1);
}
}
}
if (beforeTokens.every((t) => isOpeningBraceToken(t) || isClosingBraceToken(t))) {
offsets.setOffsetToken(beforeTokens, 0, importToken);
}
else {
offsets.setOffsetToken(beforeTokens, 1, importToken);
}
if (fromToken) {
offsets.setOffsetToken(fromToken, 0, importToken);
offsets.setOffsetToken(afterTokens, 1, fromToken);
}
// assertions
const lastToken = sourceCode.getLastToken(node, {
filter: isNotSemicolonToken,
includeComments: false
});
const assertionTokens = sourceCode.getTokensBetween(node.source, lastToken);
if (assertionTokens.length) {
const assertToken = assertionTokens.shift();
offsets.setOffsetToken(assertToken, 0, importToken);
const assertionOpen = assertionTokens.shift();
if (assertionOpen) {
offsets.setOffsetToken(assertionOpen, 1, assertToken);
offsets.setOffsetElementList(assertionTokens, assertionOpen, lastToken, 1);
}
}
},
ImportExpression(node) {
const firstToken = sourceCode.getFirstToken(node);
const rightToken = sourceCode.getLastToken(node);
const leftToken = sourceCode.getTokenAfter(firstToken, {
filter: isOpeningParenToken,
includeComments: false
});
offsets.setOffsetToken(leftToken, 1, firstToken);
offsets.setOffsetElementList([node.source], leftToken, rightToken, 1);
},
ImportNamespaceSpecifier(node) {
const tokens = sourceCode.getTokens(node);
const firstToken = tokens.shift();
offsets.setOffsetToken(tokens, 1, firstToken);
},
ImportSpecifier(node) {
visitor.ExportSpecifier(node);
},
LabeledStatement(node) {
const labelToken = sourceCode.getFirstToken(node);
const colonToken = sourceCode.getTokenAfter(labelToken);
const bodyToken = sourceCode.getTokenAfter(colonToken);
offsets.setOffsetToken([colonToken, bodyToken], 1, labelToken);
},
SvelteReactiveStatement(node) {
visitor.LabeledStatement(node);
},
MemberExpression(node) {
const objectToken = sourceCode.getFirstToken(node);
if (node.type === 'MemberExpression' && node.computed) {
const leftBracketToken = sourceCode.getTokenBefore(node.property, {
filter: isOpeningBracketToken,
includeComments: false
});
const rightBracketToken = sourceCode.getTokenAfter(node.property, {
filter: isClosingBracketToken,
includeComments: false
});
for (const optionalToken of sourceCode.getTokensBetween(sourceCode.getLastToken(node.object), leftBracketToken, { filter: isOptionalToken, includeComments: false })) {
offsets.setOffsetToken(optionalToken, 1, objectToken);
}
offsets.setOffsetToken(leftBracketToken, 1, objectToken);
offsets.setOffsetElementList([node.property], leftBracketToken, rightBracketToken, 1);
}
else {
const dotToken = sourceCode.getTokenBefore(node.property);
const propertyToken = sourceCode.getTokenAfter(dotToken);
offsets.setOffsetToken([dotToken, propertyToken], 1, objectToken);
}
},
MetaProperty(node) {
visitor.MemberExpression(node);
},
MethodDefinition(node) {
const firstToken = sourceCode.getFirstToken(node);
const keyTokens = getFirstAndLastTokens(sourceCode, node.key);
const prefixTokens = sourceCode.getTokensBetween(firstToken, keyTokens.firstToken);
if (node.computed) {
prefixTokens.pop(); // pop [
}
offsets.setOffsetToken(prefixTokens, 0, firstToken);
let lastKeyToken;
if (node.computed) {
const leftBracketToken = sourceCode.getTokenBefore(keyTokens.firstToken);
const rightBracketToken = (lastKeyToken = sourceCode.getTokenAfter(keyTokens.lastToken));
offsets.setOffsetToken(leftBracketToken, 0, firstToken);
offsets.setOffsetElementList([node.key], leftBracketToken, rightBracketToken, 1);
}
else {
offsets.setOffsetToken(keyTokens.firstToken, 0, firstToken);
lastKeyToken = keyTokens.lastToken;
}
if (node.value) {
const initToken = sourceCode.getFirstToken(node.value);
offsets.setOffsetToken([...sourceCode.getTokensBetween(lastKeyToken, initToken), initToken], 1, lastKeyToken);
}
},
Property(node) {
visitor.MethodDefinition(node);
},
NewExpression(node) {
const typeArguments = node.typeArguments ??
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Support old typescript-eslint
node.typeParameters;
const newToken = sourceCode.getFirstToken(node);
const calleeTokens = getFirstAndLastTokens(sourceCode, node.callee);
offsets.setOffsetToken(calleeTokens.firstToken, 1, newToken);
if (typeArguments) {
offsets.setOffsetToken(sourceCode.getFirstToken(typeArguments), 1, calleeTokens.firstToken);
}
const leftParenBefore = typeArguments || calleeTokens.lastToken;
if (node.arguments.length || leftParenBefore.range[1] < node.range[1]) {
const rightParenToken = sourceCode.getLastToken(node);
const leftParenToken = sourceCode.getTokenAfter(leftParenBefore);
offsets.setOffsetToken(leftParenToken, 1, calleeTokens.firstToken);
offsets.setOffsetElementList(node.arguments, leftParenToken, rightParenToken, 1);
}
},
ObjectExpression(node) {
const firstToken = sourceCode.getFirstToken(node);
const rightToken = sourceCode.getTokenAfter(node.properties[node.properties.length - 1] || firstToken, { filter: isClosingBraceToken, includeComments: false });
offsets.setOffsetElementList(node.properties, firstToken, rightToken, 1);
},
ObjectPattern(node) {
visitor.ObjectExpression(node);
},
PropertyDefinition(node) {
visitor.MethodDefinition(node);
},
ReturnStatement(node) {
if (node.argument) {
const firstToken = sourceCode.getFirstToken(node);
const nextToken = sourceCode.getTokenAfter(firstToken);
offsets.setOffsetToken(nextToken, 1, firstToken);
}
},
ThrowStatement(node) {
visitor.ReturnStatement(node);
},
SequenceExpression(node) {
const firstToken = sourceCode.getFirstToken(node);
offsets.setOffsetElementList(node.expressions, firstToken, null, 0);
},
SwitchCase(node) {
const caseToken = sourceCode.getFirstToken(node);
if (node.test != null) {
const testTokens = getFirstAndLastTokens(sourceCode, node.test);
const colonToken = sourceCode.getTokenAfter(testTokens.lastToken);
offsets.setOffsetToken([testTokens.firstToken, colonToken], 1, caseToken);
}
else {
const colonToken = sourceCode.getTokenAfter(caseToken);
offsets.setOffsetToken(colonToken, 1, caseToken);
}
if (node.consequent.length === 1 && node.consequent[0].type === 'BlockStatement') {
offsets.setOffsetToken(sourceCode.getFirstToken(node.consequent[0]), 0, caseToken);
}
else {
for (const statement of node.consequent) {
offsets.setOffsetToken(getFirstAndLastTokens(sourceCode, statement).firstToken, 1, caseToken);
}
}
},
SwitchStatement(node) {
const switchToken = sourceCode.getFirstToken(node);
const { firstToken: leftParenToken, lastToken: rightParenToken } = getFirstAndLastTokens(sourceCode, node.discriminant);
const leftBraceToken = sourceCode.getTokenAfter(rightParenToken);
const rightBraceToken = sourceCode.getLastToken(node);
offsets.setOffsetToken(leftParenToken, 1, switchToken);
offsets.setOffsetElementList([node.discriminant], leftParenToken, rightParenToken, 1);
offsets.setOffsetToken(leftBraceToken, 0, switchToken);
offsets.setOffsetElementList(node.cases, leftBraceToken, rightBraceToken, options.switchCase);
},
TaggedTemplateExpression(node) {
const tagTokens = getFirstAndLastTokens(sourceCode, node.tag);
offsets.setOffsetToken(sourceCode.getFirstToken(node.quasi), 1, tagTokens.firstToken);
},
TemplateLiteral(node) {
const firstToken = sourceCode.getFirstToken(node);
const quasiTokens = node.quasis.slice(1).map((n) => sourceCode.getFirstToken(n));
const expressionToken = node.quasis.slice(0, -1).map((n) => sourceCode.getTokenAfter(n));
offsets.setOffsetToken(quasiTokens, 0, firstToken);
offsets.setOffsetToken(expressionToken, 1, firstToken);
},
TryStatement(node) {
const tryToken = sourceCode.getFirstToken(node);
const tryBlockToken = sourceCode.getFirstToken(node.block);
offsets.setOffsetToken(tryBlockToken, 0, tryToken);
if (node.handler != null) {
const catchToken = sourceCode.getFirstToken(node.handler);
offsets.setOffsetToken(catchToken, 0, tryToken);
}
if (node.finalizer != null) {
const finallyToken = sourceCode.getTokenBefore(node.finalizer);
const finallyBlockToken = sourceCode.getFirstToken(node.finalizer);
offsets.setOffsetToken([finallyToken, finallyBlockToken], 0, tryToken);
}
},
UpdateExpression(node) {
const firstToken = sourceCode.getFirstToken(node);
const nextToken = sourceCode.getTokenAfter(firstToken);
offsets.setOffsetToken(nextToken, 1, firstToken);
},
VariableDeclaration(node) {
offsets.setOffsetElementList(node.declarations, sourceCode.getFirstToken(node), null, 1);
},
VariableDeclarator(node) {
if (node.init != null) {
const idToken = sourceCode.getFirstToken(node);
const eqToken = sourceCode.getTokenAfter(node.id);
const initToken = sourceCode.getTokenAfter(eqToken);
offsets.setOffsetToken([eqToken, initToken], 1, idToken);
}
},
WhileStatement(node) {
const firstToken = sourceCode.getFirstToken(node);
const leftParenToken = sourceCode.getTokenAfter(firstToken);
const rightParenToken = sourceCode.getTokenBefore(node.body, {
filter: isClosingParenToken,
includeComments: false
});
offsets.setOffsetToken(leftParenToken, 1, firstToken);
offsets.setOffsetToken(rightParenToken, 0, leftParenToken);
const bodyFirstToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyFirstToken, isOpeningBraceToken(bodyFirstToken) ? 0 : 1, firstToken);
},
WithStatement(node) {
visitor.WhileStatement(node);
},
YieldExpression(node) {
if (node.argument != null) {
const [yieldToken, secondToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(secondToken, 1, yieldToken);
if (node.delegate) {
offsets.setOffsetToken(sourceCode.getTokenAfter(secondToken), 1, yieldToken);
}
}
},
// ----------------------------------------------------------------------
// SINGLE TOKEN NODES
// ----------------------------------------------------------------------
DebuggerStatement() {
// noop
},
Identifier() {
// noop
},
ImportDefaultSpecifier() {
// noop
},
Literal() {
// noop
},
PrivateIdentifier() {
// noop
},
Super() {
// noop
},
TemplateElement() {
// noop
},
ThisExpression() {
// noop
},
// ----------------------------------------------------------------------
// WRAPPER NODES
// ----------------------------------------------------------------------
ExpressionStatement() {
// noop
},
ChainExpression() {
// noop
},
EmptyStatement() {
// noop
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
const commonVisitor = {
':statement, PropertyDefinition'(node) {
const firstToken = sourceCode.getFirstToken(node);
const lastToken = sourceCode.getLastToken(node);
if (isSemicolonToken(lastToken) && firstToken !== lastToken) {
const next = sourceCode.getTokenAfter(lastToken);
if (!next || lastToken.loc.start.line < next.loc.start.line) {
// End of line semicolons
offsets.setOffsetToken(lastToken, 0, firstToken);
}
}
},
':expression'(node) {
// Proc parentheses.
let leftToken = sourceCode.getTokenBefore(node);
let rightToken = sourceCode.getTokenAfter(node);
let firstToken = sourceCode.getFirstToken(node);
while (leftToken &&
isOpeningParenToken(leftToken) &&
rightToken &&
isClosingParenToken(rightToken)) {
offsets.setOffsetToken(firstToken, 1, leftToken);
offsets.setOffsetToken(rightToken, 0, leftToken);
firstToken = leftToken;
leftToken = sourceCode.getTokenBefore(leftToken);
rightToken = sourceCode.getTokenAfter(rightToken);
}
}
};
const v = visitor;
return {
...v,
...commonVisitor
};
}
/**
* Checks whether given text is known button type
*/
function isOptionalToken(token) {
return token.type === 'Punctuator' && token.value === '?.';
}

View File

@@ -0,0 +1,10 @@
import type { RuleContext, RuleListener } from '../../types.js';
import type { IndentOptions } from './commons.js';
/**
* Creates AST event handlers for html-indent.
*
* @param context The rule context.
* @param defaultOptions The default value of options.
* @returns AST event handlers.
*/
export declare function defineVisitor(context: RuleContext, defaultOptions: Partial<IndentOptions>): RuleListener;

View File

@@ -0,0 +1,237 @@
import * as SV from './svelte.js';
import * as ES from './es.js';
import * as TS from './ts.js';
import { isNotWhitespace } from './ast.js';
import { isCommentToken } from '@eslint-community/eslint-utils';
import { OffsetContext } from './offset-context.js';
/**
* Normalize options.
* @param type The type of indentation.
* @param options Other options.
* @param defaultOptions The default value of options.
* @returns Normalized options.
*/
function parseOptions(options, defaultOptions) {
const ret = {
indentChar: ' ',
indentScript: true,
indentSize: 2,
switchCase: 1,
alignAttributesVertically: false,
ignoredNodes: [],
...defaultOptions
};
if (Number.isSafeInteger(options.indent)) {
ret.indentSize = Number(options.indent);
}
else if (options.indent === 'tab') {
ret.indentChar = '\t';
ret.indentSize = 1;
}
if (typeof options.indentScript === 'boolean') {
ret.indentScript = options.indentScript;
}
if (options.switchCase != null && Number.isSafeInteger(options.switchCase)) {
ret.switchCase = options.switchCase;
}
if (options.ignoredNodes != null) {
ret.ignoredNodes = options.ignoredNodes;
}
if (options.alignAttributesVertically && ret.indentChar === ' ') {
ret.alignAttributesVertically = true;
}
else if (ret.indentChar !== ' ') {
ret.alignAttributesVertically = false;
}
return ret;
}
/**
* Creates AST event handlers for html-indent.
*
* @param context The rule context.
* @param defaultOptions The default value of options.
* @returns AST event handlers.
*/
export function defineVisitor(context, defaultOptions) {
if (!context.filename.endsWith('.svelte'))
return {};
const options = parseOptions(context.options[0] || {}, defaultOptions);
const sourceCode = context.sourceCode;
const offsets = new OffsetContext({ sourceCode, options });
/**
* Get the text of the indentation part of the given location.
*/
function getIndentText({ line, column }) {
return sourceCode.lines[line - 1].slice(0, column);
}
/**
* Validate the given token with the pre-calculated expected indentation.
*/
function validateToken(token, expectedIndent) {
const line = token.loc.start.line;
const indentText = getIndentText(token.loc.start);
// `indentText` contains non-whitespace characters.
if (indentText.trim() !== '') {
return;
}
const actualIndent = token.loc.start.column;
const mismatchCharIndexes = [];
for (let i = 0; i < indentText.length; ++i) {
if (indentText[i] !== options.indentChar) {
mismatchCharIndexes.push(i);
}
}
if (actualIndent !== expectedIndent) {
const loc = {
start: { line, column: 0 },
end: { line, column: actualIndent }
};
context.report({
loc,
messageId: 'unexpectedIndentation',
data: {
expectedIndent: String(expectedIndent),
actualIndent: String(actualIndent),
expectedUnit: options.indentChar === '\t' ? 'tab' : 'space',
actualUnit: mismatchCharIndexes.length
? 'whitespace'
: options.indentChar === '\t'
? 'tab'
: 'space',
expectedIndentPlural: expectedIndent === 1 ? '' : 's',
actualIndentPlural: actualIndent === 1 ? '' : 's'
},
fix(fixer) {
return fixer.replaceTextRange([sourceCode.getIndexFromLoc(loc.start), sourceCode.getIndexFromLoc(loc.end)], options.indentChar.repeat(expectedIndent));
}
});
return;
}
for (const i of mismatchCharIndexes) {
const loc = {
start: { line, column: i },
end: { line, column: i + 1 }
};
context.report({
loc,
messageId: 'unexpectedChar',
data: {
expected: JSON.stringify(options.indentChar),
actual: JSON.stringify(indentText[i])
},
fix(fixer) {
return fixer.replaceTextRange([sourceCode.getIndexFromLoc(loc.start), sourceCode.getIndexFromLoc(loc.end)], options.indentChar);
}
});
}
}
/** Process line tokens */
function processLine(tokens, prevComments, prevToken, calculator) {
const firstToken = tokens[0];
const actualIndent = firstToken.loc.start.column;
const expectedIndent = calculator.getExpectedIndentFromTokens(tokens);
if (expectedIndent == null) {
calculator.saveExpectedIndent(tokens, actualIndent);
return;
}
calculator.saveExpectedIndent(tokens, Math.min(...tokens
.map((t) => calculator.getExpectedIndentFromToken(t))
.filter((i) => i != null)));
let prev = prevToken;
if (prevComments.length) {
if (prev && prev.loc.end.line < prevComments[0].loc.start.line) {
validateToken(prevComments[0], expectedIndent);
}
prev = prevComments[prevComments.length - 1];
}
if (prev && prev.loc.end.line < tokens[0].loc.start.line) {
validateToken(tokens[0], expectedIndent);
}
}
const indentContext = {
sourceCode,
options,
offsets
};
const nodesVisitor = {
...ES.defineVisitor(indentContext),
...SV.defineVisitor(indentContext),
...TS.defineVisitor(indentContext)
};
const knownNodes = new Set(Object.keys(nodesVisitor));
/**
* Build a visitor combined with a visitor to handle the given ignore selector.
*/
function compositingIgnoresVisitor(visitor) {
for (const ignoreSelector of options.ignoredNodes) {
const key = `${ignoreSelector}:exit`;
if (visitor[key]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
const handler = visitor[key];
visitor[key] = function (node, ...args) {
const ret = handler.call(this, node, ...args);
offsets.ignore(node);
return ret;
};
}
else {
visitor[key] = (node) => offsets.ignore(node);
}
}
return visitor;
}
return compositingIgnoresVisitor({
...nodesVisitor,
'*:exit'(node) {
// Ignore tokens of unknown nodes.
if (!knownNodes.has(node.type)) {
// debugger
// console.log(node.type, node.loc!.start.line)
offsets.ignore(node);
}
},
'Program:exit'(node) {
const calculator = offsets.getOffsetCalculator();
let prevToken = null;
for (const { prevComments, tokens } of iterateLineTokens()) {
processLine(tokens, prevComments, prevToken, calculator);
prevToken = tokens[tokens.length - 1];
}
/** Iterate line tokens */
function* iterateLineTokens() {
let line = 0;
let prevComments = [];
let bufferTokens = [];
for (const token of sourceCode.getTokens(node, {
includeComments: true,
filter: isNotWhitespace
})) {
const thisLine = token.loc.start.line;
if (line === thisLine || bufferTokens.length === 0) {
bufferTokens.push(token);
}
else {
if (isCommentToken(bufferTokens[0]) && bufferTokens.every(isCommentToken)) {
prevComments.push(bufferTokens[0]);
}
else {
yield {
prevComments,
tokens: bufferTokens
};
prevComments = [];
}
bufferTokens = [token];
}
line = thisLine;
}
if (bufferTokens.length && !bufferTokens.every(isCommentToken)) {
yield {
prevComments,
tokens: bufferTokens
};
}
}
}
});
}

View File

@@ -0,0 +1,88 @@
import type { ASTNode, SourceCode } from '../../types.js';
import type { AnyToken, IndentOptions, MaybeNode } from './commons.js';
declare const enum OffsetDataType {
normal = 0,
align = 1,
start = 2
}
type OffsetData = {
type: OffsetDataType.normal;
base: number;
offset: number;
expectedIndent?: number;
} | {
type: OffsetDataType.align;
base: number;
alignIndent: number;
expectedIndent?: number;
} | {
type: OffsetDataType.start;
offset: number;
expectedIndent?: number;
};
export declare class OffsetContext {
private readonly sourceCode;
private readonly options;
private readonly offsets;
private readonly ignoreRanges;
constructor(arg: {
sourceCode: SourceCode;
options: IndentOptions;
});
/**
* Set offset to the given index.
*/
setOffsetIndex(index: number, offset: number, base: number): void;
/**
* Set align indent to the given index.
*/
private setAlignIndent;
/**
* Set offset to the given tokens.
*/
setOffsetToken(token: AnyToken | null | undefined | (AnyToken | null | undefined)[], offset: number, baseToken: AnyToken): void;
/**
* Copy offset to the given index from srcIndex.
*/
copyOffset(index: number, srcIndex: number): void;
/**
* Set start offset to the given index.
*/
setStartOffsetIndex(index: number, offset: number): void;
/**
* Set start offset to the given token.
*/
setStartOffsetToken(token: AnyToken | null | undefined | (AnyToken | null | undefined)[], offset: number): void;
setOffsetElementList(nodes: (ASTNode | AnyToken | MaybeNode | null | undefined)[], baseNodeOrToken: ASTNode | AnyToken | MaybeNode, lastNodeOrToken: ASTNode | AnyToken | MaybeNode | null, offset: number, align?: boolean): void;
private _setOffsetElementList;
/**
* Ignore range of the given node.
*/
ignore(node: ASTNode): void;
getOffsetCalculator(): OffsetCalculator;
}
export declare class OffsetCalculator {
private readonly options;
private readonly offsets;
private readonly ignoreRanges;
constructor(arg: {
offsets: Map<number, OffsetData>;
options: IndentOptions;
ignoreRanges: [number, number][];
});
/**
* Calculate correct indentation of the given index.
*/
private getExpectedIndentFromIndex;
/**
* Calculate correct indentation of the given token.
*/
getExpectedIndentFromToken(token: AnyToken): number | null;
/**
* Calculate correct indentation of the line of the given tokens.
*/
getExpectedIndentFromTokens(tokens: AnyToken[]): null | number;
/** Save expected indent to give tokens */
saveExpectedIndent(tokens: AnyToken[], expectedIndent: number): void;
}
export {};

View File

@@ -0,0 +1,220 @@
import { isNotWhitespace } from './ast.js';
import { isBeginningOfLine } from './commons.js';
import { getFirstAndLastTokens } from './commons.js';
export class OffsetContext {
constructor(arg) {
this.offsets = new Map();
this.ignoreRanges = new Map();
this.sourceCode = arg.sourceCode;
this.options = arg.options;
}
/**
* Set offset to the given index.
*/
setOffsetIndex(index, offset, base) {
if (index === base) {
return;
}
this.offsets.set(index, {
type: 0 /* OffsetDataType.normal */,
base,
offset
});
}
/**
* Set align indent to the given index.
*/
setAlignIndent(index, alignIndent, base) {
if (index === base) {
return;
}
this.offsets.set(index, {
type: 1 /* OffsetDataType.align */,
base,
alignIndent
});
}
/**
* Set offset to the given tokens.
*/
setOffsetToken(token, offset, baseToken) {
if (!token) {
return;
}
if (Array.isArray(token)) {
for (const t of token) {
this.setOffsetToken(t, offset, baseToken);
}
return;
}
this.setOffsetIndex(token.range[0], offset, baseToken.range[0]);
}
/**
* Copy offset to the given index from srcIndex.
*/
copyOffset(index, srcIndex) {
const offsetData = this.offsets.get(srcIndex);
if (!offsetData) {
return;
}
if (offsetData.type === 2 /* OffsetDataType.start */) {
this.setStartOffsetIndex(index, offsetData.offset);
}
else if (offsetData.type === 1 /* OffsetDataType.align */) {
this.setAlignIndent(index, offsetData.alignIndent, offsetData.base);
}
else {
this.setOffsetIndex(index, offsetData.offset, offsetData.base);
}
}
/**
* Set start offset to the given index.
*/
setStartOffsetIndex(index, offset) {
this.offsets.set(index, {
type: 2 /* OffsetDataType.start */,
offset
});
}
/**
* Set start offset to the given token.
*/
setStartOffsetToken(token, offset) {
if (!token) {
return;
}
if (Array.isArray(token)) {
for (const t of token) {
this.setStartOffsetToken(t, offset);
}
return;
}
this.setStartOffsetIndex(token.range[0], offset);
}
setOffsetElementList(nodes, baseNodeOrToken, lastNodeOrToken, offset, align) {
let setIndent = (token, baseToken) => this.setOffsetToken(token, offset, baseToken);
if (align) {
for (const n of nodes) {
if (n) {
if (!isBeginningOfLine(this.sourceCode, n)) {
const startLoc = n.loc.start;
const alignIndent = startLoc.column - /^\s*/u.exec(this.sourceCode.lines[startLoc.line - 1])[0].length;
setIndent = (token, baseToken) => this.setAlignIndent(token.range[0], alignIndent, baseToken.range[0]);
}
break;
}
}
}
this._setOffsetElementList(nodes, baseNodeOrToken, lastNodeOrToken, setIndent);
}
_setOffsetElementList(nodes, baseNodeOrToken, lastNodeOrToken, setIndent) {
const baseToken = this.sourceCode.getFirstToken(baseNodeOrToken);
let prevToken = this.sourceCode.getLastToken(baseNodeOrToken);
for (const node of nodes) {
if (node == null) {
continue;
}
const elementTokens = getFirstAndLastTokens(this.sourceCode, node, prevToken.range[1]);
let t = prevToken;
while ((t = this.sourceCode.getTokenAfter(t, {
includeComments: true,
filter: isNotWhitespace
})) != null &&
t.range[1] <= elementTokens.firstToken.range[0]) {
setIndent(t, baseToken);
}
setIndent(elementTokens.firstToken, baseToken);
prevToken = elementTokens.lastToken;
}
if (lastNodeOrToken) {
const lastToken = this.sourceCode.getFirstToken(lastNodeOrToken);
let t = prevToken;
while ((t = this.sourceCode.getTokenAfter(t, {
includeComments: true,
filter: isNotWhitespace
})) != null &&
t.range[1] <= lastToken.range[0]) {
setIndent(t, baseToken);
}
this.setOffsetToken(lastToken, 0, baseToken);
}
}
/**
* Ignore range of the given node.
*/
ignore(node) {
const range = node.range;
const n = this.ignoreRanges.get(range[0]) ?? 0;
this.ignoreRanges.set(range[0], Math.max(n, range[1]));
}
getOffsetCalculator() {
// eslint-disable-next-line @typescript-eslint/no-use-before-define -- ignore
return new OffsetCalculator({
offsets: this.offsets,
options: this.options,
ignoreRanges: [...this.ignoreRanges]
});
}
}
export class OffsetCalculator {
constructor(arg) {
this.offsets = arg.offsets;
this.options = arg.options;
this.ignoreRanges = arg.ignoreRanges;
}
/**
* Calculate correct indentation of the given index.
*/
getExpectedIndentFromIndex(index) {
const offsetInfo = this.offsets.get(index);
if (offsetInfo == null) {
return null;
}
if (offsetInfo.expectedIndent != null) {
return offsetInfo.expectedIndent;
}
if (offsetInfo.type === 2 /* OffsetDataType.start */) {
return offsetInfo.offset * this.options.indentSize;
}
const baseIndent = this.getExpectedIndentFromIndex(offsetInfo.base);
if (baseIndent == null) {
return null;
}
if (offsetInfo.type === 1 /* OffsetDataType.align */) {
return baseIndent + offsetInfo.alignIndent;
}
return baseIndent + offsetInfo.offset * this.options.indentSize;
}
/**
* Calculate correct indentation of the given token.
*/
getExpectedIndentFromToken(token) {
return this.getExpectedIndentFromIndex(token.range[0]);
}
/**
* Calculate correct indentation of the line of the given tokens.
*/
getExpectedIndentFromTokens(tokens) {
for (const token of tokens) {
const index = token.range[0];
if (this.ignoreRanges.some(([f, t]) => f <= index && index < t)) {
return null;
}
const expectedIndent = this.getExpectedIndentFromIndex(index);
if (expectedIndent != null) {
return expectedIndent;
}
}
return null;
}
/** Save expected indent to give tokens */
saveExpectedIndent(tokens, expectedIndent) {
for (const token of tokens) {
const offsetInfo = this.offsets.get(token.range[0]);
if (offsetInfo == null) {
continue;
}
offsetInfo.expectedIndent = offsetInfo.expectedIndent ?? expectedIndent;
}
}
}

View File

@@ -0,0 +1,11 @@
import type { SvelteNodeListener } from '../../types-for-node.js';
import type { IndentContext } from './commons.js';
type NodeListener = SvelteNodeListener;
/**
* Creates AST event handlers for svelte nodes.
*
* @param context The rule context.
* @returns AST event handlers.
*/
export declare function defineVisitor(context: IndentContext): NodeListener;
export {};

View File

@@ -0,0 +1,472 @@
import { isNotWhitespace } from './ast.js';
import { isBeginningOfElement } from './commons.js';
import { isBeginningOfLine } from './commons.js';
import { getFirstAndLastTokens } from './commons.js';
import { isClosingParenToken, isOpeningParenToken } from '@eslint-community/eslint-utils';
const PREFORMATTED_ELEMENT_NAMES = ['pre', 'textarea', 'template'];
/**
* Creates AST event handlers for svelte nodes.
*
* @param context The rule context.
* @returns AST event handlers.
*/
export function defineVisitor(context) {
const { sourceCode, offsets, options } = context;
const visitor = {
// ----------------------------------------------------------------------
// ELEMENTS
// ----------------------------------------------------------------------
SvelteScriptElement(node) {
offsets.setOffsetElementList(node.body, node.startTag, node.endTag, options.indentScript ? 1 : 0);
},
SvelteStyleElement(node) {
node.children.forEach((n) => offsets.ignore(n));
},
SvelteElement(node) {
if (node.name.type === 'Identifier' || node.name.type === 'SvelteName') {
if (PREFORMATTED_ELEMENT_NAMES.includes(node.name.name)) {
const startTagToken = sourceCode.getFirstToken(node);
const endTagToken = node.endTag && sourceCode.getFirstToken(node.endTag);
offsets.setOffsetToken(endTagToken, 0, startTagToken);
node.children.forEach((n) => offsets.ignore(n));
return;
}
if (node.name.name === 'style') {
// Inline style tag
node.children.forEach((n) => offsets.ignore(n));
return;
}
}
if (node.endTag) {
offsets.setOffsetElementList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, 1);
}
},
// ----------------------------------------------------------------------
// TAGS
// ----------------------------------------------------------------------
SvelteStartTag(node) {
const openToken = sourceCode.getFirstToken(node);
const closeToken = sourceCode.getLastToken(node);
offsets.setOffsetElementList(node.attributes, openToken, closeToken, 1, options.alignAttributesVertically);
if (node.selfClosing) {
const slash = sourceCode.getTokenBefore(closeToken);
if (slash.value === '/') {
offsets.setOffsetToken(slash, 0, openToken);
}
}
},
SvelteEndTag(node) {
const openToken = sourceCode.getFirstToken(node);
const closeToken = sourceCode.getLastToken(node);
offsets.setOffsetElementList([], openToken, closeToken, 1);
},
// ----------------------------------------------------------------------
// ATTRIBUTES
// ----------------------------------------------------------------------
SvelteAttribute(node) {
const keyToken = sourceCode.getFirstToken(node);
const eqToken = sourceCode.getTokenAfter(node.key);
if (eqToken != null && eqToken.range[1] <= node.range[1]) {
offsets.setOffsetToken(eqToken, 1, keyToken);
const valueStartToken = sourceCode.getTokenAfter(eqToken);
if (valueStartToken != null && valueStartToken.range[1] <= node.range[1]) {
offsets.setOffsetToken(valueStartToken, 1, keyToken);
const values = node.type === 'SvelteAttribute' || node.type === 'SvelteStyleDirective'
? node.value
: [];
// process quoted
let processedValues = false;
if (valueStartToken.type === 'Punctuator') {
const quoted = ['"', "'"].includes(valueStartToken.value);
const mustache = !quoted && valueStartToken.value === '{';
if (quoted || mustache) {
const last = sourceCode.getLastToken(node);
if (last.type === 'Punctuator' &&
((quoted && last.value === valueStartToken.value) ||
(mustache && last.value === '}'))) {
offsets.setOffsetToken(last, 0, valueStartToken);
offsets.setOffsetElementList(values, valueStartToken, last, 1);
processedValues = true;
}
}
}
if (!processedValues) {
for (const val of values) {
const token = sourceCode.getFirstToken(val);
offsets.setOffsetToken(token, 0, valueStartToken);
}
}
}
}
},
SvelteDirective(node) {
visitor.SvelteAttribute(node);
},
SvelteStyleDirective(node) {
visitor.SvelteAttribute(node);
},
SvelteSpecialDirective(node) {
visitor.SvelteAttribute(node);
},
SvelteShorthandAttribute(node) {
const openToken = sourceCode.getFirstToken(node);
const closeToken = sourceCode.getLastToken(node);
offsets.setOffsetElementList([], openToken, closeToken, 1);
},
SvelteSpreadAttribute(node) {
visitor.SvelteShorthandAttribute(node);
},
// ----------------------------------------------------------------------
// ATTRIBUTE KEYS
// ----------------------------------------------------------------------
SvelteDirectiveKey(_node) {
// noop
},
SvelteSpecialDirectiveKey(_node) {
// noop
},
// ----------------------------------------------------------------------
// CONTENTS
// ----------------------------------------------------------------------
SvelteText(node) {
const tokens = sourceCode.getTokens(node, {
filter: isNotWhitespace,
includeComments: false
});
const first = tokens.shift();
if (!first) {
return;
}
offsets.setOffsetToken(tokens, isBeginningOfLine(sourceCode, first) ? 0 : isBeginningOfElement(node) ? 1 : 0, first);
},
SvelteLiteral(node) {
const tokens = sourceCode.getTokens(node, {
filter: isNotWhitespace,
includeComments: false
});
const first = tokens.shift();
if (!first) {
return;
}
offsets.setOffsetToken(tokens, isBeginningOfLine(sourceCode, first) ? 0 : 1, first);
},
// ----------------------------------------------------------------------
// MUSTACHE TAGS
// ----------------------------------------------------------------------
SvelteMustacheTag(node) {
const openToken = sourceCode.getFirstToken(node);
const closeToken = sourceCode.getLastToken(node);
offsets.setOffsetElementList([node.expression], openToken, closeToken, 1);
},
SvelteDebugTag(node) {
const openToken = sourceCode.getFirstToken(node);
const closeToken = sourceCode.getLastToken(node);
offsets.setOffsetElementList(node.identifiers, openToken, closeToken, 1);
},
SvelteConstTag(node) {
const openToken = sourceCode.getFirstToken(node);
const constToken = sourceCode.getTokenAfter(openToken);
const declarationToken = sourceCode.getFirstToken(node.declaration);
const closeToken = sourceCode.getLastToken(node);
offsets.setOffsetToken(constToken, 1, openToken);
offsets.setOffsetToken(declarationToken, 1, openToken);
offsets.setOffsetToken(closeToken, 0, openToken);
},
SvelteDeclarationTag(node) {
const openToken = sourceCode.getFirstToken(node);
const declarationToken = sourceCode.getFirstToken(node.declaration);
const closeToken = sourceCode.getLastToken(node);
offsets.setOffsetToken(declarationToken, 1, openToken);
offsets.setOffsetToken(closeToken, 0, openToken);
},
SvelteRenderTag(node) {
const openToken = sourceCode.getFirstToken(node);
const renderToken = sourceCode.getTokenAfter(openToken);
offsets.setOffsetToken(renderToken, 1, openToken);
offsets.setOffsetToken(node.expression, 1, renderToken);
},
// ----------------------------------------------------------------------
// BLOCKS
// ----------------------------------------------------------------------
SvelteIfBlock(node) {
const [openToken, ...ifTokens] = sourceCode.getFirstTokens(node, {
count: node.elseif ? 3 : 2,
includeComments: false
});
offsets.setOffsetToken(ifTokens, 1, openToken);
const exp = getFirstAndLastTokens(sourceCode, node.expression);
offsets.setOffsetToken(exp.firstToken, 1, ifTokens[0]);
const closeOpenTagToken = sourceCode.getTokenAfter(exp.lastToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
if (node.else) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.else), 0, openToken);
if (node.else.elseif) {
// else if
return;
}
}
const [openCloseTagToken, endIfToken, closeCloseTagToken] = sourceCode.getLastTokens(node, {
count: 3,
includeComments: false
});
offsets.setOffsetToken(openCloseTagToken, 0, openToken);
offsets.setOffsetToken(endIfToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
SvelteElseBlock(node) {
if (node.elseif) {
return;
}
const [openToken, elseToken, closeToken] = sourceCode.getFirstTokens(node, {
count: 3,
includeComments: false
});
offsets.setOffsetToken(elseToken, 1, openToken);
offsets.setOffsetToken(closeToken, 0, openToken);
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
},
SvelteEachBlock(node) {
const [openToken, eachToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(eachToken, 1, openToken);
offsets.setOffsetElementList([node.expression, node.context, node.index], eachToken, null, 1);
if (node.key) {
const key = getFirstAndLastTokens(sourceCode, node.key);
offsets.setOffsetToken(key.firstToken, 1, eachToken);
const closeOpenTagToken = sourceCode.getTokenAfter(key.lastToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
}
else {
const closeOpenTagToken = sourceCode.getTokenAfter(node.index || node.context || node.expression);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
}
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
if (node.else) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.else), 0, openToken);
}
const [openCloseTagToken, endEachToken, closeCloseTagToken] = sourceCode.getLastTokens(node, {
count: 3,
includeComments: false
});
offsets.setOffsetToken(openCloseTagToken, 0, openToken);
offsets.setOffsetToken(endEachToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
SvelteAwaitBlock(node) {
const [openToken, awaitToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(awaitToken, 1, openToken);
const exp = getFirstAndLastTokens(sourceCode, node.expression);
offsets.setOffsetToken(exp.firstToken, 1, awaitToken);
if (node.pending) {
const closeOpenTagToken = sourceCode.getTokenAfter(exp.lastToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
offsets.setOffsetToken(sourceCode.getFirstToken(node.pending, {
includeComments: false,
filter: isNotWhitespace
}), 1, openToken);
}
if (node.then) {
if (node.kind === 'await-then') {
// {#await expression then value}
const thenToken = sourceCode.getTokenAfter(exp.lastToken);
offsets.setOffsetToken(thenToken, 1, openToken);
if (node.then.value) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.then.value), 1, thenToken);
}
const closeOpenTagToken = sourceCode.getTokenAfter(node.then.value || thenToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
}
else {
// {:then value}
offsets.setOffsetToken(sourceCode.getFirstToken(node.then), 0, openToken);
}
}
if (node.catch) {
if (node.kind === 'await-catch') {
// {#await expression catch error}
const catchToken = sourceCode.getTokenAfter(exp.lastToken);
offsets.setOffsetToken(catchToken, 1, openToken);
if (node.catch.error) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.catch.error), 1, catchToken);
}
const closeOpenTagToken = sourceCode.getTokenAfter(node.catch.error || catchToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
}
else {
// {:catch value}
offsets.setOffsetToken(sourceCode.getFirstToken(node.catch), 0, openToken);
}
}
const [openCloseTagToken, endAwaitToken, closeCloseTagToken] = sourceCode.getLastTokens(node, {
count: 3,
includeComments: false
});
offsets.setOffsetToken(openCloseTagToken, 0, openToken);
offsets.setOffsetToken(endAwaitToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
SvelteAwaitPendingBlock(node) {
const openToken = sourceCode.getFirstToken(node);
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
},
SvelteAwaitThenBlock(node) {
if (!node.awaitThen) {
// {:then value}
const [openToken, thenToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(thenToken, 1, openToken);
if (node.value) {
const valueToken = sourceCode.getFirstToken(node.value);
offsets.setOffsetToken(valueToken, 1, thenToken);
}
const closeOpenTagToken = sourceCode.getTokenAfter(node.value || thenToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
}
const openToken = sourceCode.getFirstToken(node);
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
},
SvelteAwaitCatchBlock(node) {
if (!node.awaitCatch) {
// {:catch error}
const [openToken, catchToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(catchToken, 1, openToken);
if (node.error) {
const errorToken = sourceCode.getFirstToken(node.error);
offsets.setOffsetToken(errorToken, 1, catchToken);
}
const closeOpenTagToken = sourceCode.getTokenAfter(node.error || catchToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
}
const openToken = sourceCode.getFirstToken(node);
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
},
SvelteKeyBlock(node) {
const [openToken, keyToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(keyToken, 1, openToken);
const exp = getFirstAndLastTokens(sourceCode, node.expression);
offsets.setOffsetToken(exp.firstToken, 1, keyToken);
const closeOpenTagToken = sourceCode.getTokenAfter(exp.lastToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
const [openCloseTagToken, endKeyToken, closeCloseTagToken] = sourceCode.getLastTokens(node, {
count: 3,
includeComments: false
});
offsets.setOffsetToken(openCloseTagToken, 0, openToken);
offsets.setOffsetToken(endKeyToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
SvelteSnippetBlock(node) {
const [openToken, snippetToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(snippetToken, 1, openToken);
const snippetName = sourceCode.getTokenAfter(snippetToken);
offsets.setOffsetToken(snippetName, 1, snippetToken);
const leftParenToken = sourceCode.getTokenBefore(node.params[0] || sourceCode.getLastToken(node), {
filter: isOpeningParenToken,
includeComments: false
});
const rightParenToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || leftParenToken, {
filter: isClosingParenToken,
includeComments: false
});
offsets.setOffsetToken(leftParenToken, 1, snippetName);
offsets.setOffsetElementList(node.params, leftParenToken, rightParenToken, 1);
const closeOpenTagToken = sourceCode.getTokenAfter(rightParenToken);
offsets.setOffsetToken(closeOpenTagToken, 0, openToken);
for (const child of node.children) {
const token = sourceCode.getFirstToken(child, {
includeComments: false,
filter: isNotWhitespace
});
offsets.setOffsetToken(token, 1, openToken);
}
const [openCloseTagToken, endSnippetToken, closeCloseTagToken] = sourceCode.getLastTokens(node, { count: 3, includeComments: false });
offsets.setOffsetToken(openCloseTagToken, 0, openToken);
offsets.setOffsetToken(endSnippetToken, 1, openCloseTagToken);
offsets.setOffsetToken(closeCloseTagToken, 0, openCloseTagToken);
},
// ----------------------------------------------------------------------
// COMMENTS
// ----------------------------------------------------------------------
SvelteHTMLComment(_node) {
// noop
},
// ----------------------------------------------------------------------
// NAMES
// ----------------------------------------------------------------------
SvelteName(_node) {
// noop
},
SvelteMemberExpressionName(_node) {
// noop
}
};
return visitor;
}
/**
* Check whether the given node is not an empty text node.
* @param node The node to check.
* @returns `false` if the token is empty text node.
*/
function isNotEmptyTextNode(node) {
return !(node.type === 'SvelteText' && node.value.trim() === '');
}

View File

@@ -0,0 +1,11 @@
import type { IndentContext } from './commons.js';
import type { TSNodeListener } from '../../types-for-node.js';
type NodeListener = TSNodeListener;
/**
* Creates AST event handlers for svelte nodes.
*
* @param context The rule context.
* @returns AST event handlers.
*/
export declare function defineVisitor(context: IndentContext): NodeListener;
export {};

View File

@@ -0,0 +1,915 @@
import { isClosingBracketToken, isClosingParenToken, isNotClosingParenToken, isNotOpeningBraceToken, isOpeningBraceToken, isOpeningBracketToken, isOpeningParenToken, isSemicolonToken } from '@eslint-community/eslint-utils';
import { isBeginningOfLine } from './commons.js';
import { getFirstAndLastTokens } from './commons.js';
/**
* Creates AST event handlers for svelte nodes.
*
* @param context The rule context.
* @returns AST event handlers.
*/
export function defineVisitor(context) {
const { offsets, sourceCode } = context;
const visitor = {
TSTypeAnnotation(node) {
// : Type
// => Type
const [colonOrArrowToken, secondToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
const baseToken = sourceCode.getFirstToken(node.parent);
offsets.setOffsetToken([colonOrArrowToken, secondToken], 1, baseToken);
const before = sourceCode.getTokenBefore(colonOrArrowToken);
if (before && before.value === '?') {
offsets.setOffsetToken(before, 1, baseToken);
}
},
TSAsExpression(node) {
// foo as T
// or
// foo satisfies T
const expressionTokens = getFirstAndLastTokens(sourceCode, node.expression);
const asOrSatisfiesToken = sourceCode.getTokenAfter(expressionTokens.lastToken);
offsets.setOffsetToken([asOrSatisfiesToken, getFirstAndLastTokens(sourceCode, node.typeAnnotation).firstToken], 1, expressionTokens.firstToken);
},
TSSatisfiesExpression(node) {
// foo satisfies T
visitor.TSAsExpression(node);
},
TSTypeReference(node) {
// T<U>
// or
// const ErrorMap = Map<string, Error>
// ^^^^^^^^^^^^^^^^^^
const typeArguments = node.typeArguments ??
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Support old typescript-eslint
node.typeParameters;
if (typeArguments) {
const firstToken = sourceCode.getFirstToken(node);
offsets.setOffsetToken(sourceCode.getFirstToken(typeArguments), 1, firstToken);
}
},
TSInstantiationExpression(node) {
// const ErrorMap = Map<string, Error>
// ^^^^^^^^^^^^^^^^^^
visitor.TSTypeReference(node);
},
TSTypeParameterInstantiation(node) {
// <T>
offsets.setOffsetElementList(node.params, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), 1);
},
TSTypeParameterDeclaration(node) {
// <T>
visitor.TSTypeParameterInstantiation(node);
},
TSTypeAliasDeclaration(node) {
// type T = {}
const typeToken = sourceCode.getFirstToken(node);
const idToken = sourceCode.getFirstToken(node.id);
offsets.setOffsetToken(idToken, 1, typeToken);
let eqToken;
if (node.typeParameters) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.typeParameters), 1, idToken);
eqToken = sourceCode.getTokenAfter(node.typeParameters);
}
else {
eqToken = sourceCode.getTokenAfter(node.id);
}
const initToken = sourceCode.getTokenAfter(eqToken);
offsets.setOffsetToken([eqToken, initToken], 1, idToken);
},
TSFunctionType(node) {
// ()=>void
const firstToken = sourceCode.getFirstToken(node);
// new or < or (
let currToken = firstToken;
if (node.type === 'TSConstructorType') {
// currToken is new token
// < or (
currToken = sourceCode.getTokenAfter(currToken);
offsets.setOffsetToken(currToken, 1, firstToken);
}
if (node.typeParameters) {
// currToken is < token
// (
currToken = sourceCode.getTokenAfter(node.typeParameters);
offsets.setOffsetToken(currToken, 1, firstToken);
}
const leftParenToken = currToken;
const rightParenToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || leftParenToken, { filter: isClosingParenToken, includeComments: false });
offsets.setOffsetElementList(node.params, leftParenToken, rightParenToken, 1);
const arrowToken = sourceCode.getTokenAfter(rightParenToken);
offsets.setOffsetToken(arrowToken, 1, leftParenToken);
},
TSConstructorType(node) {
// new ()=>void
visitor.TSFunctionType(node);
},
TSTypeLiteral(node) {
// {foo:any}
offsets.setOffsetElementList(node.members, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), 1);
},
TSPropertySignature(node) {
// { target:any }
// ^^^^^^^^^^
const firstToken = sourceCode.getFirstToken(node);
const keyTokens = getFirstAndLastTokens(sourceCode, node.key);
let keyLast;
if (node.computed) {
const closeBracket = sourceCode.getTokenAfter(keyTokens.lastToken);
offsets.setOffsetElementList([node.key], firstToken, closeBracket, 1);
keyLast = closeBracket;
}
else {
keyLast = keyTokens.lastToken;
}
if (node.typeAnnotation) {
const typeAnnotationToken = sourceCode.getFirstToken(node.typeAnnotation);
offsets.setOffsetToken([...sourceCode.getTokensBetween(keyLast, typeAnnotationToken), typeAnnotationToken], 1, firstToken);
}
else if (node.optional) {
const qToken = sourceCode.getLastToken(node);
offsets.setOffsetToken(qToken, 1, firstToken);
}
},
TSIndexSignature(node) {
// { [k: string ]: string[] };
// ^^^^^^^^^^^^^^^^^^^^^^
const leftBracketToken = sourceCode.getFirstToken(node);
const rightBracketToken = sourceCode.getTokenAfter(node.parameters[node.parameters.length - 1] || leftBracketToken, { filter: isClosingBracketToken, includeComments: false });
offsets.setOffsetElementList(node.parameters, leftBracketToken, rightBracketToken, 1);
const keyLast = rightBracketToken;
if (node.typeAnnotation) {
const typeAnnotationToken = sourceCode.getFirstToken(node.typeAnnotation);
offsets.setOffsetToken([...sourceCode.getTokensBetween(keyLast, typeAnnotationToken), typeAnnotationToken], 1, leftBracketToken);
}
},
TSArrayType(node) {
// T[]
const firstToken = sourceCode.getFirstToken(node);
offsets.setOffsetToken(sourceCode.getLastTokens(node, { count: 2, includeComments: false }), 0, firstToken);
},
TSTupleType(node) {
// [T, U]
offsets.setOffsetElementList(node.elementTypes, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), 1);
},
TSQualifiedName(node) {
// A.B
const objectToken = sourceCode.getFirstToken(node);
const dotToken = sourceCode.getTokenBefore(node.right);
const propertyToken = sourceCode.getTokenAfter(dotToken);
offsets.setOffsetToken([dotToken, propertyToken], 1, objectToken);
},
TSIndexedAccessType(node) {
// A[B]
const objectToken = sourceCode.getFirstToken(node);
const leftBracketToken = sourceCode.getTokenBefore(node.indexType, {
filter: isOpeningBracketToken,
includeComments: false
});
const rightBracketToken = sourceCode.getTokenAfter(node.indexType, {
filter: isClosingBracketToken,
includeComments: false
});
offsets.setOffsetToken(leftBracketToken, 1, objectToken);
offsets.setOffsetElementList([node.indexType], leftBracketToken, rightBracketToken, 1);
},
TSUnionType(node) {
// A | B
const firstToken = sourceCode.getFirstToken(node);
const types = [...node.types];
if (getFirstAndLastTokens(sourceCode, types[0]).firstToken === firstToken) {
types.shift();
}
offsets.setOffsetElementList(types, firstToken, null, isBeginningOfLine(sourceCode, firstToken) ? 0 : 1);
},
TSIntersectionType(node) {
// A & B
visitor.TSUnionType(node);
},
TSMappedType(node) {
// {[key in foo]: bar}
const leftBraceToken = sourceCode.getFirstToken(node);
const leftBracketToken = sourceCode.getTokenBefore(node.key || node.typeParameter);
const rightBracketToken = sourceCode.getTokenAfter(node.nameType || node.constraint || node.typeParameter);
offsets.setOffsetToken([...sourceCode.getTokensBetween(leftBraceToken, leftBracketToken), leftBracketToken], 1, leftBraceToken);
offsets.setOffsetElementList([node.key, node.constraint, node.typeParameter, node.nameType], leftBracketToken, rightBracketToken, 1);
if (node.key && node.constraint) {
const firstToken = sourceCode.getFirstToken(node.key);
offsets.setOffsetToken([
...sourceCode.getTokensBetween(firstToken, node.constraint),
sourceCode.getFirstToken(node.constraint)
], 1, firstToken);
}
const rightBraceToken = sourceCode.getLastToken(node);
if (node.typeAnnotation) {
const typeAnnotationToken = sourceCode.getFirstToken(node.typeAnnotation);
offsets.setOffsetToken([
...sourceCode.getTokensBetween(rightBracketToken, typeAnnotationToken),
typeAnnotationToken
], 1, leftBraceToken);
}
else {
offsets.setOffsetToken([...sourceCode.getTokensBetween(rightBracketToken, rightBraceToken)], 1, leftBraceToken);
}
offsets.setOffsetToken(rightBraceToken, 0, leftBraceToken);
},
TSTypeParameter(node) {
// {[key in foo]: bar}
// ^^^^^^^^^^
// type T<U extends V = W>
// ^^^^^^^^^^^^^^^
const [firstToken, ...afterTokens] = sourceCode.getTokens(node);
for (const child of [node.constraint, node.default]) {
if (!child) {
continue;
}
const [, ...removeTokens] = sourceCode.getTokens(child);
for (const token of removeTokens) {
const i = afterTokens.indexOf(token);
if (i >= 0) {
afterTokens.splice(i, 1);
}
}
}
const secondToken = afterTokens.shift();
if (!secondToken) {
return;
}
offsets.setOffsetToken(secondToken, 1, firstToken);
if (secondToken.value === 'extends') {
let prevToken = null;
let token = afterTokens.shift();
while (token) {
if (token.value === '=') {
break;
}
offsets.setOffsetToken(token, 1, secondToken);
prevToken = token;
token = afterTokens.shift();
}
while (token) {
offsets.setOffsetToken(token, 1, prevToken || secondToken);
token = afterTokens.shift();
}
}
else {
offsets.setOffsetToken(afterTokens, 1, firstToken);
}
},
TSConditionalType(node) {
// T extends Foo ? T : U
const checkTypeToken = sourceCode.getFirstToken(node);
const extendsToken = sourceCode.getTokenAfter(node.checkType);
const extendsTypeToken = sourceCode.getFirstToken(node.extendsType);
offsets.setOffsetToken(extendsToken, 1, checkTypeToken);
offsets.setOffsetToken(extendsTypeToken, 1, extendsToken);
const questionToken = sourceCode.getTokenAfter(node.extendsType, {
filter: isNotClosingParenToken,
includeComments: false
});
const consequentToken = sourceCode.getTokenAfter(questionToken);
const colonToken = sourceCode.getTokenAfter(node.trueType, {
filter: isNotClosingParenToken,
includeComments: false
});
const alternateToken = sourceCode.getTokenAfter(colonToken);
let baseNode = node;
let parent = baseNode.parent;
while (parent && parent.type === 'TSConditionalType' && parent.falseType === baseNode) {
baseNode = parent;
parent = baseNode.parent;
}
const baseToken = sourceCode.getFirstToken(baseNode);
offsets.setOffsetToken([questionToken, colonToken], 1, baseToken);
offsets.setOffsetToken(consequentToken, 1, questionToken);
offsets.setOffsetToken(alternateToken, 1, colonToken);
},
TSInterfaceDeclaration(node) {
// interface I {}
const interfaceToken = sourceCode.getFirstToken(node);
offsets.setOffsetToken(sourceCode.getFirstToken(node.id), 1, interfaceToken);
if (node.typeParameters != null) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.typeParameters), 1, sourceCode.getFirstToken(node.id));
}
if (node.extends != null && node.extends.length) {
const extendsToken = sourceCode.getTokenBefore(node.extends[0]);
offsets.setOffsetToken(extendsToken, 1, interfaceToken);
offsets.setOffsetElementList(node.extends, extendsToken, null, 1);
}
// It may not calculate the correct location because the visitor key is not provided.
// if (node.implements != null && node.implements.length) {
// const implementsToken = sourceCode.getTokenBefore(node.implements[0])!
// offsets.setOffsetToken(implementsToken, 1, interfaceToken)
// offsets.setOffsetElementList( node.implements, implementsToken, null, 1)
// }
const bodyToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyToken, 0, interfaceToken);
},
TSInterfaceBody(node) {
offsets.setOffsetElementList(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), 1);
},
TSClassImplements(node) {
// class C implements T {}
// ^
const typeArguments = node.typeArguments ??
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Support old typescript-eslint
node.typeParameters;
if (typeArguments) {
offsets.setOffsetToken(sourceCode.getFirstToken(typeArguments), 1, sourceCode.getFirstToken(node));
}
},
TSInterfaceHeritage(node) {
// interface I extends E implements T {}
// ^ ^
visitor.TSClassImplements(node);
},
TSEnumDeclaration(node) {
// enum E {}
const firstToken = sourceCode.getFirstToken(node);
const idTokens = getFirstAndLastTokens(sourceCode, node.id);
const prefixTokens = sourceCode.getTokensBetween(firstToken, idTokens.firstToken);
offsets.setOffsetToken(prefixTokens, 0, firstToken);
offsets.setOffsetToken(idTokens.firstToken, 1, firstToken);
const leftBraceToken = sourceCode.getTokenAfter(idTokens.lastToken);
const rightBraceToken = sourceCode.getLastToken(node);
offsets.setOffsetToken(leftBraceToken, 0, firstToken);
if (node.members)
offsets.setOffsetElementList(node.members, leftBraceToken, rightBraceToken, 1);
},
TSEnumBody(node) {
// enum E {...}
// ^^^^^
const leftBraceToken = sourceCode.getFirstToken(node);
const rightBraceToken = sourceCode.getLastToken(node);
offsets.setOffsetElementList(node.members, leftBraceToken, rightBraceToken, 1);
},
TSModuleDeclaration(node) {
const firstToken = sourceCode.getFirstToken(node);
const idTokens = getFirstAndLastTokens(sourceCode, node.id);
const prefixTokens = sourceCode.getTokensBetween(firstToken, idTokens.firstToken);
offsets.setOffsetToken(prefixTokens, 0, firstToken);
offsets.setOffsetToken(idTokens.firstToken, 1, firstToken);
if (node.body) {
const bodyFirstToken = sourceCode.getFirstToken(node.body);
offsets.setOffsetToken(bodyFirstToken, isOpeningBraceToken(bodyFirstToken) ? 0 : 1, firstToken);
}
},
TSModuleBlock(node) {
visitor.TSInterfaceBody(node);
},
TSMethodSignature(node) {
// fn(arg: A): R | null;
const firstToken = sourceCode.getFirstToken(node);
const keyTokens = getFirstAndLastTokens(sourceCode, node.key);
let keyLast;
if (node.computed) {
const closeBracket = sourceCode.getTokenAfter(keyTokens.lastToken);
offsets.setOffsetElementList([node.key], firstToken, closeBracket, 1);
keyLast = closeBracket;
}
else {
keyLast = keyTokens.lastToken;
}
const leftParenToken = sourceCode.getTokenAfter(keyLast, {
filter: isOpeningParenToken,
includeComments: false
});
offsets.setOffsetToken([...sourceCode.getTokensBetween(keyLast, leftParenToken), leftParenToken], 1, firstToken);
const rightParenToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || leftParenToken, { filter: isClosingParenToken, includeComments: false });
offsets.setOffsetElementList(node.params, leftParenToken, rightParenToken, 1);
if (node.returnType) {
const typeAnnotationToken = sourceCode.getFirstToken(node.returnType);
offsets.setOffsetToken([...sourceCode.getTokensBetween(keyLast, typeAnnotationToken), typeAnnotationToken], 1, firstToken);
}
},
TSCallSignatureDeclaration(node) {
// interface A { <T> (e: E): R }
// ^^^^^^^^^^^^^
const firstToken = sourceCode.getFirstToken(node);
// new or < or (
let currToken = firstToken;
if (node.type === 'TSConstructSignatureDeclaration') {
// currToken is new token
// < or (
currToken = sourceCode.getTokenAfter(currToken);
offsets.setOffsetToken(currToken, 1, firstToken);
}
if (node.typeParameters) {
// currToken is < token
// (
currToken = sourceCode.getTokenAfter(node.typeParameters);
offsets.setOffsetToken(currToken, 1, firstToken);
}
const leftParenToken = currToken;
const rightParenToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || leftParenToken, { filter: isClosingParenToken, includeComments: false });
offsets.setOffsetElementList(node.params, leftParenToken, rightParenToken, 1);
if (node.returnType) {
const typeAnnotationToken = sourceCode.getFirstToken(node.returnType);
offsets.setOffsetToken([
...sourceCode.getTokensBetween(rightParenToken, typeAnnotationToken),
typeAnnotationToken
], 1, firstToken);
}
},
TSConstructSignatureDeclaration(node) {
// interface A { new <T> (e: E): R }
// ^^^^^^^^^^^^^^^^^
visitor.TSCallSignatureDeclaration(node);
},
TSEmptyBodyFunctionExpression(node) {
const firstToken = sourceCode.getFirstToken(node);
let leftParenToken, bodyBaseToken;
if (firstToken.type === 'Punctuator') {
// method
leftParenToken = firstToken;
bodyBaseToken = sourceCode.getFirstToken(node.parent);
}
else {
let nextToken = sourceCode.getTokenAfter(firstToken);
let nextTokenOffset = 0;
while (nextToken && !isOpeningParenToken(nextToken) && nextToken.value !== '<') {
if (nextToken.value === '*' || (node.id && nextToken.range[0] === node.id.range[0])) {
nextTokenOffset = 1;
}
offsets.setOffsetToken(nextToken, nextTokenOffset, firstToken);
nextToken = sourceCode.getTokenAfter(nextToken);
}
leftParenToken = nextToken;
bodyBaseToken = firstToken;
}
if (!isOpeningParenToken(leftParenToken) && node.typeParameters) {
leftParenToken = sourceCode.getTokenAfter(node.typeParameters);
}
const rightParenToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || leftParenToken, { filter: isClosingParenToken, includeComments: false });
offsets.setOffsetToken(leftParenToken, 1, bodyBaseToken);
offsets.setOffsetElementList(node.params, leftParenToken, rightParenToken, 1);
},
TSDeclareFunction(node) {
// function fn(): void;
visitor.TSEmptyBodyFunctionExpression(node);
},
TSTypeOperator(node) {
// keyof T
const firstToken = sourceCode.getFirstToken(node);
const nextToken = sourceCode.getTokenAfter(firstToken);
offsets.setOffsetToken(nextToken, 1, firstToken);
},
TSTypeQuery(node) {
// type T = typeof a
visitor.TSTypeOperator(node);
},
TSInferType(node) {
// infer U
visitor.TSTypeOperator(node);
},
TSTypePredicate(node) {
// v is T
const firstToken = sourceCode.getFirstToken(node);
const opToken = sourceCode.getTokenAfter(node.parameterName, {
filter: isNotClosingParenToken,
includeComments: false
});
const rightToken = node.typeAnnotation && getFirstAndLastTokens(sourceCode, node.typeAnnotation).firstToken;
offsets.setOffsetToken([opToken, rightToken], 1, getFirstAndLastTokens(sourceCode, firstToken).firstToken);
},
TSAbstractMethodDefinition(node) {
const { keyNode, valueNode } = node.type === 'TSEnumMember'
? { keyNode: node.id, valueNode: node.initializer }
: { keyNode: node.key, valueNode: node.value };
const firstToken = sourceCode.getFirstToken(node);
const keyTokens = getFirstAndLastTokens(sourceCode, keyNode);
const prefixTokens = sourceCode.getTokensBetween(firstToken, keyTokens.firstToken);
if (node.computed) {
prefixTokens.pop(); // pop [
}
offsets.setOffsetToken(prefixTokens, 0, firstToken);
let lastKeyToken;
if (node.computed) {
const leftBracketToken = sourceCode.getTokenBefore(keyTokens.firstToken);
const rightBracketToken = (lastKeyToken = sourceCode.getTokenAfter(keyTokens.lastToken));
offsets.setOffsetToken(leftBracketToken, 0, firstToken);
offsets.setOffsetElementList([keyNode], leftBracketToken, rightBracketToken, 1);
}
else {
offsets.setOffsetToken(keyTokens.firstToken, 0, firstToken);
lastKeyToken = keyTokens.lastToken;
}
if (valueNode != null) {
const initToken = sourceCode.getFirstToken(valueNode);
offsets.setOffsetToken([...sourceCode.getTokensBetween(lastKeyToken, initToken), initToken], 1, lastKeyToken);
}
},
TSAbstractPropertyDefinition(node) {
visitor.TSAbstractMethodDefinition(node);
},
TSEnumMember(node) {
visitor.TSAbstractMethodDefinition(node);
},
TSAbstractAccessorProperty(node) {
visitor.TSAbstractMethodDefinition(node);
},
TSOptionalType(node) {
// [number?]
// ^^^^^^^
offsets.setOffsetToken(sourceCode.getLastToken(node), 1, sourceCode.getFirstToken(node));
},
TSNonNullExpression(node) {
// v!
visitor.TSOptionalType(node);
},
TSJSDocNonNullableType(
// @ts-expect-error -- Missing TSJSDocNonNullableType type
node) {
// T!
visitor.TSOptionalType(node);
},
TSTypeAssertion(node) {
// <const>
const firstToken = sourceCode.getFirstToken(node);
const expressionToken = getFirstAndLastTokens(sourceCode, node.expression).firstToken;
offsets.setOffsetElementList([node.typeAnnotation], firstToken, sourceCode.getTokenBefore(expressionToken), 1);
offsets.setOffsetToken(expressionToken, 1, firstToken);
},
TSImportType(node) {
// import('foo').B
const typeArguments = node.typeArguments ??
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Support old typescript-eslint
node.typeParameters;
const firstToken = sourceCode.getFirstToken(node);
const leftParenToken = sourceCode.getTokenAfter(firstToken, {
filter: isOpeningParenToken,
includeComments: false
});
offsets.setOffsetToken(leftParenToken, 1, firstToken);
const args = [];
if (node.source) {
args.push(node.source);
if (node.options) {
args.push(node.options);
}
}
else {
// typescript-eslint<v8.48 node
args.push(node.argument ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- typescript-eslint<v6 node
node.parameter);
}
const rightParenToken = sourceCode.getTokenAfter(args[args.length - 1], {
filter: isClosingParenToken,
includeComments: false
});
offsets.setOffsetElementList(args, leftParenToken, rightParenToken, 1);
if (node.qualifier) {
const dotToken = sourceCode.getTokenBefore(node.qualifier);
const propertyToken = sourceCode.getTokenAfter(dotToken);
offsets.setOffsetToken([dotToken, propertyToken], 1, firstToken);
}
if (typeArguments) {
offsets.setOffsetToken(sourceCode.getFirstToken(typeArguments), 1, firstToken);
}
},
TSParameterProperty(node) {
// constructor(private a)
const firstToken = sourceCode.getFirstToken(node);
const parameterToken = sourceCode.getFirstToken(node.parameter);
offsets.setOffsetToken([...sourceCode.getTokensBetween(firstToken, parameterToken), parameterToken], 1, firstToken);
},
TSImportEqualsDeclaration(node) {
// import foo = require('foo')
const importToken = sourceCode.getFirstToken(node);
const idTokens = getFirstAndLastTokens(sourceCode, node.id);
offsets.setOffsetToken(idTokens.firstToken, 1, importToken);
const opToken = sourceCode.getTokenAfter(idTokens.lastToken);
offsets.setOffsetToken([opToken, sourceCode.getFirstToken(node.moduleReference)], 1, idTokens.lastToken);
},
TSExternalModuleReference(node) {
// require('foo')
const requireToken = sourceCode.getFirstToken(node);
const leftParenToken = sourceCode.getTokenAfter(requireToken, {
filter: isOpeningParenToken,
includeComments: false
});
const rightParenToken = sourceCode.getLastToken(node);
offsets.setOffsetToken(leftParenToken, 1, requireToken);
offsets.setOffsetElementList([node.expression], leftParenToken, rightParenToken, 1);
},
TSExportAssignment(node) {
// export = foo
const exportNode = sourceCode.getFirstToken(node);
const exprTokens = getFirstAndLastTokens(sourceCode, node.expression);
const opToken = sourceCode.getTokenBefore(exprTokens.firstToken);
offsets.setOffsetToken([opToken, exprTokens.firstToken], 1, exportNode);
},
TSNamedTupleMember(node) {
// [a: string, ...b: string[]]
// ^^^^^^^^^
const labelToken = sourceCode.getFirstToken(node);
const elementTokens = getFirstAndLastTokens(sourceCode, node.elementType);
offsets.setOffsetToken([
...sourceCode.getTokensBetween(labelToken, elementTokens.firstToken),
elementTokens.firstToken
], 1, labelToken);
},
TSRestType(node) {
// [a: string, ...b: string[]]
// ^^^^^^^^^^^^^^
const firstToken = sourceCode.getFirstToken(node);
const nextToken = sourceCode.getTokenAfter(firstToken);
offsets.setOffsetToken(nextToken, 1, firstToken);
},
TSNamespaceExportDeclaration(node) {
const firstToken = sourceCode.getFirstToken(node);
const idToken = sourceCode.getFirstToken(node.id);
offsets.setOffsetToken([...sourceCode.getTokensBetween(firstToken, idToken), idToken], 1, firstToken);
},
TSTemplateLiteralType(node) {
const firstToken = sourceCode.getFirstToken(node);
const quasiTokens = node.quasis.slice(1).map((n) => sourceCode.getFirstToken(n));
const expressionToken = node.quasis.slice(0, -1).map((n) => sourceCode.getTokenAfter(n));
offsets.setOffsetToken(quasiTokens, 0, firstToken);
offsets.setOffsetToken(expressionToken, 1, firstToken);
},
// ----------------------------------------------------------------------
// NON-STANDARD NODES
// ----------------------------------------------------------------------
Decorator(node) {
// @Decorator
const [atToken, secondToken] = sourceCode.getFirstTokens(node, {
count: 2,
includeComments: false
});
offsets.setOffsetToken(secondToken, 0, atToken);
const parent = node.parent;
const { decorators } = parent;
if (!decorators || decorators.length === 0) {
return;
}
if (decorators[0] === node) {
if (parent.range[0] === node.range[0]) {
const startParentToken = sourceCode.getTokenAfter(decorators[decorators?.length - 1]);
offsets.setOffsetToken(startParentToken, 0, atToken);
}
else {
const startParentToken = sourceCode.getFirstToken(parent.parent &&
(parent.parent.type === 'ExportDefaultDeclaration' ||
parent.parent.type === 'ExportNamedDeclaration') &&
node.range[0] < parent.parent.range[0]
? parent.parent
: parent);
offsets.copyOffset(atToken.range[0], startParentToken.range[0]);
}
}
else {
offsets.setOffsetToken(atToken, 0, sourceCode.getFirstToken(decorators[0]));
}
},
AccessorProperty(node) {
const keyNode = node.key;
const valueNode = node.value;
const firstToken = sourceCode.getFirstToken(node);
const keyTokens = getFirstAndLastTokens(sourceCode, keyNode);
const prefixTokens = sourceCode.getTokensBetween(firstToken, keyTokens.firstToken);
if (node.computed) {
prefixTokens.pop(); // pop [
}
offsets.setOffsetToken(prefixTokens, 0, firstToken);
let lastKeyToken;
if (node.computed) {
const leftBracketToken = sourceCode.getTokenBefore(keyTokens.firstToken);
const rightBracketToken = (lastKeyToken = sourceCode.getTokenAfter(keyTokens.lastToken));
offsets.setOffsetToken(leftBracketToken, 0, firstToken);
offsets.setOffsetElementList([keyNode], leftBracketToken, rightBracketToken, 1);
}
else {
offsets.setOffsetToken(keyTokens.firstToken, 0, firstToken);
lastKeyToken = keyTokens.lastToken;
}
if (valueNode != null) {
const initToken = sourceCode.getFirstToken(valueNode);
offsets.setOffsetToken([...sourceCode.getTokensBetween(lastKeyToken, initToken), initToken], 1, lastKeyToken);
}
},
StaticBlock(node) {
const firstToken = sourceCode.getFirstToken(node);
let next = sourceCode.getTokenAfter(firstToken);
while (next && isNotOpeningBraceToken(next)) {
offsets.setOffsetToken(next, 0, firstToken);
next = sourceCode.getTokenAfter(next);
}
offsets.setOffsetToken(next, 0, firstToken);
offsets.setOffsetElementList(node.body, next, sourceCode.getLastToken(node), 1);
},
ImportAttribute(node) {
const firstToken = sourceCode.getFirstToken(node);
const keyTokens = getFirstAndLastTokens(sourceCode, node.key);
const prefixTokens = sourceCode.getTokensBetween(firstToken, keyTokens.firstToken);
offsets.setOffsetToken(prefixTokens, 0, firstToken);
offsets.setOffsetToken(keyTokens.firstToken, 0, firstToken);
const initToken = sourceCode.getFirstToken(node.value);
offsets.setOffsetToken([...sourceCode.getTokensBetween(keyTokens.lastToken, initToken), initToken], 1, keyTokens.lastToken);
},
// ----------------------------------------------------------------------
// SINGLE TOKEN NODES
// ----------------------------------------------------------------------
// VALUES KEYWORD
TSAnyKeyword() {
// noop
},
TSBigIntKeyword() {
// noop
},
TSBooleanKeyword() {
// noop
},
TSNeverKeyword() {
// noop
},
TSNullKeyword() {
// noop
},
TSNumberKeyword() {
// noop
},
TSObjectKeyword() {
// noop
},
TSStringKeyword() {
// noop
},
TSSymbolKeyword() {
// noop
},
TSUndefinedKeyword() {
// noop
},
TSUnknownKeyword() {
// noop
},
TSVoidKeyword() {
// noop
},
// MODIFIERS KEYWORD
TSAbstractKeyword() {
// noop
},
TSAsyncKeyword() {
// noop
},
TSPrivateKeyword() {
// noop
},
TSProtectedKeyword() {
// noop
},
TSPublicKeyword() {
// noop
},
TSReadonlyKeyword() {
// noop
},
TSStaticKeyword() {
// noop
},
// OTHERS KEYWORD
TSDeclareKeyword() {
// noop
},
TSExportKeyword() {
// noop
},
TSIntrinsicKeyword() {
// noop
},
// OTHERS
TSThisType() {
// noop
},
// ----------------------------------------------------------------------
// WRAPPER NODES
// ----------------------------------------------------------------------
TSLiteralType() {
// noop
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
const commonsVisitor = {
// Process semicolons.
['TSTypeAliasDeclaration, TSCallSignatureDeclaration, TSConstructSignatureDeclaration, TSImportEqualsDeclaration,' +
'TSAbstractMethodDefinition, TSAbstractPropertyDefinition, AccessorProperty, TSAbstractAccessorProperty, TSEnumMember,' +
'TSPropertySignature, TSIndexSignature, TSMethodSignature,' +
'TSAbstractClassProperty, ClassProperty'](node) {
const firstToken = sourceCode.getFirstToken(node);
const lastToken = sourceCode.getLastToken(node);
if (isSemicolonToken(lastToken) && firstToken !== lastToken) {
const next = sourceCode.getTokenAfter(lastToken);
if (!next || lastToken.loc.start.line < next.loc.start.line) {
// End of line semicolons
offsets.setOffsetToken(lastToken, 0, firstToken);
}
}
},
'*[type=/^TS/]'(node) {
if (node.type !== 'TSAnyKeyword' &&
node.type !== 'TSArrayType' &&
node.type !== 'TSBigIntKeyword' &&
node.type !== 'TSBooleanKeyword' &&
node.type !== 'TSConditionalType' &&
node.type !== 'TSConstructorType' &&
node.type !== 'TSFunctionType' &&
node.type !== 'TSImportType' &&
node.type !== 'TSIndexedAccessType' &&
node.type !== 'TSInferType' &&
node.type !== 'TSIntersectionType' &&
node.type !== 'TSIntrinsicKeyword' &&
node.type !== 'TSLiteralType' &&
node.type !== 'TSMappedType' &&
node.type !== 'TSNamedTupleMember' &&
node.type !== 'TSNeverKeyword' &&
node.type !== 'TSNullKeyword' &&
node.type !== 'TSNumberKeyword' &&
node.type !== 'TSObjectKeyword' &&
node.type !== 'TSOptionalType' &&
node.type !== 'TSRestType' &&
node.type !== 'TSStringKeyword' &&
node.type !== 'TSSymbolKeyword' &&
node.type !== 'TSTemplateLiteralType' &&
node.type !== 'TSThisType' &&
node.type !== 'TSTupleType' &&
node.type !== 'TSTypeLiteral' &&
node.type !== 'TSTypeOperator' &&
node.type !== 'TSTypePredicate' &&
node.type !== 'TSTypeQuery' &&
node.type !== 'TSTypeReference' &&
node.type !== 'TSUndefinedKeyword' &&
node.type !== 'TSUnionType' &&
node.type !== 'TSUnknownKeyword' &&
node.type !== 'TSVoidKeyword') {
return;
}
const typeNode = node;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
if (typeNode.parent.type === 'TSParenthesizedType') {
return;
}
// Process parentheses.
let leftToken = sourceCode.getTokenBefore(typeNode);
let rightToken = sourceCode.getTokenAfter(typeNode);
let firstToken = sourceCode.getFirstToken(typeNode);
while (leftToken &&
isOpeningParenToken(leftToken) &&
rightToken &&
isClosingParenToken(rightToken)) {
offsets.setOffsetToken(firstToken, 1, leftToken);
offsets.setOffsetToken(rightToken, 0, leftToken);
firstToken = leftToken;
leftToken = sourceCode.getTokenBefore(leftToken);
rightToken = sourceCode.getTokenAfter(rightToken);
}
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
const extendsESVisitor = {
['ClassDeclaration[implements], ClassDeclaration[typeParameters], ClassDeclaration[superTypeParameters],' +
'ClassExpression[implements], ClassExpression[typeParameters], ClassExpression[superTypeParameters]'](node) {
if (node.typeParameters != null) {
offsets.setOffsetToken(sourceCode.getFirstToken(node.typeParameters), 1, sourceCode.getFirstToken(node.id || node));
}
const superTypeArguments = node.superTypeArguments ??
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Support old typescript-eslint
node.superTypeParameters;
if (superTypeArguments != null && node.superClass != null) {
offsets.setOffsetToken(sourceCode.getFirstToken(superTypeArguments), 1, sourceCode.getFirstToken(node.superClass));
}
if (node.implements != null && node.implements.length) {
const classToken = sourceCode.getFirstToken(node);
const implementsToken = sourceCode.getTokenBefore(node.implements[0]);
offsets.setOffsetToken(implementsToken, 1, classToken);
offsets.setOffsetElementList(node.implements, implementsToken, null, 1);
}
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
const deprecatedVisitor = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
TSParenthesizedType(node) {
// (T)
offsets.setOffsetElementList([node.typeAnnotation], sourceCode.getFirstToken(node), sourceCode.getLastToken(node), 1);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
ClassProperty(node) {
visitor.TSAbstractMethodDefinition(node);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
TSAbstractClassProperty(node) {
visitor.TSAbstractMethodDefinition(node);
}
};
const v = visitor;
return {
...v,
...commonsVisitor,
...extendsESVisitor,
...deprecatedVisitor
};
}

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

47
node_modules/eslint-plugin-svelte/lib/rules/indent.js generated vendored Normal file
View File

@@ -0,0 +1,47 @@
import { createRule } from '../utils/index.js';
import { defineVisitor } from './indent-helpers/index.js';
export default createRule('indent', {
meta: {
docs: {
description: 'enforce consistent indentation',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: true
},
fixable: 'whitespace',
schema: [
{
type: 'object',
properties: {
indent: {
anyOf: [{ type: 'integer', minimum: 1 }, { enum: ['tab'] }]
},
indentScript: { type: 'boolean' },
switchCase: { type: 'integer', minimum: 0 },
alignAttributesVertically: { type: 'boolean' },
ignoredNodes: {
type: 'array',
items: {
allOf: [
{ type: 'string' },
{ not: { type: 'string', pattern: ':exit$' } },
{ not: { type: 'string', pattern: '^\\s*$' } }
]
},
uniqueItems: true,
additionalItems: false
}
},
additionalProperties: false
}
],
messages: {
unexpectedChar: 'Expected {{expected}} character, but found {{actual}} character.',
unexpectedIndentation: 'Expected indentation of {{expectedIndent}} {{expectedUnit}}{{expectedIndentPlural}} but found {{actualIndent}} {{actualUnit}}{{actualIndentPlural}}.'
},
type: 'layout' // "problem",
},
create(context) {
return defineVisitor(context, {});
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,329 @@
import { ReferenceTracker } from '@eslint-community/eslint-utils';
import { createRule } from '../utils/index.js';
import { findVariable } from '../utils/ast-utils.js';
import { traverseNodes } from 'svelte-eslint-parser';
/**
* Get usage of `tick`
*/
function extractTickReferences(context) {
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope);
const a = referenceTracker.iterateEsmReferences({
svelte: {
[ReferenceTracker.ESM]: true,
tick: {
[ReferenceTracker.CALL]: true
}
}
});
return Array.from(a).map(({ node, path }) => {
return {
node: node,
name: path[path.length - 1]
};
});
}
/**
* Get usage of `setTimeout`, `setInterval`, `queueMicrotask`
*/
function extractTaskReferences(context) {
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope);
const a = referenceTracker.iterateGlobalReferences({
setTimeout: { [ReferenceTracker.CALL]: true },
setInterval: { [ReferenceTracker.CALL]: true },
queueMicrotask: { [ReferenceTracker.CALL]: true }
});
return Array.from(a).map(({ node, path }) => {
return {
node: node,
name: path[path.length - 1]
};
});
}
/**
* If `node` is inside of `maybeAncestorNode`, return true.
*/
function isChildNode(maybeAncestorNode, node) {
let parent = node.parent;
while (parent) {
if (parent === maybeAncestorNode)
return true;
parent = parent.parent;
}
return false;
}
/**
* Return true if `node` is a function call.
*/
function isFunctionCall(node) {
if (node.type !== 'Identifier')
return false;
const { parent } = node;
if (parent?.type !== 'CallExpression')
return false;
return parent.callee.type === 'Identifier' && parent.callee.name === node.name;
}
/**
* Return true if `node` is a reactive variable.
*/
function isReactiveVariableNode(reactiveVariableReferences, node) {
if (node.type !== 'Identifier')
return false;
return reactiveVariableReferences.includes(node);
}
/**
* e.g. foo.bar = baz + 1
* If node is `foo`, return true.
* Otherwise, return false.
*/
function isNodeForAssign(node) {
const { parent } = node;
if (parent?.type === 'AssignmentExpression') {
return parent.left.type === 'Identifier' && parent.left.name === node.name;
}
return (parent?.type === 'MemberExpression' &&
parent.parent?.type === 'AssignmentExpression' &&
parent.parent.left.type === 'MemberExpression' &&
parent.parent.left.object.type === 'Identifier' &&
parent.parent.left.object.name === node.name);
}
/**
* Return true if `node` is inside of `then` or `catch`.
*/
function isPromiseThenOrCatchBody(node) {
if (!getDeclarationBody(node))
return false;
const { parent } = node;
if (parent?.type !== 'CallExpression' || parent?.callee?.type !== 'MemberExpression') {
return false;
}
const { property } = parent.callee;
if (property?.type !== 'Identifier')
return false;
return ['then', 'catch'].includes(property.name);
}
/**
* Get all reactive variable reference.
*/
function getReactiveVariableReferences(context) {
const scopeManager = context.sourceCode.scopeManager;
// Find the top-level (module or global) scope.
// Any variable defined at the top-level (module scope or global scope) can be made reactive.
const toplevelScope = scopeManager.globalScope?.childScopes.find((scope) => scope.type === 'module') ||
scopeManager.globalScope;
if (!toplevelScope) {
return [];
}
// Extracts all reactive references to variables defined in the top-level scope.
const reactiveVariableNodes = [];
for (const variable of toplevelScope.variables) {
for (const reference of variable.references) {
if (reference.identifier.type === 'Identifier' && !isFunctionCall(reference.identifier)) {
reactiveVariableNodes.push(reference.identifier);
}
}
}
return reactiveVariableNodes;
}
/**
* Get all tracked reactive variables.
*/
function getTrackedVariableNodes(reactiveVariableReferences, ast) {
const reactiveVariableNodes = new Set();
for (const identifier of reactiveVariableReferences) {
if (
// If the identifier is within the reactive statement range,
// it is used within the reactive statement.
ast.range[0] <= identifier.range[0] &&
identifier.range[1] <= ast.range[1]) {
reactiveVariableNodes.add(identifier);
}
}
return reactiveVariableNodes;
}
/** */
function getDeclarationBody(node, functionName) {
if (node.type === 'VariableDeclarator' &&
node.id.type === 'Identifier' &&
(!functionName || node.id.name === functionName)) {
if (node.init?.type === 'ArrowFunctionExpression' || node.init?.type === 'FunctionExpression') {
return node.init.body;
}
}
else if (node.type === 'FunctionDeclaration' &&
node.id?.type === 'Identifier' &&
(!functionName || node.id?.name === functionName)) {
return node.body;
}
else if (!functionName && node.type === 'ArrowFunctionExpression') {
return node.body;
}
return null;
}
/** */
function getFunctionDeclarationNode(context, functionCall) {
const variable = findVariable(context, functionCall);
if (!variable) {
return null;
}
for (const def of variable.defs) {
if (def.type === 'FunctionName') {
if (def.node.type === 'FunctionDeclaration') {
return def.node.body;
}
}
if (def.type === 'Variable') {
if (def.node.init &&
(def.node.init.type === 'FunctionExpression' ||
def.node.init.type === 'ArrowFunctionExpression')) {
return def.node.init.body;
}
}
}
return null;
}
/**
* If the node is inside of a function, return true.
*
* e.g. `$: await foo()`
* if `node` is `foo`, return false because reactive statement is not function.
*
* e.g. `const bar = () => foo()`
* if `node` is `foo`, return true.
*
*/
function isInsideOfFunction(node) {
let parent = node;
while (parent) {
parent = parent.parent;
if (!parent)
break;
if (parent.type === 'FunctionDeclaration' && parent.async)
return true;
if (parent.type === 'VariableDeclarator' &&
(parent.init?.type === 'FunctionExpression' ||
parent.init?.type === 'ArrowFunctionExpression') &&
parent.init?.async) {
return true;
}
}
return false;
}
/** Let's lint! */
function doLint(context, ast, callFuncIdentifiers, tickCallExpressions, taskReferences, reactiveVariableNames, reactiveVariableReferences, pIsSameTask) {
const processed = new Set();
verifyInternal(ast, callFuncIdentifiers, pIsSameTask);
/** verify for node */
function verifyInternal(ast, callFuncIdentifiers, pIsSameTask) {
if (processed.has(ast)) {
// Avoid infinite recursion with recursive references.
return;
}
processed.add(ast);
let isSameMicroTask = pIsSameTask;
const differentMicroTaskEnterNodes = [];
traverseNodes(ast, {
enterNode(node) {
// Promise.then() or Promise.catch() is called.
if (isPromiseThenOrCatchBody(node)) {
differentMicroTaskEnterNodes.push(node);
isSameMicroTask = false;
}
// `tick`, `setTimeout`, `setInterval` , `queueMicrotask` is called
for (const { node: callExpression } of [...tickCallExpressions, ...taskReferences]) {
if (isChildNode(callExpression, node)) {
differentMicroTaskEnterNodes.push(node);
isSameMicroTask = false;
}
}
// left side of await block
if (node.parent?.type === 'AssignmentExpression' &&
node.parent?.right.type === 'AwaitExpression' &&
node.parent?.left === node) {
differentMicroTaskEnterNodes.push(node);
isSameMicroTask = false;
}
if (node.type === 'Identifier' && isFunctionCall(node)) {
// traverse used functions body
const functionDeclarationNode = getFunctionDeclarationNode(context, node);
if (functionDeclarationNode) {
verifyInternal(functionDeclarationNode, [...callFuncIdentifiers, node], isSameMicroTask);
}
}
if (!isSameMicroTask) {
if (isReactiveVariableNode(reactiveVariableReferences, node) &&
reactiveVariableNames.includes(node.name) &&
isNodeForAssign(node)) {
context.report({
node,
loc: node.loc,
messageId: 'unexpected'
});
callFuncIdentifiers.forEach((callFuncIdentifier) => {
context.report({
node: callFuncIdentifier,
loc: callFuncIdentifier.loc,
messageId: 'unexpectedCall',
data: {
variableName: node.name
}
});
});
}
}
},
leaveNode(node) {
if (node.type === 'AwaitExpression') {
if (ast.parent?.type === 'SvelteReactiveStatement') {
// MEMO: It checks that `await` is used in reactive statement directly or not.
// If `await` is used in inner function of a reactive statement, result of `isInsideOfFunction` will be `true`.
if (!isInsideOfFunction(node)) {
isSameMicroTask = false;
}
}
else {
isSameMicroTask = false;
}
}
if (differentMicroTaskEnterNodes.includes(node)) {
isSameMicroTask = true;
}
}
});
}
}
export default createRule('infinite-reactive-loop', {
meta: {
docs: {
description: "Svelte runtime prevents calling the same reactive statement twice in a microtask. But between different microtask, it doesn't prevent.",
category: 'Possible Errors',
recommended: true
},
schema: [],
messages: {
unexpected: 'Possibly it may occur an infinite reactive loop.',
unexpectedCall: 'Possibly it may occur an infinite reactive loop because this function may update `{{variableName}}`.'
},
type: 'suggestion',
// Do not run this rule on Svelte 5 with runes.
conditions: [
{
svelteVersions: ['3/4']
},
{
svelteVersions: ['5'],
runes: [false, 'undetermined']
}
]
},
create(context) {
return {
['SvelteReactiveStatement']: (ast) => {
const tickCallExpressions = extractTickReferences(context);
const taskReferences = extractTaskReferences(context);
const reactiveVariableReferences = getReactiveVariableReferences(context);
const trackedVariableNodes = getTrackedVariableNodes(reactiveVariableReferences, ast);
doLint(context, ast.body, [], tickCallExpressions, taskReferences, Array.from(trackedVariableNodes).map((node) => node.name), reactiveVariableReferences, true);
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,110 @@
import { createRule } from '../utils/index.js';
/**
* Check whether the component is declared in a single line or not.
*/
function isSingleLine(node) {
return node.loc.start.line === node.loc.end.line;
}
/**
* Group attributes line by line.
*/
function groupAttributesByLine(attributes) {
const group = [];
for (const attr of attributes) {
if (group[0]?.[0]?.loc.end.line === attr.loc.start.line) {
group[0].push(attr);
}
else {
group.unshift([attr]);
}
}
return group.reverse();
}
export default createRule('max-attributes-per-line', {
meta: {
docs: {
description: 'enforce the maximum number of attributes per line',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: true
},
fixable: 'whitespace',
schema: [
{
type: 'object',
properties: {
multiline: {
type: 'number',
minimum: 1
},
singleline: {
type: 'number',
minimum: 1
}
},
additionalProperties: false
}
],
messages: {
requireNewline: "'{{name}}' should be on a new line."
},
type: 'layout'
},
create(context) {
const multilineMaximum = context.options[0]?.multiline ?? 1;
const singlelineMaximum = context.options[0]?.singleline ?? 1;
const sourceCode = context.sourceCode;
/**
* Report attributes
*/
function report(attribute) {
if (!attribute) {
return;
}
let name;
if (attribute.type === 'SvelteAttribute' ||
attribute.type === 'SvelteShorthandAttribute' ||
attribute.type === 'SvelteDirective' ||
attribute.type === 'SvelteStyleDirective' ||
attribute.type === 'SvelteSpecialDirective') {
name = sourceCode.text.slice(...attribute.key.range);
}
else {
// if (attribute.type === "SvelteSpreadAttribute")
name = sourceCode.text.slice(...attribute.range);
}
context.report({
node: attribute,
loc: attribute.loc,
messageId: 'requireNewline',
data: { name },
fix(fixer) {
// Find the closest token before the current attribute
// that is not a white space
const prevToken = sourceCode.getTokenBefore(attribute, {
includeComments: true
});
const range = [prevToken.range[1], attribute.range[0]];
return fixer.replaceTextRange(range, '\n');
}
});
}
return {
SvelteStartTag(node) {
const numberOfAttributes = node.attributes.length;
if (!numberOfAttributes)
return;
if (isSingleLine(node)) {
if (numberOfAttributes > singlelineMaximum) {
report(node.attributes[singlelineMaximum]);
}
}
else {
for (const attrs of groupAttributesByLine(node.attributes)) {
report(attrs[multilineMaximum]);
}
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,232 @@
import { Input } from 'postcss';
import tokenize from 'postcss/lib/tokenize';
import { createRule } from '../utils/index.js';
/** Check if a comment occupies the entire source line (matching ESLint core max-lines behavior). */
function isFullLineComment(line, lineNumber, loc) {
return ((loc.start.line < lineNumber || !line.slice(0, loc.start.column).trim()) &&
(loc.end.line > lineNumber || !line.slice(loc.end.column).trim()));
}
/** Collect line numbers where AST comments occupy the full line. */
function collectAstCommentLines(comments, sourceLines, startLine, endLine) {
const lines = new Set();
for (const comment of comments) {
if (comment.loc.end.line < startLine || comment.loc.start.line > endLine)
continue;
for (let i = Math.max(comment.loc.start.line, startLine); i <= Math.min(comment.loc.end.line, endLine); i++) {
if (isFullLineComment(sourceLines[i - 1], i, comment.loc)) {
lines.add(i);
}
}
}
return lines;
}
/** Collect line numbers where CSS comments occupy the full line, using postcss tokenizer. */
function collectCssCommentLines(sourceLines, startLine, endLine) {
const result = new Set();
const cssText = sourceLines.slice(startLine - 1, endLine).join('\n');
if (!cssText.trim())
return result;
try {
const input = new Input(cssText);
const tk = tokenize(input);
const commentLines = new Set();
const codeLines = new Set();
let token;
while ((token = tk.nextToken())) {
if (token[2] == null)
continue;
const startPos = input.fromOffset(token[2]);
if (!startPos)
continue;
const tokenLine = startPos.line + startLine - 1;
if (token[0] === 'comment') {
const endPos = token[3] != null ? input.fromOffset(token[3]) : null;
const commentEndLine = endPos ? endPos.line + startLine - 1 : tokenLine;
for (let i = tokenLine; i <= commentEndLine; i++) {
commentLines.add(i);
}
}
else {
codeLines.add(tokenLine);
}
}
for (const line of commentLines) {
if (!codeLines.has(line))
result.add(line);
}
}
catch {
// Malformed CSS — don't skip any lines
}
return result;
}
/** Count inner content lines, skipping blanks and/or comment lines. */
function countLines(sourceLines, startLine, endLine, skipBlankLines, commentLines) {
if (endLine - startLine <= 1)
return 0;
let count = 0;
for (let i = startLine + 1; i < endLine; i++) {
if (skipBlankLines && sourceLines[i - 1].trim().length === 0)
continue;
if (commentLines.has(i))
continue;
count++;
}
return count;
}
function isSvelteOptions(node) {
return node.name.type === 'SvelteName' && node.name.name === 'svelte:options';
}
export default createRule('max-lines-per-block', {
meta: {
docs: {
description: 'enforce maximum number of lines in svelte component blocks',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: false
},
schema: [
{
type: 'object',
properties: {
script: {
type: 'integer',
minimum: 1
},
template: {
type: 'integer',
minimum: 1
},
style: {
type: 'integer',
minimum: 1
},
skipBlankLines: {
type: 'boolean'
},
skipComments: {
type: 'boolean'
}
},
additionalProperties: false
}
],
messages: {
tooManyLines: '{{block}} block has too many lines ({{lineCount}}). Maximum allowed is {{max}}.'
},
type: 'suggestion'
},
create(context) {
const options = context.options[0] ?? {};
const scriptMax = options.script;
const templateMax = options.template;
const styleMax = options.style;
const skipBlankLines = options.skipBlankLines ?? false;
const skipComments = options.skipComments ?? false;
const sourceCode = context.sourceCode;
const htmlCommentNodes = [];
const emptySet = new Set();
return {
SvelteHTMLComment(node) {
htmlCommentNodes.push(node);
},
SvelteScriptElement(node) {
if (scriptMax == null)
return;
const commentLines = skipComments
? collectAstCommentLines(sourceCode.getAllComments(), sourceCode.lines, node.loc.start.line + 1, node.loc.end.line - 1)
: emptySet;
const lineCount = countLines(sourceCode.lines, node.loc.start.line, node.loc.end.line, skipBlankLines, commentLines);
if (lineCount > scriptMax) {
context.report({
node,
messageId: 'tooManyLines',
data: {
block: '<script>',
lineCount: String(lineCount),
max: String(scriptMax)
}
});
}
},
SvelteStyleElement(node) {
if (styleMax == null)
return;
const commentLines = skipComments
? collectCssCommentLines(sourceCode.lines, node.loc.start.line + 1, node.loc.end.line - 1)
: emptySet;
const lineCount = countLines(sourceCode.lines, node.loc.start.line, node.loc.end.line, skipBlankLines, commentLines);
if (lineCount > styleMax) {
context.report({
node,
messageId: 'tooManyLines',
data: {
block: '<style>',
lineCount: String(lineCount),
max: String(styleMax)
}
});
}
},
'Program:exit'(program) {
if (templateMax == null)
return;
const totalLines = sourceCode.lines.length;
// Exclude lines occupied by <script>, <style>, and <svelte:options>
const excludedLines = new Set();
for (const child of program.body) {
if (child.type === 'SvelteScriptElement' ||
child.type === 'SvelteStyleElement' ||
(child.type === 'SvelteElement' && isSvelteOptions(child))) {
for (let i = child.loc.start.line; i <= child.loc.end.line; i++) {
excludedLines.add(i);
}
}
}
// Collect full-line comment lines for template region
const commentLines = new Set();
if (skipComments) {
const allComments = [
...htmlCommentNodes,
...sourceCode.getAllComments()
];
for (const comment of allComments) {
for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) {
if (excludedLines.has(i))
continue;
if (isFullLineComment(sourceCode.lines[i - 1], i, comment.loc)) {
commentLines.add(i);
}
}
}
}
let templateLineCount = 0;
for (let i = 1; i <= totalLines; i++) {
if (excludedLines.has(i))
continue;
if (skipBlankLines && sourceCode.lines[i - 1].trim().length === 0)
continue;
if (commentLines.has(i))
continue;
templateLineCount++;
}
if (templateLineCount > templateMax) {
const firstTemplateNode = program.body.find((child) => child.type !== 'SvelteScriptElement' &&
child.type !== 'SvelteStyleElement' &&
!(child.type === 'SvelteElement' && isSvelteOptions(child)));
if (firstTemplateNode) {
context.report({
node: firstTemplateNode,
messageId: 'tooManyLines',
data: {
block: 'template',
lineCount: String(templateLineCount),
max: String(templateMax)
}
});
}
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,276 @@
import { isClosingBraceToken, isOpeningBraceToken } from '@eslint-community/eslint-utils';
import { createRule } from '../utils/index.js';
import { getMustacheTokens } from '../utils/ast-utils.js';
const VALUE_SCHEMA = { enum: ['never', 'always'] };
/**
* Normalize options.
*/
function parseOptions(options) {
return {
textExpressions: options?.textExpressions || 'never',
attributesAndProps: options?.attributesAndProps || 'never',
directiveExpressions: options?.directiveExpressions || 'never',
tags: {
openingBrace: options?.tags?.openingBrace || 'never',
closingBrace: options?.tags?.closingBrace || 'never'
}
};
}
export default createRule('mustache-spacing', {
meta: {
docs: {
description: 'enforce unified spacing in mustache',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: true
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
textExpressions: VALUE_SCHEMA,
attributesAndProps: VALUE_SCHEMA,
directiveExpressions: VALUE_SCHEMA,
tags: {
type: 'object',
properties: {
openingBrace: VALUE_SCHEMA,
closingBrace: {
enum: ['never', 'always', 'always-after-expression']
}
},
additionalProperties: false
}
},
additionalProperties: false
}
],
messages: {
expectedOpening: "Expected 1 space after '{', but not found.",
expectedClosing: "Expected 1 space before '}', but not found.",
unexpectedOpening: "Expected no space after '{', but found.",
unexpectedClosing: "Expected no space before '}', but found."
},
type: 'layout'
},
create(context) {
const options = parseOptions(context.options[0]);
const sourceCode = context.sourceCode;
/** Verify */
function verifyBraces(openingBrace, closingBrace, openingOption, closingOption, hasExpression) {
const firstToken = sourceCode.getTokenAfter(openingBrace, {
includeComments: true
});
if (openingOption === 'always') {
if (openingBrace.range[1] === firstToken.range[0]) {
context.report({
node: openingBrace,
messageId: 'expectedOpening',
fix: (fixer) => fixer.insertTextAfter(openingBrace, ' ')
});
}
}
else if (openingBrace.range[1] !== firstToken.range[0]) {
context.report({
loc: {
start: openingBrace.loc.start,
end: firstToken.loc.start
},
messageId: 'unexpectedOpening',
fix: (fixer) => fixer.removeRange([openingBrace.range[1], firstToken.range[0]])
});
}
if (!closingBrace) {
return;
}
const lastToken = sourceCode.getTokenBefore(closingBrace, {
includeComments: true
});
if (closingOption === 'always' ||
(closingOption === 'always-after-expression' && hasExpression)) {
if (closingBrace.range[0] === lastToken.range[1]) {
context.report({
node: closingBrace,
messageId: 'expectedClosing',
fix: (fixer) => fixer.insertTextBefore(closingBrace, ' ')
});
}
}
else if (closingBrace.range[0] !== lastToken.range[1]) {
context.report({
loc: {
start: lastToken.loc.end,
end: closingBrace.loc.end
},
messageId: 'unexpectedClosing',
fix: (fixer) => fixer.removeRange([lastToken.range[1], closingBrace.range[0]])
});
}
}
/** Verify for {expr} */
function verifyExpression(node, option) {
const mustacheTokens = getMustacheTokens(node, sourceCode);
if (!mustacheTokens) {
return;
}
verifyBraces(mustacheTokens.openToken, mustacheTokens.closeToken, option, option);
}
return {
SvelteMustacheTag(node) {
if (node.kind === 'raw') {
const mustacheTokens = getMustacheTokens(node, sourceCode);
verifyBraces(mustacheTokens.openToken, mustacheTokens.closeToken, options.tags.openingBrace, options.tags.closingBrace, true);
return;
}
let option;
if (node.parent.type === 'SvelteAttribute') {
option = options.attributesAndProps;
}
else if (node.parent.type === 'SvelteStyleDirective') {
option = options.directiveExpressions;
}
else {
option = options.textExpressions;
}
verifyExpression(node, option);
},
SvelteShorthandAttribute(node) {
verifyExpression(node, options.attributesAndProps);
},
SvelteSpreadAttribute(node) {
verifyExpression(node, options.attributesAndProps);
},
SvelteDirective(node) {
verifyExpression(node, options.directiveExpressions);
},
SvelteSpecialDirective(node) {
verifyExpression(node, options.directiveExpressions);
},
SvelteDebugTag(node) {
const mustacheTokens = getMustacheTokens(node, sourceCode);
verifyBraces(mustacheTokens.openToken, mustacheTokens.closeToken, options.tags.openingBrace, options.tags.closingBrace, true);
},
SvelteDeclarationTag(node) {
const mustacheTokens = getMustacheTokens(node, sourceCode);
verifyBraces(mustacheTokens.openToken, mustacheTokens.closeToken, options.tags.openingBrace, options.tags.closingBrace, true);
},
SvelteRenderTag(node) {
const mustacheTokens = getMustacheTokens(node, sourceCode);
verifyBraces(mustacheTokens.openToken, mustacheTokens.closeToken, options.tags.openingBrace, options.tags.closingBrace, true);
},
SvelteIfBlock(node) {
const openBlockOpeningToken = sourceCode.getFirstToken(node);
const openBlockClosingToken = sourceCode.getTokenAfter(node.expression, {
includeComments: false,
filter: isClosingBraceToken
});
verifyBraces(openBlockOpeningToken, openBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, true);
if (node.elseif) {
return;
}
const closeBlockClosingToken = sourceCode.getLastToken(node);
const closeBlockOpeningToken = sourceCode.getTokenBefore(closeBlockClosingToken, {
includeComments: false,
filter: isOpeningBraceToken
});
verifyBraces(closeBlockOpeningToken, closeBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, false);
},
SvelteElseBlock(node) {
if (node.elseif) {
return;
}
const openToken = sourceCode.getFirstToken(node);
const closeToken = sourceCode.getTokenAfter(openToken, {
includeComments: false,
filter: isClosingBraceToken
});
verifyBraces(openToken, closeToken, options.tags.openingBrace, options.tags.closingBrace, false);
},
SvelteEachBlock(node) {
const openBlockOpeningToken = sourceCode.getFirstToken(node);
const openBlockClosingToken = sourceCode.getTokenAfter(node.key || node.index || node.context || node.expression, {
includeComments: false,
filter: isClosingBraceToken
});
verifyBraces(openBlockOpeningToken, openBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, true);
const closeBlockClosingToken = sourceCode.getLastToken(node);
const closeBlockOpeningToken = sourceCode.getTokenBefore(closeBlockClosingToken, {
includeComments: false,
filter: isOpeningBraceToken
});
verifyBraces(closeBlockOpeningToken, closeBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, false);
},
SvelteKeyBlock(node) {
const openBlockOpeningToken = sourceCode.getFirstToken(node);
const openBlockClosingToken = sourceCode.getTokenAfter(node.expression, {
includeComments: false,
filter: isClosingBraceToken
});
verifyBraces(openBlockOpeningToken, openBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, true);
const closeBlockClosingToken = sourceCode.getLastToken(node);
const closeBlockOpeningToken = sourceCode.getTokenBefore(closeBlockClosingToken, {
includeComments: false,
filter: isOpeningBraceToken
});
verifyBraces(closeBlockOpeningToken, closeBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, false);
},
SvelteAwaitBlock(node) {
const closeBlockClosingToken = sourceCode.getLastToken(node);
const closeBlockOpeningToken = sourceCode.getTokenBefore(closeBlockClosingToken, {
includeComments: false,
filter: isOpeningBraceToken
});
verifyBraces(closeBlockOpeningToken, closeBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, false);
},
SvelteAwaitPendingBlock(node) {
const openBlockOpeningToken = sourceCode.getFirstToken(node);
const openBlockClosingToken = sourceCode.getTokenAfter(node.parent.expression, {
includeComments: false,
filter: isClosingBraceToken
});
verifyBraces(openBlockOpeningToken, openBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, true);
},
SvelteAwaitThenBlock(node) {
const openBlockOpeningToken = sourceCode.getFirstToken(node);
const openBlockLast = node.value || (node.awaitThen ? node.parent.expression : null);
const openBlockClosingToken = openBlockLast
? sourceCode.getTokenAfter(openBlockLast, {
includeComments: false,
filter: isClosingBraceToken
})
: null;
verifyBraces(openBlockOpeningToken, openBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, Boolean(openBlockClosingToken &&
openBlockLast &&
openBlockClosingToken === sourceCode.getTokenAfter(openBlockLast)));
},
SvelteAwaitCatchBlock(node) {
const openBlockOpeningToken = sourceCode.getFirstToken(node);
const openBlockLast = node.error || (node.awaitCatch ? node.parent.expression : null);
const openBlockClosingToken = openBlockLast
? sourceCode.getTokenAfter(openBlockLast, {
includeComments: false,
filter: isClosingBraceToken
})
: null;
verifyBraces(openBlockOpeningToken, openBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, Boolean(openBlockClosingToken &&
openBlockLast &&
openBlockClosingToken === sourceCode.getTokenAfter(openBlockLast)));
},
SvelteSnippetBlock(node) {
const openBlockOpeningToken = sourceCode.getFirstToken(node);
const openBlockClosingToken = sourceCode.getTokenAfter(node.params[node.params.length - 1] || node.id, {
includeComments: false,
filter: isClosingBraceToken
});
verifyBraces(openBlockOpeningToken, openBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, true);
const closeBlockClosingToken = sourceCode.getLastToken(node);
const closeBlockOpeningToken = sourceCode.getTokenBefore(closeBlockClosingToken, {
includeComments: false,
filter: isOpeningBraceToken
});
verifyBraces(closeBlockOpeningToken, closeBlockClosingToken, options.tags.openingBrace, options.tags.closingBrace, false);
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,58 @@
import { createRule } from '../utils/index.js';
export default createRule('no-add-event-listener', {
meta: {
docs: {
description: 'Warns against the use of `addEventListener`',
category: 'Best Practices',
recommended: false
},
hasSuggestions: true,
schema: [],
messages: {
unexpected: 'Do not use `addEventListener`. Use the `on` function from `svelte/events` instead.'
},
type: 'suggestion',
conditions: [
{
svelteVersions: ['5']
}
]
},
create(context) {
return {
CallExpression(node) {
const { callee } = node;
let target = null;
if (callee.type === 'MemberExpression' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'addEventListener') {
target = context.sourceCode.getText(callee.object);
}
else if (callee.type === 'Identifier' && callee.name === 'addEventListener') {
target = 'window';
}
if (target === null) {
return;
}
const openParen = context.sourceCode.getTokenAfter(callee);
const suggest = [];
if (openParen !== null) {
suggest.push({
desc: 'Use `on` from `svelte/events` instead',
fix(fixer) {
return [
fixer.replaceText(callee, 'on'),
fixer.insertTextAfter(openParen, `${target}, `)
];
}
});
}
context.report({
node,
messageId: 'unexpected',
suggest
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,67 @@
import { createRule } from '../utils/index.js';
import { getSvelteContext } from '../utils/svelte-context.js';
import { VERSION as SVELTE_VERSION } from 'svelte/compiler';
import semver from 'semver';
const shouldRun = semver.satisfies(SVELTE_VERSION, '>=5.56.0');
export default createRule('no-at-const-tags', {
meta: {
docs: {
description: 'disallow the use of `{@const}` in favor of `{const ...}` declaration tags',
category: 'Best Practices',
recommended: false
},
fixable: 'code',
schema: [],
messages: {
unexpected: 'Use `{const ...}` declaration tag instead of legacy `{@const ...}`.'
},
type: 'suggestion',
conditions: [
{
svelteVersions: ['5']
}
]
},
create(context) {
if (!shouldRun) {
return {};
}
const sourceCode = context.sourceCode;
const runes = getSvelteContext(context)?.runes;
// Only report and fix in runes mode, since preserving reactivity requires
// `$derived(...)`, which is unavailable outside runes mode.
if (runes !== true) {
return {};
}
return {
SvelteConstTag(node) {
context.report({
node,
messageId: 'unexpected',
*fix(fixer) {
const text = sourceCode.getText(node);
const match = /^\{(\s*)@const\b/u.exec(text);
if (!match) {
return;
}
const atOffset = node.range[0] + 1 + match[1].length;
yield fixer.removeRange([atOffset, atOffset + 1]);
const init = node.declarations[0].init;
if (init == null) {
return;
}
// Preserve the reactivity of legacy `{@const}` by wrapping the
// initializer in `$derived(...)`. Skip when already wrapped.
if (init.type === 'CallExpression' &&
init.callee.type === 'Identifier' &&
init.callee.name === '$derived') {
return;
}
yield fixer.insertTextBefore(init, '$derived(');
yield fixer.insertTextAfter(init, ')');
}
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,34 @@
import { createRule } from '../utils/index.js';
export default createRule('no-at-debug-tags', {
meta: {
docs: {
description: 'disallow the use of `{@debug}`',
category: 'Best Practices',
recommended: true,
default: 'warn'
},
hasSuggestions: true,
schema: [],
messages: {
unexpected: 'Unexpected `{@debug}`.',
suggestRemove: 'Remove `{@debug}` from the source'
},
type: 'problem'
},
create(context) {
return {
SvelteDebugTag(node) {
context.report({
node,
messageId: 'unexpected',
suggest: [
{
messageId: 'suggestRemove',
fix: (fixer) => fixer.remove(node)
}
]
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,25 @@
import { createRule } from '../utils/index.js';
export default createRule('no-at-html-tags', {
meta: {
docs: {
description: 'disallow use of `{@html}` to prevent XSS attack',
category: 'Security Vulnerability',
recommended: true
},
schema: [],
messages: {
unexpected: '`{@html}` can lead to XSS attack.'
},
type: 'suggestion' // "problem",
},
create(context) {
return {
'SvelteMustacheTag[kind=raw]'(node) {
context.report({
node,
messageId: 'unexpected'
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("src/types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,73 @@
import { getStaticValue } from '@eslint-community/eslint-utils';
import { getScope } from '../utils/ast-utils.js';
import { createRule } from '../utils/index.js';
export default createRule('no-bind-value-on-checkable-inputs', {
meta: {
hasSuggestions: true,
docs: {
description: 'disallow useless `bind:value` on `<input type="checkbox">` and `<input type="radio">`',
category: 'Possible Errors',
recommended: false
},
schema: [],
type: 'problem',
messages: {
checkboxValueBinding: '`bind:value` does not work on checkbox inputs. Did you mean `bind:checked` or `bind:group`?',
radioValueBinding: '`bind:value` does not work on radio inputs. Did you mean `bind:group`?', // svelte compiler disallows `bind:checked` on radios
checkedSuggestion: 'Change `bind:value` to `bind:checked`.',
groupSuggestion: 'Change `bind:value` to `bind:group`.'
}
},
create(context) {
return {
"SvelteElement[name.name='input']"(node) {
function getType() {
const typeAttr = node.startTag?.attributes.find((attr) => attr.type === 'SvelteAttribute' && attr.key.name.toLowerCase() === 'type');
if (!typeAttr)
return null;
if (typeAttr.value.length !== 1)
return null;
const typeValue = typeAttr.value[0];
if (typeValue.type === 'SvelteLiteral') {
// <input type="checkbox" />
return typeValue.value.toLowerCase();
}
if (typeValue.type === 'SvelteMustacheTag') {
const staticValue = getStaticValue(typeValue.expression, getScope(context, typeValue.expression));
if (typeof staticValue?.value !== 'string')
return null;
// <input type={"checkbox"} />
return staticValue.value.toLowerCase();
}
return null;
}
const type = getType();
if (!type || (type !== 'checkbox' && type !== 'radio'))
return;
const bindValue = node.startTag?.attributes.find((attr) => attr.type === 'SvelteDirective' &&
attr.kind === 'Binding' &&
attr.key.name.name === 'value');
if (!bindValue)
return;
function suggestion(suggestionType, bindValue // make typescript happy
) {
return {
messageId: `${suggestionType}Suggestion`,
fix(fixer) {
if (bindValue.shorthand)
return fixer.replaceText(bindValue, `bind:${suggestionType}={value}`);
return fixer.replaceText(bindValue.key, `bind:${suggestionType}`);
}
};
}
context.report({
node: bindValue,
messageId: `${type}ValueBinding`,
suggest: type === 'checkbox'
? [suggestion('checked', bindValue), suggestion('group', bindValue)]
: [suggestion('group', bindValue)]
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,104 @@
import fs from 'node:fs';
import path from 'node:path';
import { createRule } from '../utils/index.js';
// Extensions that plain TypeScript resolution appends to an unknown `.svelte`
// specifier. `.d.ts` is intentionally excluded: `Foo.svelte.d.ts` is a
// hand-written component declaration and does not shadow the component.
const MODULE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mts', '.cts', '.mjs', '.cjs'];
function isFile(filePath) {
try {
return fs.statSync(filePath).isFile();
}
catch {
return false;
}
}
// Resolve the real on-disk casing of a path's basename. On case-insensitive
// file systems (macOS, Windows) `fs.statSync` matches a differently-cased
// sibling, so the constructed name may not exist with that exact casing. Read
// the directory and use the actual entry name when one matches.
function realBasename(filePath) {
const wanted = path.basename(filePath);
try {
const entries = fs.readdirSync(path.dirname(filePath));
return (entries.find((entry) => entry === wanted) ??
entries.find((entry) => entry.toLowerCase() === wanted.toLowerCase()) ??
wanted);
}
catch {
return wanted;
}
}
export default createRule('no-conflicting-module-names', {
meta: {
docs: {
description: 'disallow a `.svelte` component and a same-named runes module (e.g. `Foo.svelte` and `Foo.svelte.ts`) from coexisting',
category: 'Possible Errors',
recommended: false
},
schema: [],
messages: {
conflictOnComponent: 'The module `{{moduleName}}` has the same name as this component. TypeScript resolves the import `{{specifier}}` to that module, not to this component. Rename `{{moduleName}}`.',
conflictOnModule: 'This module has the same name as the component `{{svelteName}}`. TypeScript resolves the import `{{specifier}}` to this module, not to the component. Rename this file.'
},
type: 'problem'
},
create(context) {
const filename = context.physicalFilename;
// Skip virtual/untitled files that do not exist on disk (editors, tests
// with fake paths). Only real files on disk are checked.
if (!isFile(filename)) {
return {};
}
// Both sides of the collision are linted by this plugin, and a lint run
// may contain only one of them, so each side reports on itself.
if (filename.endsWith('.svelte')) {
return {
Program(node) {
const svelteName = path.basename(filename);
for (const ext of MODULE_EXTENSIONS) {
const modulePath = `${filename}${ext}`;
if (isFile(modulePath)) {
context.report({
node,
loc: { line: 1, column: 0 },
messageId: 'conflictOnComponent',
data: {
moduleName: realBasename(modulePath),
specifier: `./${svelteName}`
}
});
return;
}
}
}
};
}
// A module named `<name>.svelte.<ext>`. Dropping the extension gives the
// component path it collides with. Declaration files such as
// `Foo.svelte.d.ts` do not match, because dropping `.ts` leaves
// `Foo.svelte.d`, which is not a component name.
const moduleExt = MODULE_EXTENSIONS.find((ext) => filename.endsWith(ext));
if (moduleExt == null) {
return {};
}
const sveltePath = filename.slice(0, -moduleExt.length);
if (!sveltePath.endsWith('.svelte') || !isFile(sveltePath)) {
return {};
}
return {
Program(node) {
const svelteName = realBasename(sveltePath);
context.report({
node,
loc: { line: 1, column: 0 },
messageId: 'conflictOnModule',
data: {
svelteName,
specifier: `./${svelteName}`
}
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,116 @@
import { createRule } from '../utils/index.js';
import { findVariable, getNodeName } from '../utils/ast-utils.js';
import { getPropertyName } from '@eslint-community/eslint-utils';
const DOM_MANIPULATING_METHODS = new Set([
'appendChild', // https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild
'insertBefore', // https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
'normalize', // https://developer.mozilla.org/en-US/docs/Web/API/Node/normalize
'removeChild', // https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild
'replaceChild', // https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild
'after', // https://developer.mozilla.org/en-US/docs/Web/API/Element/after
'append', // https://developer.mozilla.org/en-US/docs/Web/API/Element/append
'before', // https://developer.mozilla.org/en-US/docs/Web/API/Element/before
'insertAdjacentElement', // https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement
'insertAdjacentHTML', // https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
'insertAdjacentText', // https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentText
'prepend', // https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend
'remove', // https://developer.mozilla.org/en-US/docs/Web/API/Element/remove
'replaceChildren', // https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren
'replaceWith' // https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceWith
]);
const DOM_MANIPULATING_PROPERTIES = new Set([
'textContent', // https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
'innerHTML', // https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
'outerHTML', // https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML
'innerText', // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText
'outerText' // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText
]);
export default createRule('no-dom-manipulating', {
meta: {
docs: {
description: 'disallow DOM manipulating',
category: 'Possible Errors',
recommended: true
},
schema: [],
messages: {
disallowManipulateDOM: "Don't manipulate the DOM directly. The Svelte runtime can get confused if there is a difference between the actual DOM and the DOM expected by the Svelte runtime."
},
type: 'problem'
},
create(context) {
const domVariables = new Set();
/**
* Verify DOM variable identifier node
*/
function verifyIdentifier(node) {
const member = node.parent;
if (member?.type !== 'MemberExpression' || member.object !== node) {
return;
}
const name = getPropertyName(member);
if (!name) {
return;
}
let target = member;
let parent = target.parent;
while (parent?.type === 'ChainExpression') {
target = parent;
parent = parent.parent;
}
if (!parent) {
return;
}
if (parent.type === 'CallExpression') {
if (parent.callee !== target || !DOM_MANIPULATING_METHODS.has(name)) {
return;
}
}
else if (parent.type === 'AssignmentExpression') {
if (parent.left !== target || !DOM_MANIPULATING_PROPERTIES.has(name)) {
return;
}
}
else {
return;
}
context.report({
node: member,
messageId: 'disallowManipulateDOM'
});
}
return {
"SvelteDirective[kind='Binding']"(node) {
if (node.key.name.name !== 'this' ||
!node.expression ||
node.expression.type !== 'Identifier') {
// not bind:this={id}
return;
}
const element = node.parent.parent;
if (element.type !== 'SvelteElement' || !isHTMLElement(element)) {
// not HTML element
return;
}
const variable = findVariable(context, node.expression);
if (!variable || (variable.scope.type !== 'module' && variable.scope.type !== 'global')) {
return;
}
domVariables.add(variable);
},
'Program:exit'() {
for (const variable of domVariables) {
for (const reference of variable.references) {
verifyIdentifier(reference.identifier);
}
}
}
};
/**
* Checks whether the given node is a HTML element or not.
*/
function isHTMLElement(node) {
return (node.kind === 'html' || (node.kind === 'special' && getNodeName(node) === 'svelte:element'));
}
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,130 @@
import { createRule } from '../utils/index.js';
import { equalTokens } from '../utils/ast-utils.js';
/**
* Splits the given node by the given logical operator.
* @param operator Logical operator `||` or `&&`.
* @param node The node to split.
* @returns Array of conditions that makes the node when joined by the operator.
*/
function splitByLogicalOperator(operator, node) {
if (node.type === 'LogicalExpression' && node.operator === operator) {
return [
...splitByLogicalOperator(operator, node.left),
...splitByLogicalOperator(operator, node.right)
];
}
return [node];
}
/**
* Split with ||.
*/
function splitByOr(node) {
return splitByLogicalOperator('||', node);
}
/**
* Split with &&.
*/
function splitByAnd(node) {
return splitByLogicalOperator('&&', node);
}
/**
* Build OrOperands
*/
function buildOrOperands(node) {
const orOperands = splitByOr(node);
return {
node,
operands: orOperands.map((orOperand) => {
const andOperands = splitByAnd(orOperand);
return {
node: orOperand,
operands: andOperands
};
})
};
}
export default createRule('no-dupe-else-if-blocks', {
meta: {
docs: {
description: 'disallow duplicate conditions in `{#if}` / `{:else if}` chains',
category: 'Possible Errors',
recommended: true
},
schema: [],
messages: {
unexpected: 'This branch can never execute. Its condition is a duplicate or covered by previous conditions in the `{#if}` / `{:else if}` chain.'
},
type: 'problem' // "problem",
},
create(context) {
const sourceCode = context.sourceCode;
/**
* Determines whether the two given nodes are considered to be equal. In particular, given that the nodes
* represent expressions in a boolean context, `||` and `&&` can be considered as commutative operators.
* @param a First node.
* @param b Second node.
* @returns `true` if the nodes are considered to be equal.
*/
function equal(a, b) {
if (a.type !== b.type) {
return false;
}
if (a.type === 'LogicalExpression' &&
b.type === 'LogicalExpression' &&
(a.operator === '||' || a.operator === '&&') &&
a.operator === b.operator) {
return ((equal(a.left, b.left) && equal(a.right, b.right)) ||
(equal(a.left, b.right) && equal(a.right, b.left)));
}
return equalTokens(a, b, sourceCode);
}
/**
* Determines whether the first given AndOperands is a subset of the second given AndOperands.
*
* e.g. A: (a && b), B: (a && b && c): B is a subset of A.
*
* @param operandsA The AndOperands to compare from.
* @param operandsB The AndOperands to compare against.
* @returns `true` if the `andOperandsA` is a subset of the `andOperandsB`.
*/
function isSubset(operandsA, operandsB) {
return operandsA.operands.every((operandA) => operandsB.operands.some((operandB) => equal(operandA, operandB)));
}
/** Iterate SvelteIfBlock nodes */
function* iterateIfElseIf(node) {
let target = node;
while (target.parent.type === 'SvelteElseBlock' &&
target.parent.children.includes(target) &&
target.parent.parent.type === 'SvelteIfBlock') {
yield target.parent.parent;
target = target.parent.parent;
}
}
return {
SvelteIfBlock(node) {
const test = node.expression;
const conditionsToCheck = test.type === 'LogicalExpression' && test.operator === '&&'
? [...splitByAnd(test), test]
: [test];
const listToCheck = conditionsToCheck.map(buildOrOperands);
for (const currentIdBlock of iterateIfElseIf(node)) {
if (currentIdBlock.expression) {
const currentOrOperands = buildOrOperands(currentIdBlock.expression);
for (const condition of listToCheck) {
const operands = (condition.operands = condition.operands.filter((orOperand) => {
return !currentOrOperands.operands.some((currentOrOperand) => isSubset(currentOrOperand, orOperand));
}));
if (!operands.length) {
context.report({
node: condition.node,
messageId: 'unexpected'
});
return;
}
}
}
}
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,70 @@
import { createRule } from '../utils/index.js';
import { equalTokens } from '../utils/ast-utils.js';
export default createRule('no-dupe-on-directives', {
meta: {
docs: {
description: 'disallow duplicate `on:` directives',
category: 'Possible Errors',
recommended: true
},
schema: [],
messages: {
duplication: 'This `on:{{type}}` directive is the same and duplicate directives in L{{lineNo}}.'
},
type: 'problem'
},
create(context) {
const sourceCode = context.sourceCode;
const directiveDataMap = new Map();
return {
SvelteDirective(node) {
if (node.kind !== 'EventHandler')
return;
const directiveDataList = directiveDataMap.get(node.key.name.name);
if (!directiveDataList) {
directiveDataMap.set(node.key.name.name, [
{
expression: node.expression,
nodes: [node]
}
]);
return;
}
const directiveData = directiveDataList.find((data) => {
if (!data.expression || !node.expression) {
return data.expression === node.expression;
}
return equalTokens(data.expression, node.expression, sourceCode);
});
if (!directiveData) {
directiveDataList.push({
expression: node.expression,
nodes: [node]
});
return;
}
directiveData.nodes.push(node);
},
'SvelteStartTag:exit'() {
for (const [type, directiveDataList] of directiveDataMap) {
for (const { nodes } of directiveDataList) {
if (nodes.length < 2) {
continue;
}
for (const node of nodes) {
context.report({
node,
messageId: 'duplication',
data: {
type,
lineNo: String((nodes[0] !== node ? nodes[0] : nodes[1]).loc.start.line)
}
});
}
}
}
directiveDataMap.clear();
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,89 @@
import { createRule } from '../utils/index.js';
import { parseStyleAttributeValue } from '../utils/css-utils/index.js';
export default createRule('no-dupe-style-properties', {
meta: {
docs: {
description: 'disallow duplicate style properties',
category: 'Possible Errors',
recommended: true
},
schema: [],
messages: {
unexpected: "Duplicate property '{{name}}'."
},
type: 'problem'
},
create(context) {
return {
SvelteStartTag(node) {
const reported = new Set();
const beforeDeclarations = new Map();
for (const { decls } of iterateStyleDeclSetFromAttrs(node.attributes)) {
for (const decl of decls) {
const already = beforeDeclarations.get(decl.prop);
if (already) {
for (const report of [already, decl].filter((n) => !reported.has(n))) {
context.report({
node,
loc: report.loc,
messageId: 'unexpected',
data: { name: report.prop }
});
reported.add(report);
}
}
}
for (const decl of decls) {
beforeDeclarations.set(decl.prop, decl);
}
}
}
};
/** Iterate the style decl set from attrs */
function* iterateStyleDeclSetFromAttrs(attrs) {
for (const attr of attrs) {
if (attr.type === 'SvelteStyleDirective') {
yield {
decls: [{ prop: attr.key.name.name, loc: attr.key.name.loc }]
};
}
else if (attr.type === 'SvelteAttribute') {
if (attr.key.name !== 'style') {
continue;
}
const root = parseStyleAttributeValue(attr, context);
if (!root) {
continue;
}
yield* iterateStyleDeclSetFromStyleRoot(root);
}
}
}
/** Iterate the style decl set from style root */
function* iterateStyleDeclSetFromStyleRoot(root) {
for (const child of root.nodes) {
if (child.type === 'decl') {
yield {
decls: [
{
prop: child.prop.name,
get loc() {
return child.prop.loc;
}
}
]
};
}
else if (child.type === 'inline') {
const decls = [];
for (const root of child.getAllInlineStyles().values()) {
for (const set of iterateStyleDeclSetFromStyleRoot(root)) {
decls.push(...set.decls);
}
}
yield { decls };
}
}
}
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,71 @@
import { createRule } from '../utils/index.js';
import { equalTokens, getAttributeKeyText } from '../utils/ast-utils.js';
export default createRule('no-dupe-use-directives', {
meta: {
docs: {
description: 'disallow duplicate `use:` directives',
category: 'Possible Errors',
recommended: true
},
schema: [],
messages: {
duplication: 'This `{{keyText}}` directive is the same and duplicate directives in L{{lineNo}}.'
},
type: 'problem'
},
create(context) {
const sourceCode = context.sourceCode;
const directiveDataMap = new Map();
return {
SvelteDirective(node) {
if (node.kind !== 'Action')
return;
const keyText = getAttributeKeyText(node, context);
const directiveDataList = directiveDataMap.get(keyText);
if (!directiveDataList) {
directiveDataMap.set(keyText, [
{
expression: node.expression,
nodes: [node]
}
]);
return;
}
const directiveData = directiveDataList.find((data) => {
if (!data.expression || !node.expression) {
return data.expression === node.expression;
}
return equalTokens(data.expression, node.expression, sourceCode);
});
if (!directiveData) {
directiveDataList.push({
expression: node.expression,
nodes: [node]
});
return;
}
directiveData.nodes.push(node);
},
'SvelteStartTag:exit'() {
for (const [keyText, directiveDataList] of directiveDataMap) {
for (const { nodes } of directiveDataList) {
if (nodes.length < 2) {
continue;
}
for (const node of nodes) {
context.report({
node,
messageId: 'duplication',
data: {
keyText,
lineNo: String((nodes[0] !== node ? nodes[0] : nodes[1]).loc.start.line)
}
});
}
}
}
directiveDataMap.clear();
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,81 @@
import { createRule } from '../utils/index.js';
import { FindVariableContext, getAttributeValueQuoteAndRange, getStringIfConstant } from '../utils/ast-utils.js';
export default createRule('no-dynamic-slot-name', {
meta: {
docs: {
description: 'disallow dynamic slot name',
category: 'Possible Errors',
recommended: false
},
fixable: 'code',
deprecated: true,
schema: [],
messages: {
unexpected: '`<slot>` name cannot be dynamic.',
requireValue: '`<slot>` name requires a value.'
},
type: 'problem',
replacedBy: {
note: 'Now Svelte compiler itself throws an compile error.'
}
},
create(context) {
const sourceCode = context.sourceCode;
return {
"SvelteElement[name.name='slot'] > SvelteStartTag.startTag > SvelteAttribute[key.name='name']"(node) {
if (node.value.length === 0) {
context.report({
node,
messageId: 'requireValue'
});
return;
}
for (const vNode of node.value) {
if (vNode.type === 'SvelteMustacheTag') {
context.report({
node: vNode,
messageId: 'unexpected',
fix(fixer) {
const text = getStaticText(vNode.expression);
if (text == null) {
return null;
}
if (node.value.length === 1) {
const range = getAttributeValueQuoteAndRange(node, sourceCode).range;
return fixer.replaceTextRange(range, `"${text}"`);
}
const range = vNode.range;
return fixer.replaceTextRange(range, text);
}
});
}
}
}
};
/**
* Get static text from given expression
*/
function getStaticText(node) {
const expr = findRootExpression(new FindVariableContext(context), node);
return getStringIfConstant(expr);
}
/** Find data expression */
function findRootExpression(ctx, node) {
if (node.type !== 'Identifier') {
return node;
}
const variable = ctx.findVariable(node);
if (!variable || variable.defs.length !== 1) {
return node;
}
const def = variable.defs[0];
if (def.type === 'Variable') {
if (def.parent.kind === 'const' && def.node.init) {
const init = def.node.init;
return findRootExpression(ctx, init);
}
}
return node;
}
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,44 @@
import { createRule } from '../utils/index.js';
export default createRule('no-export-load-in-svelte-module-in-kit-pages', {
meta: {
docs: {
description: 'disallow exporting load functions in `*.svelte` module in SvelteKit page components.',
category: 'SvelteKit',
recommended: true
},
schema: [],
messages: {
unexpected: 'disallow exporting load functions in `*.svelte` module in SvelteKit page components.'
},
type: 'problem',
conditions: [
{
svelteKitFileTypes: ['+page.svelte', '+error.svelte', '+layout.svelte']
}
]
},
create(context) {
let isModule = false;
return {
// <script context="module">
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute[key.name="context"] > SvelteLiteral[value="module"]`]: () => {
isModule = true;
},
// </script>
'Program > SvelteScriptElement:exit': () => {
isModule = false;
},
// export function load() {}
// export const load = () => {}
[`:matches(ExportNamedDeclaration > FunctionDeclaration, ExportNamedDeclaration > VariableDeclaration > VariableDeclarator) > Identifier.id[name="load"]`]: (node) => {
if (!isModule)
return {};
return context.report({
node,
loc: node.loc,
messageId: 'unexpected'
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,57 @@
import { createRule } from '../utils/index.js';
export default createRule('no-extra-reactive-curlies', {
meta: {
docs: {
description: 'disallow wrapping single reactive statements in curly braces',
category: 'Stylistic Issues',
recommended: false,
conflictWithPrettier: false
},
hasSuggestions: true,
schema: [],
messages: {
extraCurlies: `Do not wrap reactive statements in curly braces unless necessary.`,
removeExtraCurlies: `Remove the unnecessary curly braces.`
},
type: 'suggestion',
conditions: [
{
svelteVersions: ['3/4']
},
{
svelteVersions: ['5'],
runes: [false, 'undetermined']
}
]
},
create(context) {
return {
// $: { foo = "bar"; }
[`SvelteReactiveStatement > BlockStatement[body.length=1]`]: (node) => {
const source = context.sourceCode;
return context.report({
node,
loc: node.loc,
messageId: 'extraCurlies',
suggest: [
{
messageId: 'removeExtraCurlies',
fix(fixer) {
const tokens = source.getTokens(node, { includeComments: true });
// Remove everything up to the second token, and the entire last token since
// those are known to be "{" and "}"
return [
fixer.removeRange([tokens[0].range[0], tokens[1].range[0]]),
fixer.removeRange([
tokens[tokens.length - 2].range[1],
tokens[tokens.length - 1].range[1]
])
];
}
}
]
});
}
};
}
});

View File

@@ -0,0 +1,2 @@
declare const _default: import("../types.js").RuleModule;
export default _default;

View File

@@ -0,0 +1,111 @@
import { createRule } from '../utils/index.js';
import { ReferenceTracker } from '@eslint-community/eslint-utils';
import { findVariable } from '../utils/ast-utils.js';
export default createRule('no-goto-without-base', {
meta: {
deprecated: true,
replacedBy: ['no-navigation-without-resolve'],
docs: {
description: 'disallow using goto() without the base path',
category: 'SvelteKit',
recommended: false
},
schema: [],
messages: {
isNotPrefixedWithBasePath: "Found a goto() call with a url that isn't prefixed with the base path."
},
type: 'suggestion',
conditions: [
{
svelteKitVersions: ['1.0.0-next', '1', '2']
}
]
},
create(context) {
return {
Program() {
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope);
const basePathNames = extractBasePathReferences(referenceTracker, context);
for (const gotoCall of extractGotoReferences(referenceTracker)) {
if (gotoCall.arguments.length < 1) {
continue;
}
const path = gotoCall.arguments[0];
switch (path.type) {
case 'BinaryExpression':
checkBinaryExpression(context, path, basePathNames);
break;
case 'Literal':
checkLiteral(context, path);
break;
case 'TemplateLiteral':
checkTemplateLiteral(context, path, basePathNames);
break;
default:
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
}
}
}
};
}
});
function checkBinaryExpression(context, path, basePathNames) {
if (path.left.type !== 'Identifier' || !basePathNames.has(path.left)) {
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
}
}
function checkTemplateLiteral(context, path, basePathNames) {
const startingIdentifier = extractStartingIdentifier(path);
if (startingIdentifier === undefined || !basePathNames.has(startingIdentifier)) {
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
}
}
function checkLiteral(context, path) {
if (!/^[+a-z]*:/i.test(path.value?.toString() ?? '')) {
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
}
}
function extractStartingIdentifier(templateLiteral) {
const literalParts = [...templateLiteral.expressions, ...templateLiteral.quasis].sort((a, b) => a.range[0] < b.range[0] ? -1 : 1);
for (const part of literalParts) {
if (part.type === 'TemplateElement' && part.value.raw === '') {
// Skip empty quasi in the begining
continue;
}
if (part.type === 'Identifier') {
return part;
}
return undefined;
}
return undefined;
}
function extractGotoReferences(referenceTracker) {
return Array.from(referenceTracker.iterateEsmReferences({
'$app/navigation': {
[ReferenceTracker.ESM]: true,
goto: {
[ReferenceTracker.CALL]: true
}
}
}), ({ node }) => node);
}
function extractBasePathReferences(referenceTracker, context) {
const set = new Set();
for (const { node } of referenceTracker.iterateEsmReferences({
'$app/paths': {
[ReferenceTracker.ESM]: true,
base: {
[ReferenceTracker.READ]: true
}
}
})) {
const variable = findVariable(context, node.local);
if (!variable)
continue;
for (const reference of variable.references) {
if (reference.identifier.type === 'Identifier')
set.add(reference.identifier);
}
}
return set;
}

Some files were not shown because too many files have changed in this diff Show More