-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathno-reactive-reassign.ts
283 lines (277 loc) · 8.46 KB
/
no-reactive-reassign.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import type { TSESTree } from "@typescript-eslint/types"
import type { AST } from "svelte-eslint-parser"
import { createRule } from "../utils"
import { getPropertyName } from "@eslint-community/eslint-utils"
export default createRule("no-reactive-reassign", {
meta: {
docs: {
description: "disallow reassigning reactive values",
category: "Possible Errors",
// TODO Switch to recommended in the major version.
recommended: false,
},
schema: [
{
type: "object",
properties: {
props: {
type: "boolean",
},
},
additionalProperties: false,
},
],
messages: {
assignmentToReactiveValue: "Assignment to reactive value '{{name}}'.",
assignmentToReactiveValueProp:
"Assignment to property of reactive value '{{name}}'.",
},
type: "problem",
},
create(context) {
const props = context.options[0]?.props !== false // default true
const sourceCode = context.getSourceCode()
const scopeManager = sourceCode.scopeManager
const globalScope = scopeManager.globalScope
const toplevelScope =
globalScope?.childScopes.find((scope) => scope.type === "module") ||
globalScope
if (!globalScope || !toplevelScope) {
return {}
}
type CheckContext<P extends TSESTree.Node | AST.SvelteDirective> = {
node: TSESTree.Expression
parent: P
pathNodes: TSESTree.MemberExpression[]
}
const CHECK_REASSIGN: {
[key in TSESTree.Node["type"] | "SvelteDirective"]?: (
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Ignore
ctx: CheckContext<any>,
) =>
| null // The given expression does not reassign.
// The given expression will reassign.
| {
type: "reassign"
node: TSESTree.Node | AST.SvelteNode
pathNodes?: TSESTree.MemberExpression[]
}
// The context to check next.
| {
type: "check"
node: TSESTree.Expression
pathNodes?: TSESTree.MemberExpression[]
}
} = {
UpdateExpression:
// e.g. foo ++, foo --
({ parent }) => ({ type: "reassign", node: parent }),
UnaryExpression: ({ parent }: CheckContext<TSESTree.UnaryExpression>) => {
if (parent.operator === "delete") {
// e.g. delete foo.prop
return { type: "reassign", node: parent }
}
return null
},
AssignmentExpression: ({
node,
parent,
}: CheckContext<TSESTree.AssignmentExpression>) => {
if (parent.left === node) {
// e.g. foo = 42, foo += 42, foo -= 42
return { type: "reassign", node: parent }
}
return null
},
ForInStatement: ({
node,
parent,
}: CheckContext<TSESTree.ForInStatement>) => {
if (parent.left === node) {
// e.g. for (foo in itr)
return { type: "reassign", node: parent }
}
return null
},
ForOfStatement: ({
node,
parent,
}: CheckContext<TSESTree.ForOfStatement>) => {
if (parent.left === node) {
// e.g. for (foo of itr)
return { type: "reassign", node: parent }
}
return null
},
CallExpression: ({
node,
parent,
pathNodes,
}: CheckContext<TSESTree.CallExpression>) => {
if (pathNodes.length > 0 && parent.callee === node) {
const mem = pathNodes[pathNodes.length - 1]
const callName = getPropertyName(mem)
if (
callName &&
/^(?:push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)$/u.test(
callName,
)
) {
// e.g. foo.push()
return {
type: "reassign",
node: parent,
pathNodes: pathNodes.slice(0, -1),
}
}
}
return null
},
MemberExpression: ({
node,
parent,
pathNodes,
}: CheckContext<TSESTree.MemberExpression>) => {
if (parent.object === node) {
// The context to check next.
return {
type: "check",
node: parent,
pathNodes: [...pathNodes, parent],
}
}
return null
},
ChainExpression: ({ parent }: CheckContext<TSESTree.ChainExpression>) => {
// e.g. `foo?.prop`
// The context to check next.
return { type: "check", node: parent }
},
ConditionalExpression: ({
node,
parent,
}: CheckContext<TSESTree.ConditionalExpression>) => {
if (parent.test === node) {
return null
}
// The context to check next for `(test ? foo : bar).prop`.
return { type: "check", node: parent }
},
Property: ({ node, parent }: CheckContext<TSESTree.Property>) => {
if (
parent.value === node &&
parent.parent &&
parent.parent.type === "ObjectPattern"
) {
// The context to check next for `({a: foo} = obj)`.
return { type: "check", node: parent.parent }
}
return null
},
ArrayPattern: ({ node, parent }: CheckContext<TSESTree.ArrayPattern>) => {
if (parent.elements.includes(node as TSESTree.DestructuringPattern)) {
// The context to check next for `([foo] = obj)`.
return { type: "check", node: parent }
}
return null
},
RestElement: ({ node, parent }: CheckContext<TSESTree.RestElement>) => {
if (parent.argument === node && parent.parent) {
// The context to check next for `({...foo} = obj)`.
return {
type: "check",
node: parent.parent as
| TSESTree.ArrayPattern
| TSESTree.ObjectPattern,
}
}
return null
},
SvelteDirective: ({
node,
parent,
}: CheckContext<AST.SvelteDirective>) => {
if (parent.kind !== "Binding") {
return null
}
if (parent.shorthand || parent.expression === node) {
return {
type: "reassign",
node: parent,
}
}
return null
},
}
/**
* Returns the reassign information for the given expression node if it has a reassign.
*/
function getReassignData(expr: TSESTree.Expression) {
let pathNodes: TSESTree.MemberExpression[] = []
let node: TSESTree.Expression = expr
let parent
while ((parent = node.parent)) {
const check = CHECK_REASSIGN[parent.type]
if (!check) {
return null
}
const result = check({ node, parent, pathNodes })
if (!result) {
return null
}
pathNodes = result.pathNodes || pathNodes
if (result.type === "reassign") {
return {
node: result.node,
pathNodes,
}
}
node = result.node
}
return null
}
return {
SvelteReactiveStatement(node: AST.SvelteReactiveStatement) {
if (
node.body.type !== "ExpressionStatement" ||
node.body.expression.type !== "AssignmentExpression" ||
node.body.expression.operator !== "="
) {
return
}
const assignment = node.body.expression
for (const variable of toplevelScope.variables) {
if (!variable.defs.some((def) => def.node === assignment)) {
continue
}
for (const reference of variable.references) {
const id = reference.identifier
if (
(assignment.left.range[0] <= id.range[0] &&
id.range[1] <= assignment.left.range[1]) ||
id.type === "JSXIdentifier"
) {
continue
}
const reassign = getReassignData(id)
if (!reassign) {
continue
}
// Suppresses reporting if the props option is set to `false` and reassigned to properties.
if (!props && reassign.pathNodes.length > 0) continue
context.report({
node: reassign.node,
messageId:
reassign.pathNodes.length === 0
? "assignmentToReactiveValue"
: "assignmentToReactiveValueProp",
data: {
name: id.name,
},
})
}
}
},
}
},
})