-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathexhaustive-deps.utils.ts
More file actions
439 lines (381 loc) · 12 KB
/
exhaustive-deps.utils.ts
File metadata and controls
439 lines (381 loc) · 12 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { ASTUtils } from '../../utils/ast-utils'
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
export const ExhaustiveDepsUtils = {
isRelevantReference(params: {
sourceCode: Readonly<TSESLint.SourceCode>
reference: TSESLint.Scope.Reference
scopeManager: TSESLint.Scope.ScopeManager
node: TSESTree.Node
filename: string
}) {
const { sourceCode, reference, scopeManager, node, filename } = params
const component = ASTUtils.getFunctionAncestor(sourceCode, node)
const queryFnScope = scopeManager.acquire(node)
if (queryFnScope === null || reference.isValueReference === false) {
return false
}
let currentScope = reference.resolved?.scope ?? null
while (currentScope !== null) {
if (currentScope === queryFnScope) {
return false
}
currentScope = currentScope.upper
}
if (component !== undefined) {
if (
!ASTUtils.isDeclaredInNode({
scopeManager,
reference,
functionNode: component,
})
) {
return false
}
} else {
const isVueFile = filename.endsWith('.vue')
if (!isVueFile) {
return false
}
const definition = reference.resolved?.defs[0]
const isGlobalVariable = definition === undefined
const isImport = definition?.type === 'ImportBinding'
if (isGlobalVariable || isImport) {
return false
}
}
return (
reference.identifier.name !== 'undefined' &&
reference.identifier.parent.type !== AST_NODE_TYPES.NewExpression &&
!ExhaustiveDepsUtils.isInstanceOfKind(reference.identifier.parent)
)
},
/**
* Given required refs and existing queryKey entries, compute missing dependency paths
* respecting allowlisted variables and types.
*/
computeFilteredMissingPaths(params: {
requiredRefs: Array<{
path: string
root: string
allowlistedByType: boolean
}>
allowlistedVariables: Set<string>
existingRootIdentifiers: Set<string>
existingFullPaths: Set<string>
}): Array<string> {
const {
requiredRefs,
allowlistedVariables,
existingRootIdentifiers,
existingFullPaths,
} = params
const missingPaths = new Set<string>()
for (const { root, path, allowlistedByType } of requiredRefs) {
// If root itself is present in the key, it covers all members
if (existingRootIdentifiers.has(root)) continue
if (allowlistedVariables.has(root)) continue
if (existingFullPaths.has(path)) continue
if (allowlistedByType) continue
missingPaths.add(path)
}
// Collapse descendants: if a root is already missing, drop deeper paths
for (const path of missingPaths) {
const root = path.split('.')[0]
if (root !== path && root !== undefined && missingPaths.has(root)) {
missingPaths.delete(path)
}
}
return Array.from(missingPaths)
},
/**
* Extract existing queryKey deps as root identifiers and full member paths.
*/
collectQueryKeyDeps(params: {
sourceCode: Readonly<TSESLint.SourceCode>
scopeManager: TSESLint.Scope.ScopeManager
queryKeyNode: TSESTree.Node
}): { roots: Set<string>; paths: Set<string> } {
const { sourceCode, scopeManager, queryKeyNode } = params
const roots = new Set<string>()
const paths = new Set<string>()
const visitorKeys = sourceCode.visitorKeys
function addRoot(name: string) {
const cleaned = ExhaustiveDepsUtils.normalizeChain(name)
roots.add(cleaned)
paths.add(cleaned)
}
function addFull(text: string) {
const cleaned = ExhaustiveDepsUtils.normalizeChain(text)
paths.add(cleaned)
}
function addRefPath(
refPath: {
path: string
root: string
coversRootMembers: boolean
} | null,
) {
if (!refPath) return
if (refPath.coversRootMembers) {
addRoot(refPath.root)
return
}
addFull(refPath.path)
}
function visitChildren(node: TSESTree.Node): void {
const keys = (visitorKeys[node.type] ?? []) as ReadonlyArray<
keyof TSESTree.Node
>
for (const key of keys) {
const value = node[key]
if (Array.isArray(value)) {
for (const item of value) {
if (ExhaustiveDepsUtils.isNode(item)) {
visit(item)
}
}
continue
}
if (ExhaustiveDepsUtils.isNode(value)) {
visit(value)
}
}
}
function visit(node: TSESTree.Node | null | undefined): void {
if (!node) return
switch (node.type) {
case AST_NODE_TYPES.Identifier: {
addRefPath(
ExhaustiveDepsUtils.computeRefPath({
identifier: node,
sourceCode: sourceCode,
}),
)
return
}
case AST_NODE_TYPES.ArrowFunctionExpression:
case AST_NODE_TYPES.FunctionExpression:
for (const reference of ExhaustiveDepsUtils.collectExternalRefsInFunction(
{
functionNode: node,
scopeManager: scopeManager,
},
)) {
if (reference.identifier.type !== AST_NODE_TYPES.Identifier) {
continue
}
addRefPath(
ExhaustiveDepsUtils.computeRefPath({
identifier: reference.identifier,
sourceCode: sourceCode,
}),
)
}
return
case AST_NODE_TYPES.Property:
visit(node.value)
return
case AST_NODE_TYPES.MemberExpression:
if (
node.parent.type === AST_NODE_TYPES.CallExpression &&
node.parent.callee === node &&
node.object.type === AST_NODE_TYPES.Identifier
) {
addRoot(node.object.name)
} else {
visit(node.object)
}
return
case AST_NODE_TYPES.CallExpression:
node.arguments.forEach((argument) => visit(argument))
switch (node.callee.type) {
case AST_NODE_TYPES.Identifier:
case AST_NODE_TYPES.MemberExpression:
case AST_NODE_TYPES.ChainExpression:
case AST_NODE_TYPES.TSNonNullExpression:
visit(node.callee)
break
}
return
}
visitChildren(node)
}
visit(queryKeyNode)
return { roots, paths }
},
isNode(value: unknown): value is TSESTree.Node {
return (
typeof value === 'object' &&
value !== null &&
'type' in value &&
typeof value.type === 'string'
)
},
/**
* Checks whether the resolved variable is allowlisted by its type annotation
*/
variableIsAllowlistedByType(params: {
allowlistedTypes: Set<string>
variable: TSESLint.Scope.Variable | null
}): boolean {
const { allowlistedTypes, variable } = params
if (allowlistedTypes.size === 0) return false
if (!variable) return false
for (const id of variable.identifiers) {
if (id.typeAnnotation) {
const typeIdentifiers = new Set<string>()
ExhaustiveDepsUtils.collectTypeIdentifiers(
id.typeAnnotation.typeAnnotation,
typeIdentifiers,
)
for (const typeIdentifier of typeIdentifiers) {
if (allowlistedTypes.has(typeIdentifier)) return true
}
}
}
return false
},
isInstanceOfKind(node: TSESTree.Node) {
return (
node.type === AST_NODE_TYPES.BinaryExpression &&
node.operator === 'instanceof'
)
},
/**
* Normalizes a chain by removing optional chaining operators
*
* Example: `a?.b.c!` -> `a.b.c`
*/
normalizeChain(text: string): string {
return text.replace(/(?:\?(\.)|!)/g, '$1').replace(/\s+/g, '')
},
/**
* Computes the reference path for an identifier
*
* Example: `a.b.c!` -> `{ path: 'a.b.c', root: 'a' }`
*/
computeRefPath(params: {
identifier: TSESTree.Identifier
sourceCode: Readonly<TSESLint.SourceCode>
}): { path: string; root: string; coversRootMembers: boolean } | null {
const { identifier, sourceCode } = params
const fullChainNode = ASTUtils.traverseUpOnly(identifier, [
AST_NODE_TYPES.MemberExpression,
AST_NODE_TYPES.TSNonNullExpression,
AST_NODE_TYPES.Identifier,
])
const fullText = ExhaustiveDepsUtils.normalizeChain(
sourceCode.getText(fullChainNode),
)
const parent = fullChainNode.parent
let dependencyPath = fullText
let coversRootMembers = fullText === identifier.name
if (
parent &&
parent.type === AST_NODE_TYPES.CallExpression &&
parent.callee === fullChainNode
) {
const segments = fullText.split('.')
if (segments.length > 1) {
dependencyPath = segments.slice(0, -1).join('.')
}
coversRootMembers = false
}
dependencyPath =
dependencyPath.split('.')[0] === '' ? identifier.name : dependencyPath
const root = dependencyPath.split('.')[0]
return {
path: dependencyPath,
root: root ?? identifier.name,
coversRootMembers: coversRootMembers && dependencyPath === root,
}
},
collectExternalRefsInFunction(params: {
functionNode: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression
scopeManager: TSESLint.Scope.ScopeManager
}): Array<TSESLint.Scope.Reference> {
const { functionNode, scopeManager } = params
const functionScope = scopeManager.acquire(functionNode)
if (functionScope === null) {
return []
}
const externalRefs: Array<TSESLint.Scope.Reference> = []
function collect(scope: TSESLint.Scope.Scope) {
for (const reference of scope.references) {
if (!reference.isRead() || reference.resolved === null) {
continue
}
let currentScope: TSESLint.Scope.Scope | null = reference.resolved.scope
let declaredInsideFunction = false
while (currentScope !== null) {
if (currentScope === functionScope) {
declaredInsideFunction = true
break
}
currentScope = currentScope.upper
}
if (!declaredInsideFunction) {
externalRefs.push(reference)
}
}
for (const childScope of scope.childScopes) {
collect(childScope)
}
}
collect(functionScope)
return externalRefs
},
/**
* Recursively collects type identifiers from a type annotation
*/
collectTypeIdentifiers(typeNode: TSESTree.TypeNode, out: Set<string>): void {
switch (typeNode.type) {
case AST_NODE_TYPES.TSTypeReference: {
if (typeNode.typeName.type === AST_NODE_TYPES.Identifier) {
out.add(typeNode.typeName.name)
}
break
}
case AST_NODE_TYPES.TSUnionType:
case AST_NODE_TYPES.TSIntersectionType: {
typeNode.types.forEach((t) =>
ExhaustiveDepsUtils.collectTypeIdentifiers(t, out),
)
break
}
case AST_NODE_TYPES.TSArrayType: {
ExhaustiveDepsUtils.collectTypeIdentifiers(typeNode.elementType, out)
break
}
case AST_NODE_TYPES.TSTupleType: {
typeNode.elementTypes.forEach((et) =>
ExhaustiveDepsUtils.collectTypeIdentifiers(et, out),
)
break
}
}
},
/**
* Gets the function expression nodes from a queryFn property, handling conditional expressions.
* When neither branch is skipToken, returns both branches so all deps are scanned.
*/
getQueryFnNodes(queryFn: TSESTree.Property): Array<TSESTree.Node> {
if (queryFn.value.type !== AST_NODE_TYPES.ConditionalExpression) {
return [queryFn.value]
}
if (
queryFn.value.consequent.type === AST_NODE_TYPES.Identifier &&
queryFn.value.consequent.name === 'skipToken'
) {
return [queryFn.value.alternate]
}
if (
queryFn.value.alternate.type === AST_NODE_TYPES.Identifier &&
queryFn.value.alternate.name === 'skipToken'
) {
return [queryFn.value.consequent]
}
return [queryFn.value.consequent, queryFn.value.alternate]
},
}