Skip to content

feat: Add svelte/prefer-const rule that excludes reactive variables #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-windows-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

Add svelte/prefer-const rule that excludes reactive variables
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [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-svelte-ignore](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/) | disallow unused svelte-ignore comments | :star: |
| [svelte/no-useless-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/) | disallow unnecessary mustache interpolations | :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 (excludes reactive values). | :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/require-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-each-key/) | require keyed `{#each}` block | |
| [svelte/require-event-dispatcher-types](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-event-dispatcher-types/) | require type parameters for `createEventDispatcher` | |
Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [svelte/no-unused-class-name](./rules/no-unused-class-name.md) | disallow the use of a class in the template without a corresponding style | |
| [svelte/no-unused-svelte-ignore](./rules/no-unused-svelte-ignore.md) | disallow unused svelte-ignore comments | :star: |
| [svelte/no-useless-mustaches](./rules/no-useless-mustaches.md) | disallow unnecessary mustache interpolations | :wrench: |
| [svelte/prefer-const](./rules/prefer-const.md) | Require `const` declarations for variables that are never reassigned after declared (excludes reactive values). | :wrench: |
| [svelte/prefer-destructured-store-props](./rules/prefer-destructured-store-props.md) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
| [svelte/require-each-key](./rules/require-each-key.md) | require keyed `{#each}` block | |
| [svelte/require-event-dispatcher-types](./rules/require-event-dispatcher-types.md) | require type parameters for `createEventDispatcher` | |
Expand Down
22 changes: 22 additions & 0 deletions docs/rules/prefer-const.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
pageClass: 'rule-details'
sidebarDepth: 0
title: 'svelte/prefer-const'
description: 'Require `const` declarations for variables that are never reassigned after declared (excludes reactive values).'
---

# svelte/prefer-const

> Require `const` declarations for variables that are never reassigned after declared (excludes reactive values).

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

## :book: Rule Details

Based on https://eslint.org/docs/latest/rules/prefer-const but skips reactive variables created by runes.

## :mag: Implementation

- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/prefer-const.ts)
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/prefer-const.ts)
16 changes: 8 additions & 8 deletions packages/eslint-plugin-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"eslint-compat-utils": "^0.5.1",
"esutils": "^2.0.3",
"known-css-properties": "^0.34.0",
"postcss": "^8.4.38",
"postcss": "^8.4.39",
"postcss-load-config": "^3.1.4",
"postcss-safe-parser": "^6.0.0",
"postcss-selector-parser": "^6.1.0",
Expand All @@ -78,27 +78,27 @@
"@types/esutils": "^2.0.2",
"@types/json-schema": "^7.0.15",
"@types/less": "^3.0.6",
"@types/mocha": "^10.0.6",
"@types/node": "^20.14.2",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.10",
"@types/postcss-safe-parser": "^5.0.4",
"@types/semver": "^7.5.8",
"@types/stylus": "^0.48.42",
"acorn": "^8.12.0",
"acorn": "^8.12.1",
"assert": "^2.1.0",
"esbuild": "^0.23.0",
"esbuild-register": "^3.5.0",
"eslint-scope": "^8.0.1",
"eslint-typegen": "^0.2.4",
"eslint-visitor-keys": "^4.0.0",
"espree": "^10.0.1",
"espree": "^10.1.0",
"less": "^4.2.0",
"mocha": "^10.4.0",
"mocha": "^10.6.0",
"nyc": "^17.0.0",
"postcss-nested": "^6.0.1",
"sass": "^1.77.5",
"sass": "^1.77.6",
"source-map-js": "^1.2.0",
"stylus": "^0.63.0",
"svelte": "^5.0.0-next.155",
"svelte": "5.0.0-next.175",
"svelte-i18n": "^4.0.0",
"type-coverage": "^2.29.0",
"yaml": "^2.4.5"
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin-svelte/src/rule-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ export interface RuleOptions {
* @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 (excludes reactive values).
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/
*/
'svelte/prefer-const'?: Linter.RuleEntry<SveltePreferConst>
/**
* destructure values from object stores for better change tracking & fewer redraws
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/
Expand Down Expand Up @@ -460,6 +465,11 @@ type SvelteNoUselessMustaches = []|[{
type SveltePreferClassDirective = []|[{
prefer?: ("always" | "empty")
}]
// ----- svelte/prefer-const -----
type SveltePreferConst = []|[{
destructuring?: ("any" | "all")
ignoreReadBeforeAssign?: boolean
}]
// ----- svelte/shorthand-attribute -----
type SvelteShorthandAttribute = []|[{
prefer?: ("always" | "never")
Expand Down
Loading