|
1
|
import type { TSESLint, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/utils" |
|
3
|
|
|
4
|
/** |
|
5
|
* True when `node` is something other than a pure literal — i.e. it references |
|
6
|
* a binding or computes a value at runtime. `Literal` (string/number/boolean/ |
|
7
|
* regex/etc.) and bare `TemplateLiteral` (no interpolations are non-literal in |
|
8
|
* themselves) return false; everything else (Identifier, MemberExpression, |
|
9
|
* CallExpression, …) returns true. |
|
10
|
* |
|
11
|
* Useful for skipping noise like `1 + 1` or `` `hello ${"x"}` `` when a rule |
|
12
|
* cares whether an expression touches a real binding. |
|
13
|
*/ |
|
14
|
export const isNonLiteralRef = (node: TSESTree.Node): boolean => { |
|
15
|
if ( |
|
16
|
node.type === AST_NODE_TYPES.Literal || |
|
17
|
node.type === AST_NODE_TYPES.TemplateLiteral |
|
18
|
) { |
|
19
|
return false |
|
20
|
} |
|
21
|
return true |
|
22
|
} |
|
23
|
|
|
24
|
/** |
|
25
|
* True when `node` represents an explicit `undefined` value — either the |
|
26
|
* `undefined` identifier or `void <expr>` (which always evaluates to |
|
27
|
* `undefined`). |
|
28
|
*/ |
|
29
|
export const isUndefinedExpression = (node: TSESTree.Node): boolean => { |
|
30
|
if (node.type === AST_NODE_TYPES.Identifier && node.name === "undefined") { |
|
31
|
return true |
|
32
|
} |
|
33
|
if ( |
|
34
|
node.type === AST_NODE_TYPES.UnaryExpression && |
|
35
|
node.operator === "void" |
|
36
|
) { |
|
37
|
return true |
|
38
|
} |
|
39
|
return false |
|
40
|
} |
|
41
|
|
|
42
|
/** |
|
43
|
* Strips TypeScript-only expression wrappers (`x as T`, `x satisfies T`, `x!`, |
|
44
|
* `<T>x`) that don't change the runtime value, so the underlying expression can |
|
45
|
* be inspected — e.g. `StepResponse.skip() as any` unwraps to the call. |
|
46
|
*/ |
|
47
|
export const unwrapTsExpression = ( |
|
48
|
node: TSESTree.Expression |
|
49
|
): TSESTree.Expression => { |
|
50
|
let current = node |
|
51
|
while ( |
|
52
|
current.type === AST_NODE_TYPES.TSAsExpression || |
|
53
|
current.type === AST_NODE_TYPES.TSSatisfiesExpression || |
|
54
|
current.type === AST_NODE_TYPES.TSNonNullExpression || |
|
55
|
current.type === AST_NODE_TYPES.TSTypeAssertion |
|
56
|
) { |
|
57
|
current = current.expression |
|
58
|
} |
|
59
|
return current |
|
60
|
} |
|
61
|
|
|
62
|
/** |
|
63
|
* The three AST node types that represent a function value: |
|
64
|
* `ArrowFunctionExpression`, `FunctionExpression`, and `FunctionDeclaration`. |
|
65
|
*/ |
|
66
|
export type FunctionNode = |
|
67
|
| TSESTree.ArrowFunctionExpression |
|
68
|
| TSESTree.FunctionExpression |
|
69
|
| TSESTree.FunctionDeclaration |
|
70
|
|
|
71
|
const FUNCTION_NODE_TYPES = new Set<string>([ |
|
72
|
AST_NODE_TYPES.ArrowFunctionExpression, |
|
73
|
AST_NODE_TYPES.FunctionExpression, |
|
74
|
AST_NODE_TYPES.FunctionDeclaration, |
|
75
|
]) |
|
76
|
|
|
77
|
/** |
|
78
|
* Recursively walks the AST rooted at `node`, calling `visit` for every |
|
79
|
* descendant (including `node` itself). When `visit` returns `false` the walk |
|
80
|
* does not descend into that subtree's children — useful for "skip nested |
|
81
|
* function bodies" patterns. |
|
82
|
* |
|
83
|
* Skips the `parent` back-reference. Generic over the standard TSESTree shape; |
|
84
|
* does not know about parser-specific extension keys. |
|
85
|
*/ |
|
86
|
export const walkAst = ( |
|
87
|
node: TSESTree.Node | null | undefined, |
|
88
|
visit: (node: TSESTree.Node) => boolean | void |
|
89
|
): void => { |
|
90
|
if (!node) { |
|
91
|
return |
|
92
|
} |
|
93
|
if (visit(node) === false) { |
|
94
|
return |
|
95
|
} |
|
96
|
|
|
97
|
for (const key of Object.keys(node)) { |
|
98
|
if (key === "parent") { |
|
99
|
continue |
|
100
|
} |
|
101
|
const value = (node as unknown as Record<string, unknown>)[key] |
|
102
|
if (!value) { |
|
103
|
continue |
|
104
|
} |
|
105
|
if (Array.isArray(value)) { |
|
106
|
for (const child of value) { |
|
107
|
if (child && typeof child === "object" && "type" in child) { |
|
108
|
walkAst(child as TSESTree.Node, visit) |
|
109
|
} |
|
110
|
} |
|
111
|
} else if (typeof value === "object" && "type" in (value as object)) { |
|
112
|
walkAst(value as TSESTree.Node, visit) |
|
113
|
} |
|
114
|
} |
|
115
|
} |
|
116
|
|
|
117
|
/** |
|
118
|
* True for `ArrowFunctionExpression`, `FunctionExpression`, and |
|
119
|
* `FunctionDeclaration`. Narrows to `FunctionNode`; accepts nullable input so |
|
120
|
* it can guard optional fields like a declarator's `init`. |
|
121
|
*/ |
|
122
|
export const isFunctionNode = ( |
|
123
|
node: TSESTree.Node | null | undefined |
|
124
|
): node is FunctionNode => !!node && FUNCTION_NODE_TYPES.has(node.type) |
|
125
|
|
|
126
|
/** |
|
127
|
* Returns the static key name of an object-literal property when it can be |
|
128
|
* resolved without scope analysis — either an `Identifier` key or a string |
|
129
|
* `Literal` key. Returns `null` for computed keys, non-`Property` elements |
|
130
|
* (spread, methods written as `SpreadElement`), and numeric/template keys. |
|
131
|
*/ |
|
132
|
export const getPropertyKeyName = ( |
|
133
|
prop: TSESTree.ObjectLiteralElement |
|
134
|
): string | null => { |
|
135
|
if (prop.type !== AST_NODE_TYPES.Property || prop.computed) { |
|
136
|
return null |
|
137
|
} |
|
138
|
const key = prop.key |
|
139
|
if (key.type === AST_NODE_TYPES.Identifier) { |
|
140
|
return key.name |
|
141
|
} |
|
142
|
if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") { |
|
143
|
return key.value |
|
144
|
} |
|
145
|
return null |
|
146
|
} |
|
147
|
|
|
148
|
/** |
|
149
|
* Returns the first non-computed `Property` on `obj` whose static key name |
|
150
|
* matches `name`, or `null` if none exists. Skips `SpreadElement`s and any |
|
151
|
* `Property` with a computed/non-string/non-identifier key. |
|
152
|
*/ |
|
153
|
export const findProperty = ( |
|
154
|
obj: TSESTree.ObjectExpression, |
|
155
|
name: string |
|
156
|
): TSESTree.Property | null => { |
|
157
|
for (const prop of obj.properties) { |
|
158
|
if (prop.type !== AST_NODE_TYPES.Property) { |
|
159
|
continue |
|
160
|
} |
|
161
|
if (getPropertyKeyName(prop) === name) { |
|
162
|
return prop |
|
163
|
} |
|
164
|
} |
|
165
|
return null |
|
166
|
} |
|
167
|
|
|
168
|
/** |
|
169
|
* Resolves an expression to the object literal it denotes, when that can be |
|
170
|
* determined without type information: |
|
171
|
* - An `ObjectExpression` is returned as-is. |
|
172
|
* - An `Identifier` is resolved through scope to a `const x = { ... }` binding. |
|
173
|
* |
|
174
|
* Returns `null` when the value can't be statically resolved to an object |
|
175
|
* literal (call expressions, spreads of unknown bindings, re-exports, etc.). |
|
176
|
* Callers that need to assert something about the object's shape should treat a |
|
177
|
* `null` result as "can't tell" and skip the check to avoid false positives. |
|
178
|
*/ |
|
179
|
export const resolveObjectExpression = ( |
|
180
|
node: TSESTree.Node | null | undefined, |
|
181
|
scope: TSESLint.Scope.Scope | null |
|
182
|
): TSESTree.ObjectExpression | null => { |
|
183
|
if (!node) { |
|
184
|
return null |
|
185
|
} |
|
186
|
if (node.type === AST_NODE_TYPES.ObjectExpression) { |
|
187
|
return node |
|
188
|
} |
|
189
|
if (node.type === AST_NODE_TYPES.Identifier) { |
|
190
|
const variable = findVariableInScope(scope, node.name) |
|
191
|
if (variable) { |
|
192
|
for (const def of variable.defs) { |
|
193
|
if ( |
|
194
|
def.node.type === AST_NODE_TYPES.VariableDeclarator && |
|
195
|
def.node.init?.type === AST_NODE_TYPES.ObjectExpression |
|
196
|
) { |
|
197
|
return def.node.init |
|
198
|
} |
|
199
|
} |
|
200
|
} |
|
201
|
} |
|
202
|
return null |
|
203
|
} |
|
204
|
|
|
205
|
/** |
|
206
|
* Whether an object literal is known to carry a property named `name`. |
|
207
|
* |
|
208
|
* Returns `true`/`false` when it can be determined statically, and `"unknown"` |
|
209
|
* when the object contains a `SpreadElement` — the property could be supplied |
|
210
|
* by the spread, so a rule must not flag its absence. |
|
211
|
*/ |
|
212
|
export const objectHasProperty = ( |
|
213
|
obj: TSESTree.ObjectExpression, |
|
214
|
name: string |
|
215
|
): boolean | "unknown" => { |
|
216
|
if (findProperty(obj, name)) { |
|
217
|
return true |
|
218
|
} |
|
219
|
if (obj.properties.some((p) => p.type === AST_NODE_TYPES.SpreadElement)) { |
|
220
|
return "unknown" |
|
221
|
} |
|
222
|
return false |
|
223
|
} |
|
224
|
|
|
225
|
/** |
|
226
|
* Returns the name of the `VariableDeclarator` that directly initializes |
|
227
|
* `call`, e.g. `export const myWorkflow = createWorkflow(...)` → `"myWorkflow"`. |
|
228
|
* |
|
229
|
* Only matches when the call is the direct initializer of an identifier-bound |
|
230
|
* declarator — not when nested inside an object/array/etc. on the right-hand |
|
231
|
* side. Returns `null` otherwise. |
|
232
|
*/ |
|
233
|
export const getInitializedVariableName = ( |
|
234
|
call: TSESTree.CallExpression |
|
235
|
): string | null => { |
|
236
|
const parent = call.parent |
|
237
|
if ( |
|
238
|
parent && |
|
239
|
parent.type === AST_NODE_TYPES.VariableDeclarator && |
|
240
|
parent.init === call && |
|
241
|
parent.id.type === AST_NODE_TYPES.Identifier |
|
242
|
) { |
|
243
|
return parent.id.name |
|
244
|
} |
|
245
|
return null |
|
246
|
} |
|
247
|
|
|
248
|
/** |
|
249
|
* Walks the scope chain starting at `scope`, returning the first variable that |
|
250
|
* matches `name`. Returns `null` if no enclosing scope binds the name. |
|
251
|
*/ |
|
252
|
export const findVariableInScope = ( |
|
253
|
scope: TSESLint.Scope.Scope | null, |
|
254
|
name: string |
|
255
|
): TSESLint.Scope.Variable | null => { |
|
256
|
let current: TSESLint.Scope.Scope | null = scope |
|
257
|
while (current) { |
|
258
|
const found = current.variables.find((v) => v.name === name) |
|
259
|
if (found) { |
|
260
|
return found |
|
261
|
} |
|
262
|
current = current.upper |
|
263
|
} |
|
264
|
return null |
|
265
|
} |
|
266
|
|
|
267
|
/** |
|
268
|
* Resolves an identifier to the function it refers to — either a `function` |
|
269
|
* declaration or a `const x = () => …` / `const x = function () {}` |
|
270
|
* initializer. Walks the scope chain from `scope` to find the binding. |
|
271
|
* |
|
272
|
* Returns `null` when the binding isn't a project-local function (e.g. a |
|
273
|
* re-export from another module, or a non-function value). Useful for |
|
274
|
* resolving the function behind a default-export identifier |
|
275
|
* (`export default handler`, `export { handler as default }`). |
|
276
|
*/ |
|
277
|
export const resolveFunctionFromIdentifier = ( |
|
278
|
scope: TSESLint.Scope.Scope | null, |
|
279
|
identifier: TSESTree.Identifier |
|
280
|
): FunctionNode | null => { |
|
281
|
const variable = findVariableInScope(scope, identifier.name) |
|
282
|
if (!variable) { |
|
283
|
return null |
|
284
|
} |
|
285
|
|
|
286
|
for (const def of variable.defs) { |
|
287
|
if (def.node.type === AST_NODE_TYPES.FunctionDeclaration) { |
|
288
|
return def.node |
|
289
|
} |
|
290
|
if ( |
|
291
|
def.node.type === AST_NODE_TYPES.VariableDeclarator && |
|
292
|
isFunctionNode(def.node.init) |
|
293
|
) { |
|
294
|
return def.node.init |
|
295
|
} |
|
296
|
} |
|
297
|
|
|
298
|
return null |
|
299
|
} |
|
300
|
|
|
301
|
/** |
|
302
|
* If `variable` has exactly one definition and that definition is a `const` |
|
303
|
* declaration initialized with a string literal, returns the literal's value. |
|
304
|
* Returns `null` otherwise — including for `let`/`var`, non-literal initializers, |
|
305
|
* and variables with multiple definitions (which can't be trusted as constants). |
|
306
|
*/ |
|
307
|
export const getConstStringInit = ( |
|
308
|
variable: TSESLint.Scope.Variable |
|
309
|
): string | null => { |
|
310
|
if (variable.defs.length !== 1) { |
|
311
|
return null |
|
312
|
} |
|
313
|
const def = variable.defs[0] |
|
314
|
if (def.type !== "Variable") { |
|
315
|
return null |
|
316
|
} |
|
317
|
if ( |
|
318
|
def.parent?.type !== AST_NODE_TYPES.VariableDeclaration || |
|
319
|
def.parent.kind !== "const" |
|
320
|
) { |
|
321
|
return null |
|
322
|
} |
|
323
|
const init = def.node.init |
|
324
|
if ( |
|
325
|
!init || |
|
326
|
init.type !== AST_NODE_TYPES.Literal || |
|
327
|
typeof init.value !== "string" |
|
328
|
) { |
|
329
|
return null |
|
330
|
} |
|
331
|
return init.value |
|
332
|
} |
|
333
|
|
|
334
|
/** |
|
335
|
* Resolves `node` to a string value when possible: |
|
336
|
* - If `node` is a string `Literal`, returns its value. |
|
337
|
* - If `node` is an `Identifier` that resolves to a single `const` string |
|
338
|
* initializer in an enclosing scope, returns that literal's value. |
|
339
|
* |
|
340
|
* `reportNode` is the node a rule should anchor its diagnostic on — the |
|
341
|
* literal itself when the value is inline, the identifier when resolved via |
|
342
|
* scope. Returns `null` when the value cannot be statically resolved. |
|
343
|
*/ |
|
344
|
export const resolveStaticStringValue = ( |
|
345
|
node: TSESTree.Node, |
|
346
|
scope: TSESLint.Scope.Scope |
|
347
|
): { value: string; reportNode: TSESTree.Node } | null => { |
|
348
|
if (node.type === AST_NODE_TYPES.Literal && typeof node.value === "string") { |
|
349
|
return { value: node.value, reportNode: node } |
|
350
|
} |
|
351
|
if (node.type === AST_NODE_TYPES.Identifier) { |
|
352
|
const variable = findVariableInScope(scope, node.name) |
|
353
|
const literal = variable && getConstStringInit(variable) |
|
354
|
if (literal !== null) { |
|
355
|
return { value: literal, reportNode: node } |
|
356
|
} |
|
357
|
} |
|
358
|
return null |
|
359
|
} |
|
360
|
|
|
361
|
/** |
|
362
|
* Returns the expression a function returns, when it can be determined |
|
363
|
* statically: |
|
364
|
* - Arrow with expression body: that expression. |
|
365
|
* - Arrow or function with a single top-level `return <expr>`: that expression. |
|
366
|
* Returns `null` otherwise (multiple returns, bare `return`, void). Does not |
|
367
|
* descend into nested functions. |
|
368
|
*/ |
|
369
|
export const getReturnedExpression = ( |
|
370
|
fn: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression |
|
371
|
): TSESTree.Expression | null => { |
|
372
|
if ( |
|
373
|
fn.type === AST_NODE_TYPES.ArrowFunctionExpression && |
|
374
|
fn.body.type !== AST_NODE_TYPES.BlockStatement |
|
375
|
) { |
|
376
|
return fn.body |
|
377
|
} |
|
378
|
const body = fn.body |
|
379
|
if (body.type !== AST_NODE_TYPES.BlockStatement) { |
|
380
|
return null |
|
381
|
} |
|
382
|
let found: TSESTree.Expression | null = null |
|
383
|
for (const stmt of body.body) { |
|
384
|
if (stmt.type !== AST_NODE_TYPES.ReturnStatement) { |
|
385
|
continue |
|
386
|
} |
|
387
|
if (!stmt.argument) { |
|
388
|
return null |
|
389
|
} |
|
390
|
if (found) { |
|
391
|
return null |
|
392
|
} |
|
393
|
found = stmt.argument |
|
394
|
} |
|
395
|
return found |
|
396
|
} |
|
397
|
|
|
398
|
/* |
|
399
|
* True when `fn`'s return type annotation is a `Promise<...>` reference. |
|
400
|
* |
|
401
|
* Shallow check — looks only at the outermost type reference's name. Doesn't |
|
402
|
* unwrap unions/intersections (`Promise<X> | null` returns false) or aliases |
|
403
|
* (a `type MyPromise = Promise<X>` returns false). Good enough for rules that |
|
404
|
* want to give `async`-equivalent return types a pass without paying for full |
|
405
|
* type-aware analysis. |
|
406
|
*/ |
|
407
|
export const returnTypeIsPromise = ( |
|
408
|
fn: |
|
409
|
| TSESTree.FunctionExpression |
|
410
|
| TSESTree.FunctionDeclaration |
|
411
|
| TSESTree.ArrowFunctionExpression |
|
412
|
| TSESTree.TSEmptyBodyFunctionExpression |
|
413
|
): boolean => { |
|
414
|
const annotation = fn.returnType?.typeAnnotation |
|
415
|
if (!annotation) { |
|
416
|
return false |
|
417
|
} |
|
418
|
if (annotation.type !== AST_NODE_TYPES.TSTypeReference) { |
|
419
|
return false |
|
420
|
} |
|
421
|
const name = annotation.typeName |
|
422
|
if (name.type !== AST_NODE_TYPES.Identifier) { |
|
423
|
return false |
|
424
|
} |
|
425
|
return name.name === "Promise" |
|
426
|
} |
|
427
|
|
|
428
|
/** |
|
429
|
* Returns the `constructor` `MethodDefinition` on a class body, or `null` if |
|
430
|
* the class has none. |
|
431
|
*/ |
|
432
|
export const findConstructor = ( |
|
433
|
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression |
|
434
|
): TSESTree.MethodDefinition | null => { |
|
435
|
for (const member of node.body.body) { |
|
436
|
if (member.type !== AST_NODE_TYPES.MethodDefinition) { |
|
437
|
continue |
|
438
|
} |
|
439
|
if (member.kind === "constructor") { |
|
440
|
return member |
|
441
|
} |
|
442
|
} |
|
443
|
return null |
|
444
|
} |
|
445
|
|
|
446
|
/** |
|
447
|
* Returns the string value of `node` when it's a plain string `Literal`, or |
|
448
|
* `null` otherwise (numeric/boolean literals, identifiers, template literals, |
|
449
|
* spreads, holes in a sparse array, etc.). Unlike `resolveStaticStringValue`, |
|
450
|
* this does no scope resolution — it only reads an inline string literal. |
|
451
|
*/ |
|
452
|
export const getStringLiteralValue = ( |
|
453
|
node: TSESTree.Node | null | undefined |
|
454
|
): string | null => { |
|
455
|
if ( |
|
456
|
node && |
|
457
|
node.type === AST_NODE_TYPES.Literal && |
|
458
|
typeof node.value === "string" |
|
459
|
) { |
|
460
|
return node.value |
|
461
|
} |
|
462
|
return null |
|
463
|
} |
|
464
|
|
|
465
|
/** |
|
466
|
* Returns the object literal passed as the argument at `index` (default `0`) of |
|
467
|
* `call`, or `null` when that argument is missing or isn't an inline |
|
468
|
* `ObjectExpression`. Does not resolve identifiers through scope — callers that |
|
469
|
* need that should pass the result of `resolveObjectExpression`. |
|
470
|
*/ |
|
471
|
export const getCallArgumentObject = ( |
|
472
|
call: TSESTree.CallExpression, |
|
473
|
index = 0 |
|
474
|
): TSESTree.ObjectExpression | null => { |
|
475
|
const arg = call.arguments[index] |
|
476
|
if (!arg || arg.type !== AST_NODE_TYPES.ObjectExpression) { |
|
477
|
return null |
|
478
|
} |
|
479
|
return arg |
|
480
|
} |