Skip to content

Commit 50cf503

Browse files
committed
docs(prefer-const): improve rule docs
1 parent e50935e commit 50cf503

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

docs/rules/prefer-const.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description: 'Require `const` declarations for variables that are never reassign
1414

1515
## :book: Rule Details
1616

17-
This rule reports ???.
17+
This rule reports the same as the base ESLint `prefer-const` rule, except that ignores Svelte reactive values such as `$state`, `$derived` 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
<ESLintCodeBlock fix>
2020

@@ -23,11 +23,16 @@ This rule reports ???.
2323
```svelte
2424
<script>
2525
/* eslint svelte/prefer-const: "error" */
26-
</script>
2726
28-
<!-- ✓ GOOD -->
27+
<!-- ✓ GOOD -->
28+
const { a, b } = $props();
29+
let { a, b } = $state();
30+
2931
30-
<!-- ✗ BAD -->
32+
<!-- ✗ BAD -->
33+
// Imagine obj is not re-assigned, therefore it should be constant
34+
let obj = { a, b };
35+
</script>
3136
```
3237

3338
</ESLintCodeBlock>
@@ -36,15 +41,24 @@ This rule reports ???.
3641

3742
```json
3843
{
39-
"svelte/prefer-const": ["error", {}]
44+
"svelte/prefer-const": [
45+
"error",
46+
{
47+
"destructuring": "any",
48+
"ignoreReadonly": true
49+
}
50+
]
4051
}
4152
```
4253

43-
-
54+
- `destructuring`: The kind of the way to address variables in destructuring. There are 2 values:
55+
- `any` (default): if any variables in destructuring should be const, this rule warns for those variables.
56+
- `all`: if all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them.
57+
- `ignoreReadonly`: If `true`, this rule will ignore variables that are read between the declaration and the _first_ assignment.
4458

4559
## :books: Further Reading
4660

47-
-
61+
- See [ESLint `prefer-const` rule](https://eslint.org/docs/latest/rules/prefer-const) for more information about the base rule.
4862

4963
## :mag: Implementation
5064

0 commit comments

Comments
 (0)