-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-setup-props-reactivity-loss.js
380 lines (354 loc) · 10.8 KB
/
no-setup-props-reactivity-loss.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const { findVariable } = require('@eslint-community/eslint-utils')
const utils = require('../utils')
/**
* @typedef {'props'|'prop'} PropIdKind
* - `'props'`: A node is a container object that has props.
* - `'prop'`: A node is a variable with one prop.
*/
/**
* @typedef {object} PropId
* @property {Pattern} node
* @property {PropIdKind} kind
*/
/**
* Iterates over Prop identifiers by parsing the given pattern
* in the left operand of defineProps().
* @param {Pattern} node
* @returns {IterableIterator<PropId>}
*/
function* iteratePropIds(node) {
switch (node.type) {
case 'ObjectPattern': {
for (const prop of node.properties) {
yield prop.type === 'Property'
? {
// e.g. `const { prop } = defineProps()`
node: unwrapAssignment(prop.value),
kind: 'prop'
}
: {
// RestElement
// e.g. `const { x, ...prop } = defineProps()`
node: unwrapAssignment(prop.argument),
kind: 'props'
}
}
break
}
default: {
// e.g. `const props = defineProps()`
yield { node: unwrapAssignment(node), kind: 'props' }
}
}
}
/**
* @template {Pattern} T
* @param {T} node
* @returns {Pattern}
*/
function unwrapAssignment(node) {
if (node.type === 'AssignmentPattern') {
return node.left
}
return node
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'disallow usages that lose the reactivity of `props` passed to `setup`',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-setup-props-reactivity-loss.html'
},
fixable: null,
schema: [],
messages: {
destructuring:
'Destructuring the `props` will cause the value to lose reactivity.',
getProperty:
'Getting a value from the `props` in root scope of `{{scopeName}}` will cause the value to lose reactivity.'
}
},
/**
* @param {RuleContext} context
* @returns {RuleListener}
**/
create(context) {
/**
* @typedef {object} ScopePropsReferences
* @property {object} refs
* @property {Set<Identifier>} refs.props A set of references to container objects with multiple props.
* @property {Set<Identifier>} refs.prop A set of references a variable with one property.
* @property {string} scopeName
*/
/** @type {Map<FunctionDeclaration | FunctionExpression | ArrowFunctionExpression | Program, ScopePropsReferences>} */
const setupScopePropsReferenceIds = new Map()
const wrapperExpressionTypes = new Set([
'ArrayExpression',
'ObjectExpression'
])
/**
* @param {ESNode} node
* @param {string} messageId
* @param {string} scopeName
*/
function report(node, messageId, scopeName) {
context.report({
node,
messageId,
data: {
scopeName
}
})
}
/**
* @param {Pattern} left
* @param {Expression | null} right
* @param {ScopePropsReferences} propsReferences
*/
function verify(left, right, propsReferences) {
if (!right) {
return
}
const rightNode = utils.skipChainExpression(right)
if (
wrapperExpressionTypes.has(rightNode.type) &&
isPropsMemberAccessed(rightNode, propsReferences)
) {
// e.g. `const foo = { x: props.x }`
report(rightNode, 'getProperty', propsReferences.scopeName)
return
}
// Get the expression that provides the value.
/** @type {Expression | Super} */
let expression = rightNode
while (expression.type === 'MemberExpression') {
expression = utils.skipChainExpression(expression.object)
}
/** A list of expression nodes to verify */
const expressions =
expression.type === 'TemplateLiteral'
? expression.expressions
: expression.type === 'ConditionalExpression'
? [expression.test, expression.consequent, expression.alternate]
: expression.type === 'Identifier'
? [expression]
: []
if (
(left.type === 'ArrayPattern' || left.type === 'ObjectPattern') &&
expressions.some(
(expr) =>
expr.type === 'Identifier' && propsReferences.refs.props.has(expr)
)
) {
// e.g. `const {foo} = props`
report(left, 'getProperty', propsReferences.scopeName)
return
}
const reportNode = expressions.find((expr) =>
isPropsMemberAccessed(expr, propsReferences)
)
if (reportNode) {
report(reportNode, 'getProperty', propsReferences.scopeName)
}
}
/**
* @param {Expression | Super} node
* @param {ScopePropsReferences} propsReferences
*/
function isPropsMemberAccessed(node, propsReferences) {
for (const props of propsReferences.refs.props) {
const isPropsInExpressionRange = utils.inRange(node.range, props)
const isPropsMemberExpression =
props.parent.type === 'MemberExpression' &&
props.parent.object === props
if (isPropsInExpressionRange && isPropsMemberExpression) {
return true
}
}
// Checks for actual member access using prop destructuring.
for (const prop of propsReferences.refs.prop) {
const isPropsInExpressionRange = utils.inRange(node.range, prop)
if (isPropsInExpressionRange) {
return true
}
}
return false
}
/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression | Program} scopeNode
*/
/**
* @type {ScopeStack | null}
*/
let scopeStack = null
/**
* @param {PropId} propId
* @param {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression | Program} scopeNode
* @param {import('eslint').Scope.Scope} currentScope
* @param {string} scopeName
*/
function processPropId({ node, kind }, scopeNode, currentScope, scopeName) {
if (
node.type === 'RestElement' ||
node.type === 'AssignmentPattern' ||
node.type === 'MemberExpression'
) {
// cannot check
return
}
if (node.type === 'ArrayPattern' || node.type === 'ObjectPattern') {
report(node, 'destructuring', scopeName)
return
}
const variable = findVariable(currentScope, node)
if (!variable) {
return
}
let scopePropsReferences = setupScopePropsReferenceIds.get(scopeNode)
if (!scopePropsReferences) {
scopePropsReferences = {
refs: {
props: new Set(),
prop: new Set()
},
scopeName
}
setupScopePropsReferenceIds.set(scopeNode, scopePropsReferences)
}
const propsReferenceIds = scopePropsReferences.refs[kind]
for (const reference of variable.references) {
// If reference is in another scope, we can't check it.
if (reference.from !== currentScope) {
continue
}
if (!reference.isRead()) {
continue
}
propsReferenceIds.add(reference.identifier)
}
}
return utils.compositingVisitors(
{
/**
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression | Program} node
*/
'Program, :function'(node) {
scopeStack = {
upper: scopeStack,
scopeNode: node
}
},
/**
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression | Program} node
*/
'Program, :function:exit'(node) {
scopeStack = scopeStack && scopeStack.upper
setupScopePropsReferenceIds.delete(node)
},
/**
* @param {CallExpression} node
*/
CallExpression(node) {
if (!scopeStack) {
return
}
const propsReferenceIds = setupScopePropsReferenceIds.get(
scopeStack.scopeNode
)
if (!propsReferenceIds) {
return
}
if (isPropsMemberAccessed(node, propsReferenceIds)) {
report(node, 'getProperty', propsReferenceIds.scopeName)
}
},
/**
* @param {VariableDeclarator} node
*/
VariableDeclarator(node) {
if (!scopeStack) {
return
}
const propsReferenceIds = setupScopePropsReferenceIds.get(
scopeStack.scopeNode
)
if (!propsReferenceIds) {
return
}
verify(node.id, node.init, propsReferenceIds)
},
/**
* @param {AssignmentExpression} node
*/
AssignmentExpression(node) {
if (!scopeStack) {
return
}
const propsReferenceIds = setupScopePropsReferenceIds.get(
scopeStack.scopeNode
)
if (!propsReferenceIds) {
return
}
verify(node.left, node.right, propsReferenceIds)
}
},
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node) {
let target = node
if (
target.parent &&
target.parent.type === 'CallExpression' &&
target.parent.arguments[0] === target &&
target.parent.callee.type === 'Identifier' &&
target.parent.callee.name === 'withDefaults'
) {
target = target.parent
}
if (!target.parent) {
return
}
/** @type {Pattern|null} */
let id = null
if (target.parent.type === 'VariableDeclarator') {
id = target.parent.init === target ? target.parent.id : null
} else if (target.parent.type === 'AssignmentExpression') {
id = target.parent.right === target ? target.parent.left : null
}
if (!id) return
const currentScope = utils.getScope(context, node)
for (const propId of iteratePropIds(id)) {
processPropId(
propId,
context.getSourceCode().ast,
currentScope,
'<script setup>'
)
}
}
}),
utils.defineVueVisitor(context, {
onSetupFunctionEnter(node) {
const currentScope = utils.getScope(context, node)
const propsParam = utils.skipDefaultParamValue(node.params[0])
if (!propsParam) return
processPropId(
{ node: propsParam, kind: 'props' },
node,
currentScope,
'setup()'
)
}
})
)
}
}