Skip to content

Commit c3d5bd7

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

File tree

5 files changed

+10
-17
lines changed

5 files changed

+10
-17
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

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,15 @@ 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', '$state', '$derived'].includes(callee.name)) {
3535
return true;
3636
}
3737

3838
if (callee.type !== 'MemberExpression' || callee.object.type !== 'Identifier') {
3939
return false;
4040
}
4141

42-
if (
43-
callee.object.name === '$derived' &&
44-
callee.property.type === 'Identifier' &&
45-
callee.property.name === 'by'
46-
) {
42+
if (callee.object.name === '$state' || callee.object.name === '$derived') {
4743
return true;
4844
}
4945

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

-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +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
9-
- message: "'raw' is never reassigned. Use 'const' instead."
10-
line: 5
11-
column: 6
12-
suggestions: null
135
- message: "'doubled' is never reassigned. Use 'const' instead."
146
line: 6
157
column: 6

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

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

0 commit comments

Comments
 (0)