-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: add svelte/no-immutable-reactive-statements
rule
#439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||
--- | ||
|
||
feat: add `svelte/no-immutable-reactive-statements` rule |
This file contains hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/no-immutable-reactive-statements" | ||
description: "disallow reactive statements that don't reference reactive values." | ||
--- | ||
|
||
# svelte/no-immutable-reactive-statements | ||
|
||
> disallow reactive statements that don't reference reactive values. | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports if all variables referenced in reactive statements are immutable. That reactive statement is immutable and not reactive. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-immutable-reactive-statements: "error" */ | ||
import myStore from "./my-stores" | ||
import myVar from "./my-variables" | ||
let mutableVar = "hello" | ||
export let prop | ||
/* ✓ GOOD */ | ||
$: computed1 = mutableVar + " " + mutableVar | ||
$: computed2 = fn1(mutableVar) | ||
$: console.log(mutableVar) | ||
$: console.log(computed1) | ||
$: console.log($myStore) | ||
$: console.log(prop) | ||
|
||
const immutableVar = "hello" | ||
/* ✗ BAD */ | ||
$: computed3 = fn1(immutableVar) | ||
$: computed4 = fn2() | ||
$: console.log(immutableVar) | ||
$: console.log(myVar) | ||
|
||
/* ignore */ | ||
$: console.log(unknown) | ||
|
||
function fn1(v) { | ||
return v + " " + v | ||
} | ||
function fn2() { | ||
return mutableVar + " " + mutableVar | ||
} | ||
</script> | ||
|
||
<input bind:value={mutableVar} /> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-immutable-reactive-statements.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-immutable-reactive-statements.ts) |
This file contains hidden or 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,162 @@ | ||
import type { AST } from "svelte-eslint-parser" | ||
import { createRule } from "../utils" | ||
import type { | ||
Scope, | ||
Variable, | ||
Reference, | ||
Definition, | ||
} from "@typescript-eslint/scope-manager" | ||
|
||
export default createRule("no-immutable-reactive-statements", { | ||
meta: { | ||
docs: { | ||
description: | ||
"disallow reactive statements that don't reference reactive values.", | ||
category: "Best Practices", | ||
// TODO Switch to recommended in the major version. | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
immutable: | ||
"This statement is not reactive because all variables referenced in the reactive statement are immutable.", | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
const scopeManager = context.getSourceCode().scopeManager | ||
const globalScope = scopeManager.globalScope | ||
const toplevelScope = | ||
globalScope?.childScopes.find((scope) => scope.type === "module") || | ||
globalScope | ||
if (!globalScope || !toplevelScope) { | ||
return {} | ||
} | ||
|
||
const cacheMutableVariable = new WeakMap<Variable, boolean>() | ||
|
||
/** | ||
* Checks whether the given reference is a mutable variable or not. | ||
*/ | ||
function isMutableVariableReference(reference: Reference) { | ||
if (reference.identifier.name.startsWith("$")) { | ||
// It is reactive store reference. | ||
return true | ||
} | ||
if (!reference.resolved) { | ||
// Unknown variable | ||
return true | ||
} | ||
return isMutableVariable(reference.resolved) | ||
} | ||
|
||
/** | ||
* Checks whether the given variable is a mutable variable or not. | ||
*/ | ||
function isMutableVariable(variable: Variable) { | ||
const cache = cacheMutableVariable.get(variable) | ||
if (cache != null) { | ||
return cache | ||
} | ||
if (variable.defs.length === 0) { | ||
// Global variables are assumed to be immutable. | ||
return true | ||
} | ||
const isMutable = variable.defs.some((def) => { | ||
if (def.type === "Variable") { | ||
const parent = def.parent | ||
if (parent.kind === "const") { | ||
return false | ||
} | ||
const pp = parent.parent | ||
if ( | ||
pp && | ||
pp.type === "ExportNamedDeclaration" && | ||
pp.declaration === parent | ||
) { | ||
// Props | ||
return true | ||
} | ||
return hasWrite(variable) | ||
} | ||
if (def.type === "ImportBinding") { | ||
return false | ||
} | ||
|
||
if (def.node.type === "AssignmentExpression") { | ||
// Reactive values | ||
return true | ||
} | ||
return false | ||
}) | ||
cacheMutableVariable.set(variable, isMutable) | ||
return isMutable | ||
} | ||
|
||
/** Checks whether the given variable has a write or reactive store reference or not. */ | ||
function hasWrite(variable: Variable) { | ||
const defIds = variable.defs.map((def: Definition) => def.name) | ||
return variable.references.some( | ||
(reference) => | ||
reference.isWrite() && | ||
!defIds.some( | ||
(defId) => | ||
defId.range[0] <= reference.identifier.range[0] && | ||
reference.identifier.range[1] <= defId.range[1], | ||
), | ||
) | ||
} | ||
|
||
/** | ||
* Iterates through references to top-level variables in the given range. | ||
*/ | ||
function* iterateRangeReferences(scope: Scope, range: [number, number]) { | ||
for (const variable of scope.variables) { | ||
for (const reference of variable.references) { | ||
if ( | ||
range[0] <= reference.identifier.range[0] && | ||
reference.identifier.range[1] <= range[1] | ||
) { | ||
yield reference | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { | ||
SvelteReactiveStatement(node: AST.SvelteReactiveStatement) { | ||
for (const reference of iterateRangeReferences( | ||
toplevelScope, | ||
node.range, | ||
)) { | ||
if (reference.isWriteOnly()) { | ||
continue | ||
} | ||
if (isMutableVariableReference(reference)) { | ||
return | ||
} | ||
} | ||
if ( | ||
globalScope.through.some( | ||
(reference) => | ||
node.range[0] <= reference.identifier.range[0] && | ||
reference.identifier.range[1] <= node.range[1], | ||
) | ||
) { | ||
// Do not report if there are missing references. | ||
return | ||
} | ||
|
||
context.report({ | ||
node: | ||
node.body.type === "ExpressionStatement" && | ||
node.body.expression.type === "AssignmentExpression" && | ||
node.body.expression.operator === "=" | ||
? node.body.expression.right | ||
: node.body, | ||
messageId: "immutable", | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
tests/fixtures/rules/no-immutable-reactive-statements/invalid/immutable-let-errors.yaml
This file contains hidden or 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,18 @@ | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 3 | ||
column: 18 | ||
suggestions: null | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 4 | ||
column: 18 | ||
suggestions: null | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 5 | ||
column: 6 | ||
suggestions: null |
10 changes: 10 additions & 0 deletions
10
tests/fixtures/rules/no-immutable-reactive-statements/invalid/immutable-let-input.svelte
This file contains hidden or 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,10 @@ | ||
<script> | ||
let immutableVar = "hello" | ||
$: computed1 = `${immutableVar} ${immutableVar}` | ||
$: computed1 = fn1(immutableVar) | ||
$: console.log(immutableVar) | ||
|
||
function fn1(v) { | ||
return `${v} ${v}` | ||
} | ||
</script> |
24 changes: 24 additions & 0 deletions
24
tests/fixtures/rules/no-immutable-reactive-statements/invalid/immutable01-errors.yaml
This file contains hidden or 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,24 @@ | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 7 | ||
column: 18 | ||
suggestions: null | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 8 | ||
column: 18 | ||
suggestions: null | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 9 | ||
column: 6 | ||
suggestions: null | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 10 | ||
column: 6 | ||
suggestions: null |
20 changes: 20 additions & 0 deletions
20
tests/fixtures/rules/no-immutable-reactive-statements/invalid/immutable01-input.svelte
This file contains hidden or 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 @@ | ||
<script> | ||
import myVar from "./my-variables" | ||
let mutableVar = "hello" | ||
|
||
const immutableVar = "hello" | ||
/* ✗ BAD */ | ||
$: computed3 = fn1(immutableVar) | ||
$: computed4 = fn2() | ||
$: console.log(immutableVar) | ||
$: console.log(myVar) | ||
|
||
function fn1(v) { | ||
return `${v} ${v}` | ||
} | ||
function fn2() { | ||
return `${mutableVar} ${mutableVar}` | ||
} | ||
</script> | ||
|
||
<input bind:value={mutableVar} /> |
18 changes: 18 additions & 0 deletions
18
tests/fixtures/rules/no-immutable-reactive-statements/invalid/readonly-export01-errors.yaml
This file contains hidden or 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,18 @@ | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 12 | ||
column: 17 | ||
suggestions: null | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 13 | ||
column: 17 | ||
suggestions: null | ||
- message: | ||
This statement is not reactive because all variables referenced in the | ||
reactive statement are immutable. | ||
line: 14 | ||
column: 17 | ||
suggestions: null |
15 changes: 15 additions & 0 deletions
15
tests/fixtures/rules/no-immutable-reactive-statements/invalid/readonly-export01-input.svelte
This file contains hidden or 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,15 @@ | ||
<script> | ||
export const thisIs = "readonly" | ||
|
||
export function greet(name) { | ||
console.log(`hello ${name}!`) | ||
} | ||
|
||
export class Foo {} | ||
|
||
const immutableVar = "hello" | ||
|
||
$: message1 = greet(immutableVar) | ||
$: message2 = `this is${thisIs}` | ||
$: instance = new Foo() | ||
</script> |
19 changes: 19 additions & 0 deletions
19
tests/fixtures/rules/no-immutable-reactive-statements/valid/mutable01-input.svelte
This file contains hidden or 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,19 @@ | ||
<script> | ||
import myStore from "./my-stores" | ||
import myVar from "./my-variables" | ||
let mutableVar = "hello" | ||
export let prop | ||
/* ✓ GOOD */ | ||
$: computed1 = `${mutableVar} ${mutableVar}` | ||
$: computed2 = fn1(mutableVar) | ||
$: console.log(mutableVar) | ||
$: console.log(computed1) | ||
$: console.log($myStore) | ||
$: console.log(prop) | ||
|
||
function fn1(v) { | ||
return `${v} ${v}` | ||
} | ||
</script> | ||
|
||
<input bind:value={mutableVar} /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if prop is
const
,class
orfunction
, it should returnfalse
I think.https://svelte.dev/docs#component-format-script-1-export-creates-a-component-prop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your comment!
Effectively they are already checked. But I think I should add test cases 👍. I will add it.
if (def.type === "Variable") {
in L66 enters a branch only forvar
,let
, orconst
declarations.if (parent.kind === "const") {
in L68 enters a branch only forconst
declarations. and it returnsfalse
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The below code throws an ESLint error, but I think it shouldn't right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the statement is only processed the first time, so I think the correct behavior to be reported by the rule.
Do you think I'm misunderstanding how svelte works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry. I was wrong.
Yes. this is correct.🙏