-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: baseballyama <xydybaseball@gmail.com>
- Loading branch information
1 parent
117e60d
commit 71eca84
Showing
13 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': minor | ||
--- | ||
|
||
Add `prefer-const` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/prefer-const' | ||
description: 'Require `const` declarations for variables that are never reassigned after declared' | ||
--- | ||
|
||
# svelte/prefer-const | ||
|
||
> Require `const` declarations for variables that are never reassigned after declared | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
- :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. | ||
|
||
## :book: Rule Details | ||
|
||
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. | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/prefer-const: "error" */ | ||
// ✓ GOOD | ||
const { a, b } = $props(); | ||
let c = $state(''); | ||
let d = $derived(a * 2); | ||
let e = $derived.by(() => b * 2); | ||
// ✗ BAD | ||
let obj = { a, b }; | ||
let g = $state(0); | ||
let h = $state({ count: 1 }); | ||
</script> | ||
<input bind:value={c} /> | ||
<input bind:value={h.count} /> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"svelte/prefer-const": [ | ||
"error", | ||
{ | ||
"destructuring": "any", | ||
"ignoreReadonly": true | ||
} | ||
] | ||
} | ||
``` | ||
|
||
- `destructuring`: The kind of the way to address variables in destructuring. There are 2 values: | ||
- `any` (default): if any variables in destructuring should be const, this rule warns for those variables. | ||
- `all`: if all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them. | ||
- `ignoreReadonly`: If `true`, this rule will ignore variables that are read between the declaration and the _first_ assignment. | ||
|
||
## :books: Further Reading | ||
|
||
- See [ESLint `prefer-const` rule](https://eslint.org/docs/latest/rules/prefer-const) for more information about the base rule. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/prefer-const.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/prefer-const.ts) | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/prefer-const)</sup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
|
||
import { createRule } from '../utils/index.js'; | ||
import { defineWrapperListener, getCoreRule } from '../utils/eslint-core.js'; | ||
|
||
const coreRule = getCoreRule('prefer-const'); | ||
|
||
/** | ||
* Finds and returns the callee of a declaration node within variable declarations or object patterns. | ||
*/ | ||
function findDeclarationCallee(node: TSESTree.Expression) { | ||
const { parent } = node; | ||
if (parent.type === 'VariableDeclarator' && parent.init?.type === 'CallExpression') { | ||
return parent.init.callee; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Determines if a declaration should be skipped in the const preference analysis. | ||
* Specifically checks for Svelte's state management utilities ($props, $derived). | ||
*/ | ||
function shouldSkipDeclaration(declaration: TSESTree.Expression | null) { | ||
if (!declaration) { | ||
return false; | ||
} | ||
|
||
const callee = findDeclarationCallee(declaration); | ||
if (!callee) { | ||
return false; | ||
} | ||
|
||
if (callee.type === 'Identifier' && ['$props', '$derived'].includes(callee.name)) { | ||
return true; | ||
} | ||
|
||
if (callee.type !== 'MemberExpression' || callee.object.type !== 'Identifier') { | ||
return false; | ||
} | ||
|
||
if ( | ||
callee.object.name === '$derived' && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === 'by' | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default createRule('prefer-const', { | ||
meta: { | ||
...coreRule.meta, | ||
docs: { | ||
description: coreRule.meta.docs.description, | ||
category: 'Best Practices', | ||
recommended: false, | ||
extensionRule: 'prefer-const' | ||
} | ||
}, | ||
create(context) { | ||
return defineWrapperListener(coreRule, context, { | ||
createListenerProxy(coreListener) { | ||
return { | ||
...coreListener, | ||
VariableDeclaration(node) { | ||
for (const decl of node.declarations) { | ||
if (shouldSkipDeclaration(decl.init)) { | ||
return; | ||
} | ||
} | ||
|
||
coreListener.VariableDeclaration?.(node); | ||
} | ||
}; | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test01-errors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
- message: "'zero' is never reassigned. Use 'const' instead." | ||
line: 3 | ||
column: 6 | ||
suggestions: null | ||
- message: "'state' is never reassigned. Use 'const' instead." | ||
line: 4 | ||
column: 6 | ||
suggestions: null | ||
- message: "'raw' is never reassigned. Use 'const' instead." | ||
line: 5 | ||
column: 6 | ||
suggestions: null | ||
- message: "'doubled' is never reassigned. Use 'const' instead." | ||
line: 6 | ||
column: 6 | ||
suggestions: null | ||
- message: "'calculated' is never reassigned. Use 'const' instead." | ||
line: 8 | ||
column: 6 | ||
suggestions: null |
11 changes: 11 additions & 0 deletions
11
packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
let { prop1, prop2 } = $props(); | ||
let zero = 0; | ||
let state = $state(0); | ||
let raw = $state.raw(0); | ||
let doubled = state * 2; | ||
let derived = $derived(state * 2); | ||
let calculated = calc(); | ||
let derivedBy = $derived.by(calc()); | ||
let noInit; | ||
</script> |
11 changes: 11 additions & 0 deletions
11
packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test01-output.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
let { prop1, prop2 } = $props(); | ||
const zero = 0; | ||
const state = $state(0); | ||
const raw = $state.raw(0); | ||
const doubled = state * 2; | ||
let derived = $derived(state * 2); | ||
const calculated = calc(); | ||
let derivedBy = $derived.by(calc()); | ||
let noInit; | ||
</script> |
3 changes: 3 additions & 0 deletions
3
packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/test01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script> | ||
const a = {}; | ||
</script> |
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/src/rules/prefer-const.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { RuleTester } from '../../utils/eslint-compat'; | ||
import rule from '../../../src/rules/prefer-const'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
}, | ||
}); | ||
|
||
tester.run('prefer-const', rule as any, loadTestCases('prefer-const')); |