Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
svelte/no-immutable-reactive-statements
rule (#439)
- Loading branch information
Showing
16 changed files
with
387 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 | ||
--- | ||
|
||
feat: add `svelte/no-immutable-reactive-statements` 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,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 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 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 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 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 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 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 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 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 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.