Skip to content

Commit e1d8925

Browse files
committed
feat(prefer-const): add rule
1 parent e4a2611 commit e1d8925

File tree

14 files changed

+668
-9
lines changed

14 files changed

+668
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ These rules relate to better ways of doing things to help you avoid problems:
427427
| [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 | |
428428
| [svelte/no-unused-svelte-ignore](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/) | disallow unused svelte-ignore comments | :star: |
429429
| [svelte/no-useless-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/) | disallow unnecessary mustache interpolations | :wrench: |
430+
| [svelte/prefer-const](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/) | Require `const` declarations for variables that are never reassigned after declared (excludes reactive values). | :wrench: |
430431
| [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: |
431432
| [svelte/require-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-each-key/) | require keyed `{#each}` block | |
432433
| [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

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

docs/rules/prefer-const.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 (excludes reactive values).'
6+
---
7+
8+
# svelte/prefer-const
9+
10+
> Require `const` declarations for variables that are never reassigned after declared (excludes reactive values).
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 ???.
18+
19+
<ESLintCodeBlock fix>
20+
21+
<!--eslint-skip-->
22+
23+
```svelte
24+
<script>
25+
/* eslint svelte/prefer-const: "error" */
26+
</script>
27+
28+
<!-- ✓ GOOD -->
29+
30+
<!-- ✗ BAD -->
31+
```
32+
33+
</ESLintCodeBlock>
34+
35+
## :wrench: Options
36+
37+
```json
38+
{
39+
"svelte/prefer-const": ["error", {}]
40+
}
41+
```
42+
43+
-
44+
45+
## :books: Further Reading
46+
47+
-
48+
49+
## :mag: Implementation
50+
51+
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/prefer-const.ts)
52+
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/prefer-const.ts)

packages/eslint-plugin-svelte/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"cover": "nyc --reporter=lcov pnpm run test",
3131
"debug": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
3232
"lint": "run-p lint:*",
33-
"lint-fix": "pnpm run lint:es --fix && pnpm run lint:style --fix",
33+
"lint:fix": "pnpm run lint:es --fix",
3434
"lint:es": "eslint --cache .",
3535
"mocha": "pnpm run ts ./node_modules/mocha/bin/mocha.js",
3636
"new": "pnpm run ts ./tools/new-rule.ts",

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ export interface RuleOptions {
264264
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-class-directive/
265265
*/
266266
'svelte/prefer-class-directive'?: Linter.RuleEntry<SveltePreferClassDirective>
267+
/**
268+
* Require `const` declarations for variables that are never reassigned after declared (excludes reactive values).
269+
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/
270+
*/
271+
'svelte/prefer-const'?: Linter.RuleEntry<SveltePreferConst>
267272
/**
268273
* destructure values from object stores for better change tracking & fewer redraws
269274
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/
@@ -485,6 +490,11 @@ type SvelteNoUselessMustaches = []|[{
485490
type SveltePreferClassDirective = []|[{
486491
prefer?: ("always" | "empty")
487492
}]
493+
// ----- svelte/prefer-const -----
494+
type SveltePreferConst = []|[{
495+
destructuring?: ("any" | "all")
496+
ignoreReadBeforeAssign?: boolean
497+
}]
488498
// ----- svelte/shorthand-attribute -----
489499
type SvelteShorthandAttribute = []|[{
490500
prefer?: ("always" | "never")

0 commit comments

Comments
 (0)