|
| 1 | +import type { AST } from "svelte-eslint-parser" |
| 2 | +import { createRule } from "../utils" |
| 3 | +import type { |
| 4 | + Scope, |
| 5 | + Variable, |
| 6 | + Reference, |
| 7 | + Definition, |
| 8 | +} from "@typescript-eslint/scope-manager" |
| 9 | + |
| 10 | +export default createRule("no-immutable-reactive-statements", { |
| 11 | + meta: { |
| 12 | + docs: { |
| 13 | + description: |
| 14 | + "disallow reactive statements that don't reference reactive values.", |
| 15 | + category: "Best Practices", |
| 16 | + // TODO Switch to recommended in the major version. |
| 17 | + recommended: false, |
| 18 | + }, |
| 19 | + schema: [], |
| 20 | + messages: { |
| 21 | + immutable: |
| 22 | + "This statement is not reactive because all variables referenced in the reactive statement are immutable.", |
| 23 | + }, |
| 24 | + type: "suggestion", |
| 25 | + }, |
| 26 | + create(context) { |
| 27 | + const scopeManager = context.getSourceCode().scopeManager |
| 28 | + const globalScope = scopeManager.globalScope |
| 29 | + const toplevelScope = |
| 30 | + globalScope?.childScopes.find((scope) => scope.type === "module") || |
| 31 | + globalScope |
| 32 | + if (!globalScope || !toplevelScope) { |
| 33 | + return {} |
| 34 | + } |
| 35 | + |
| 36 | + const cacheMutableVariable = new WeakMap<Variable, boolean>() |
| 37 | + |
| 38 | + /** |
| 39 | + * Checks whether the given reference is a mutable variable or not. |
| 40 | + */ |
| 41 | + function isMutableVariableReference(reference: Reference) { |
| 42 | + if (reference.identifier.name.startsWith("$")) { |
| 43 | + // It is reactive store reference. |
| 44 | + return true |
| 45 | + } |
| 46 | + if (!reference.resolved) { |
| 47 | + // Unknown variable |
| 48 | + return true |
| 49 | + } |
| 50 | + return isMutableVariable(reference.resolved) |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Checks whether the given variable is a mutable variable or not. |
| 55 | + */ |
| 56 | + function isMutableVariable(variable: Variable) { |
| 57 | + const cache = cacheMutableVariable.get(variable) |
| 58 | + if (cache != null) { |
| 59 | + return cache |
| 60 | + } |
| 61 | + if (variable.defs.length === 0) { |
| 62 | + // Global variables are assumed to be immutable. |
| 63 | + return true |
| 64 | + } |
| 65 | + const isMutable = variable.defs.some((def) => { |
| 66 | + if (def.type === "Variable") { |
| 67 | + const parent = def.parent |
| 68 | + if (parent.kind === "const") { |
| 69 | + return false |
| 70 | + } |
| 71 | + const pp = parent.parent |
| 72 | + if ( |
| 73 | + pp && |
| 74 | + pp.type === "ExportNamedDeclaration" && |
| 75 | + pp.declaration === parent |
| 76 | + ) { |
| 77 | + // Props |
| 78 | + return true |
| 79 | + } |
| 80 | + return hasWrite(variable) |
| 81 | + } |
| 82 | + if (def.type === "ImportBinding") { |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + if (def.node.type === "AssignmentExpression") { |
| 87 | + // Reactive values |
| 88 | + return true |
| 89 | + } |
| 90 | + return false |
| 91 | + }) |
| 92 | + cacheMutableVariable.set(variable, isMutable) |
| 93 | + return isMutable |
| 94 | + } |
| 95 | + |
| 96 | + /** Checks whether the given variable has a write or reactive store reference or not. */ |
| 97 | + function hasWrite(variable: Variable) { |
| 98 | + const defIds = variable.defs.map((def: Definition) => def.name) |
| 99 | + return variable.references.some( |
| 100 | + (reference) => |
| 101 | + reference.isWrite() && |
| 102 | + !defIds.some( |
| 103 | + (defId) => |
| 104 | + defId.range[0] <= reference.identifier.range[0] && |
| 105 | + reference.identifier.range[1] <= defId.range[1], |
| 106 | + ), |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Iterates through references to top-level variables in the given range. |
| 112 | + */ |
| 113 | + function* iterateRangeReferences(scope: Scope, range: [number, number]) { |
| 114 | + for (const variable of scope.variables) { |
| 115 | + for (const reference of variable.references) { |
| 116 | + if ( |
| 117 | + range[0] <= reference.identifier.range[0] && |
| 118 | + reference.identifier.range[1] <= range[1] |
| 119 | + ) { |
| 120 | + yield reference |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return { |
| 127 | + SvelteReactiveStatement(node: AST.SvelteReactiveStatement) { |
| 128 | + for (const reference of iterateRangeReferences( |
| 129 | + toplevelScope, |
| 130 | + node.range, |
| 131 | + )) { |
| 132 | + if (reference.isWriteOnly()) { |
| 133 | + continue |
| 134 | + } |
| 135 | + if (isMutableVariableReference(reference)) { |
| 136 | + return |
| 137 | + } |
| 138 | + } |
| 139 | + if ( |
| 140 | + globalScope.through.some( |
| 141 | + (reference) => |
| 142 | + node.range[0] <= reference.identifier.range[0] && |
| 143 | + reference.identifier.range[1] <= node.range[1], |
| 144 | + ) |
| 145 | + ) { |
| 146 | + // Do not report if there are missing references. |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + context.report({ |
| 151 | + node: |
| 152 | + node.body.type === "ExpressionStatement" && |
| 153 | + node.body.expression.type === "AssignmentExpression" && |
| 154 | + node.body.expression.operator === "=" |
| 155 | + ? node.body.expression.right |
| 156 | + : node.body, |
| 157 | + messageId: "immutable", |
| 158 | + }) |
| 159 | + }, |
| 160 | + } |
| 161 | + }, |
| 162 | +}) |
0 commit comments