Skip to content

Commit 4f1a00a

Browse files
committed
feat: Add svelte/prefer-const rule that excludes reactive variables
1 parent 7ec6c0f commit 4f1a00a

File tree

12 files changed

+478
-0
lines changed

12 files changed

+478
-0
lines changed

.changeset/tricky-windows-brush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': minor
3+
---
4+
5+
Add svelte/prefer-const rule that excludes reactive variables

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ These rules relate to better ways of doing things to help you avoid problems:
425425
| [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 | |
426426
| [svelte/no-unused-svelte-ignore](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/) | disallow unused svelte-ignore comments | :star: |
427427
| [svelte/no-useless-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/) | disallow unnecessary mustache interpolations | :wrench: |
428+
| [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: |
428429
| [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: |
429430
| [svelte/require-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-each-key/) | require keyed `{#each}` block | |
430431
| [svelte/require-event-dispatcher-types](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-event-dispatcher-types/) | require type parameters for `createEventDispatcher` | |

docs/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ These rules relate to better ways of doing things to help you avoid problems:
6262
| [svelte/no-unused-class-name](./rules/no-unused-class-name.md) | disallow the use of a class in the template without a corresponding style | |
6363
| [svelte/no-unused-svelte-ignore](./rules/no-unused-svelte-ignore.md) | disallow unused svelte-ignore comments | :star: |
6464
| [svelte/no-useless-mustaches](./rules/no-useless-mustaches.md) | disallow unnecessary mustache interpolations | :wrench: |
65+
| [svelte/prefer-const](./rules/prefer-const.md) | Require `const` declarations for variables that are never reassigned after declared | :wrench: |
6566
| [svelte/prefer-destructured-store-props](./rules/prefer-destructured-store-props.md) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
6667
| [svelte/require-each-key](./rules/require-each-key.md) | require keyed `{#each}` block | |
6768
| [svelte/require-event-dispatcher-types](./rules/require-event-dispatcher-types.md) | require type parameters for `createEventDispatcher` | |

docs/rules/prefer-const.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
pageClass: 'rule-details'
3+
sidebarDepth: 0
4+
title: 'svelte/prefer-const'
5+
description: 'Require `const` declarations for variables that are never reassigned after declared'
6+
---
7+
8+
# svelte/prefer-const
9+
10+
> Require `const` declarations for variables that are never reassigned after declared
11+
12+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
13+
- :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.
14+
15+
## :book: Rule Details
16+
17+
Based on https://eslint.org/docs/latest/rules/prefer-const but skips reactive variables created by runes.
18+
19+
## :mag: Implementation
20+
21+
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/prefer-const.ts)
22+
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/prefer-const.ts)

packages/eslint-plugin-svelte/src/rule-types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ export interface RuleOptions {
249249
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-class-directive/
250250
*/
251251
'svelte/prefer-class-directive'?: Linter.RuleEntry<SveltePreferClassDirective>
252+
/**
253+
* Require `const` declarations for variables that are never reassigned after declared
254+
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/
255+
*/
256+
'svelte/prefer-const'?: Linter.RuleEntry<SveltePreferConst>
252257
/**
253258
* destructure values from object stores for better change tracking & fewer redraws
254259
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/
@@ -460,6 +465,11 @@ type SvelteNoUselessMustaches = []|[{
460465
type SveltePreferClassDirective = []|[{
461466
prefer?: ("always" | "empty")
462467
}]
468+
// ----- svelte/prefer-const -----
469+
type SveltePreferConst = []|[{
470+
destructuring?: ("any" | "all")
471+
ignoreReadBeforeAssign?: boolean
472+
}]
463473
// ----- svelte/shorthand-attribute -----
464474
type SvelteShorthandAttribute = []|[{
465475
prefer?: ("always" | "never")

0 commit comments

Comments
 (0)