|
| 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 | +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. |
| 18 | + |
| 19 | +<!--eslint-skip--> |
| 20 | + |
| 21 | +```svelte |
| 22 | +<script> |
| 23 | + /* eslint svelte/prefer-const: "error" */ |
| 24 | +
|
| 25 | + // ✓ GOOD |
| 26 | + const { a, b } = $props(); |
| 27 | + let c = $state(''); |
| 28 | + let d = $derived(a * 2); |
| 29 | + let e = $derived.by(() => b * 2); |
| 30 | +
|
| 31 | + // ✗ BAD |
| 32 | + let obj = { a, b }; |
| 33 | + let g = $state(0); |
| 34 | + let h = $state({ count: 1 }); |
| 35 | +</script> |
| 36 | +
|
| 37 | +<input bind:value={c} /> |
| 38 | +<input bind:value={h.count} /> |
| 39 | +``` |
| 40 | + |
| 41 | +## :wrench: Options |
| 42 | + |
| 43 | +```json |
| 44 | +{ |
| 45 | + "svelte/prefer-const": [ |
| 46 | + "error", |
| 47 | + { |
| 48 | + "destructuring": "any", |
| 49 | + "ignoreReadonly": true |
| 50 | + } |
| 51 | + ] |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +- `destructuring`: The kind of the way to address variables in destructuring. There are 2 values: |
| 56 | + - `any` (default): if any variables in destructuring should be const, this rule warns for those variables. |
| 57 | + - `all`: if all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them. |
| 58 | +- `ignoreReadonly`: If `true`, this rule will ignore variables that are read between the declaration and the _first_ assignment. |
| 59 | + |
| 60 | +## :books: Further Reading |
| 61 | + |
| 62 | +- See [ESLint `prefer-const` rule](https://eslint.org/docs/latest/rules/prefer-const) for more information about the base rule. |
| 63 | + |
| 64 | +## :mag: Implementation |
| 65 | + |
| 66 | +- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/prefer-const.ts) |
| 67 | +- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/prefer-const.ts) |
| 68 | + |
| 69 | +<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/prefer-const)</sup> |
0 commit comments