-
-
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
12 changed files
with
154 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 | ||
--- | ||
|
||
New svelte/signal-prefer-let 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,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> | ||
- :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 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 fix> | ||
|
||
<!--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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
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." | ||
}, | ||
type: 'suggestion', | ||
fixable: 'code' | ||
}, | ||
create(context) { | ||
function preferLet(node: TSESTree.VariableDeclaration) { | ||
if (node.kind !== 'const') { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
messageId: 'useLet', | ||
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
12 changes: 12 additions & 0 deletions
12
...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,12 @@ | ||
- message: "const is used for a signal value. Use 'let' instead." | ||
line: 2 | ||
column: 2 | ||
- message: "const is used for a signal value. Use 'let' instead." | ||
line: 3 | ||
column: 2 | ||
- message: "const is used for a signal value. Use 'let' instead." | ||
line: 4 | ||
column: 2 | ||
- message: "const is used for a signal value. Use 'let' instead." | ||
line: 5 | ||
column: 2 |
6 changes: 6 additions & 0 deletions
6
...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,6 @@ | ||
<script> | ||
const { prop } = $props(); | ||
const state = $state(); | ||
const derived = $derived(state + 1); | ||
const derivedBy = $derived.by(() => prop + state); | ||
</script> |
6 changes: 6 additions & 0 deletions
6
.../eslint-plugin-svelte/tests/fixtures/rules/signal-prefer-let/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,6 @@ | ||
<script> | ||
let { prop } = $props(); | ||
let state = $state(); | ||
let derived = $derived(state + 1); | ||
let derivedBy = $derived.by(() => prop + state); | ||
</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')); |