Skip to content

Commit 15e0038

Browse files
committed
fix: ignore $state in the prefer-const rule
1 parent 20a2f32 commit 15e0038

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

.changeset/sixty-cars-fail.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
fix: ignore `$state` in the `prefer-const` rule

docs/rules/prefer-const.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ since: 'v3.0.0-next.6'
1414

1515
## :book: Rule Details
1616

17-
This rule reports the same as the base ESLint `prefer-const` rule, except that ignores Svelte reactive values such as `$derived` and `$props`. If this rule is active, make sure to disable the base `prefer-const` rule, as it will conflict with this rule.
17+
This rule reports the same as the base ESLint `prefer-const` rule, except that ignores Svelte reactive values such as `$derived`, `$state`, and `$props`. If this rule is active, make sure to disable the base `prefer-const` rule, as it will conflict with this rule.
1818

1919
<!--eslint-skip-->
2020

packages/eslint-plugin-svelte/src/rules/prefer-const.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function shouldSkipDeclaration(declaration: TSESTree.Expression | null) {
3131
return false;
3232
}
3333

34-
if (callee.type === 'Identifier' && ['$props', '$derived'].includes(callee.name)) {
34+
if (callee.type === 'Identifier' && ['$props', '$derived', '$state'].includes(callee.name)) {
3535
return true;
3636
}
3737

packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test01-errors.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
line: 3
33
column: 6
44
suggestions: null
5-
- message: "'state' is never reassigned. Use 'const' instead."
6-
line: 4
7-
column: 6
8-
suggestions: null
95
- message: "'raw' is never reassigned. Use 'const' instead."
106
line: 5
117
column: 6

packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test01-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
let { prop1, prop2 } = $props();
33
const zero = 0;
4-
const state = $state(0);
4+
let state = $state(0);
55
const raw = $state.raw(0);
66
const doubled = state * 2;
77
let derived = $derived(state * 2);

0 commit comments

Comments
 (0)