-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathno-immutable-reactive-statements.ts
162 lines (153 loc) · 4.58 KB
/
no-immutable-reactive-statements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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",
})
},
}
},
})