-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
158 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
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,50 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/signal-prefer-let' | ||
description: 'use let instead of const for signals values' | ||
--- | ||
|
||
# svelte/signal-prefer-let | ||
|
||
> use let instead of const for signals values | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
- :bulb: Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports whenever a signal is assigned to a const. | ||
In JavaScript `const` are defined as immutable references which cannot be reassigned. | ||
Signals are by definition changing and are reassigned by Svelte's reactivity system. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/signal-prefer-let: "error" */ | ||
/* ✓ GOOD */ | ||
let { value } = $props(); | ||
let doubled = $derived(value * 2); | ||
/* ✗ BAD */ | ||
const { value } = $props(); | ||
const doubled = $derived(value * 2); | ||
</script> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/signal-prefer-let.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/signal-prefer-let.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
57 changes: 57 additions & 0 deletions
57
packages/eslint-plugin-svelte/src/rules/signal-prefer-let.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,57 @@ | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
import { createRule } from '../utils'; | ||
|
||
export default createRule('signal-prefer-let', { | ||
meta: { | ||
docs: { | ||
description: 'use let instead of const for signals values', | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
schema: [], | ||
messages: { | ||
useLet: "const is used for a signal value. Use 'let' instead.", | ||
replaceConst: "Replace 'const' with 'let'" | ||
}, | ||
type: 'suggestion', | ||
hasSuggestions: true | ||
}, | ||
create(context) { | ||
function preferLet(node: TSESTree.VariableDeclaration) { | ||
if (node.kind !== 'const') { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
messageId: 'useLet', | ||
suggest: [ | ||
{ | ||
messageId: 'replaceConst', | ||
fix: (fixer) => fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let') | ||
} | ||
] | ||
}); | ||
} | ||
|
||
return { | ||
'VariableDeclaration > VariableDeclarator > CallExpression > Identifier'( | ||
node: TSESTree.Identifier | ||
) { | ||
if (['$props', '$derived', '$state'].includes(node.name)) { | ||
preferLet(node.parent.parent?.parent as TSESTree.VariableDeclaration); | ||
} | ||
}, | ||
'VariableDeclaration > VariableDeclarator > CallExpression > MemberExpression > Identifier'( | ||
node: TSESTree.Identifier | ||
) { | ||
if ( | ||
node.name === 'by' && | ||
((node.parent as TSESTree.MemberExpression).object as TSESTree.Identifier).name === | ||
'$derived' | ||
) { | ||
preferLet(node.parent.parent?.parent?.parent as TSESTree.VariableDeclaration); | ||
} | ||
} | ||
}; | ||
} | ||
}); |
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
22 changes: 22 additions & 0 deletions
22
...es/eslint-plugin-svelte/tests/fixtures/rules/signal-prefer-let/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,22 @@ | ||
- message: "const is used for a signal value. Use 'let' instead." | ||
line: 2 | ||
column: 2 | ||
suggestions: | ||
- desc: "Replace 'const' with 'let'" | ||
output: | | ||
<script> | ||
let { value, fn } = $props(); | ||
const x = $derived.by(fn); | ||
</script> | ||
- message: "const is used for a signal value. Use 'let' instead." | ||
line: 4 | ||
column: 2 | ||
suggestions: | ||
- desc: "Replace 'const' with 'let'" | ||
output: | | ||
<script> | ||
const { value, fn } = $props(); | ||
let x = $derived.by(fn); | ||
</script> |
5 changes: 5 additions & 0 deletions
5
...s/eslint-plugin-svelte/tests/fixtures/rules/signal-prefer-let/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,5 @@ | ||
<script> | ||
const { value, fn } = $props(); | ||
const x = $derived.by(fn); | ||
</script> |
3 changes: 3 additions & 0 deletions
3
...ges/eslint-plugin-svelte/tests/fixtures/rules/signal-prefer-let/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> | ||
let { value } = $props(); | ||
</script> |
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/src/rules/signal-prefer-let.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/signal-prefer-let'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('signal-prefer-let', rule as any, loadTestCases('signal-prefer-let')); |