@saasmakers/eslint 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/chunks/formatters.cjs +248 -0
  2. package/dist/chunks/formatters.mjs +246 -0
  3. package/dist/chunks/import.cjs +53 -0
  4. package/dist/chunks/import.mjs +51 -0
  5. package/dist/chunks/index.cjs +120 -0
  6. package/dist/chunks/index.mjs +101 -0
  7. package/dist/chunks/index2.cjs +880 -0
  8. package/dist/chunks/index2.mjs +868 -0
  9. package/dist/chunks/index3.cjs +982 -0
  10. package/dist/chunks/index3.mjs +979 -0
  11. package/dist/chunks/index4.cjs +12099 -0
  12. package/dist/chunks/index4.mjs +12077 -0
  13. package/dist/chunks/jsdoc.cjs +38398 -0
  14. package/dist/chunks/jsdoc.mjs +38391 -0
  15. package/dist/chunks/regexp.cjs +18518 -0
  16. package/dist/chunks/regexp.mjs +18511 -0
  17. package/dist/chunks/stylistic.cjs +23935 -0
  18. package/dist/chunks/stylistic.mjs +23932 -0
  19. package/dist/chunks/typescript.cjs +56168 -0
  20. package/dist/chunks/typescript.mjs +56154 -0
  21. package/dist/chunks/unicorn.cjs +82775 -0
  22. package/dist/chunks/unicorn.mjs +82764 -0
  23. package/dist/chunks/vue.cjs +96233 -0
  24. package/dist/chunks/vue.mjs +96220 -0
  25. package/dist/eslint.config.cjs +19 -10875
  26. package/dist/eslint.config.d.cts +3897 -16
  27. package/dist/eslint.config.d.mts +3897 -16
  28. package/dist/eslint.config.d.ts +3897 -16
  29. package/dist/eslint.config.mjs +18 -10853
  30. package/dist/index.cjs +1 -1
  31. package/dist/index.d.cts +6 -2
  32. package/dist/index.d.mts +6 -2
  33. package/dist/index.d.ts +6 -2
  34. package/dist/index.mjs +1 -1
  35. package/dist/shared/eslint.05nu4VbT.mjs +9 -0
  36. package/dist/shared/eslint.07qTxm9w.mjs +3352 -0
  37. package/dist/shared/eslint.6MAvpL4q.cjs +2141 -0
  38. package/dist/shared/{eslint.CohBuu1-.mjs → eslint.B3ywQ3NK.mjs} +157 -331
  39. package/dist/shared/eslint.BGpVg2tt.cjs +13 -0
  40. package/dist/shared/eslint.BL4sYiVQ.cjs +820 -0
  41. package/dist/shared/eslint.BOOP2x9L.cjs +67 -0
  42. package/dist/shared/eslint.Bf7aat-e.mjs +10 -0
  43. package/dist/shared/eslint.Bh1W2iVQ.cjs +37181 -0
  44. package/dist/shared/eslint.Bl69eiyD.cjs +7073 -0
  45. package/dist/shared/eslint.BtkqW7nC.mjs +818 -0
  46. package/dist/shared/eslint.C12_M0Cw.cjs +9 -0
  47. package/dist/shared/eslint.CMfxPSSy.cjs +14 -0
  48. package/dist/shared/eslint.COweQ1RR.mjs +5 -0
  49. package/dist/shared/eslint.CUi9znUC.mjs +13 -0
  50. package/dist/shared/eslint.Cg6Ty7p7.mjs +2699 -0
  51. package/dist/shared/eslint.CxAZpd0w.cjs +3365 -0
  52. package/dist/shared/eslint.CyJA7jO6.cjs +3813 -0
  53. package/dist/shared/eslint.DDD2xc4l.cjs +25 -0
  54. package/dist/shared/eslint.DI7QBrVD.mjs +6 -0
  55. package/dist/shared/eslint.DUamuDzp.cjs +7 -0
  56. package/dist/shared/eslint.DV_fpPxQ.mjs +3805 -0
  57. package/dist/shared/{eslint.DhFjwkxh.cjs → eslint.Dhg0jKDi.cjs} +167 -330
  58. package/dist/shared/eslint.Difk5awg.mjs +2139 -0
  59. package/dist/shared/eslint.Dlgr3LGM.mjs +7070 -0
  60. package/dist/shared/eslint.DoYGbUIG.cjs +2724 -0
  61. package/dist/shared/eslint.Dprsk9zl.mjs +65 -0
  62. package/dist/shared/eslint.DuJbNenz.mjs +37140 -0
  63. package/dist/shared/eslint.MfgVmFE7.cjs +3054 -0
  64. package/dist/shared/eslint.W7RM7aEw.mjs +3052 -0
  65. package/package.json +2 -1
@@ -0,0 +1,2699 @@
1
+ /**
2
+ * @typedef {{ readonly [type: string]: ReadonlyArray<string> }} VisitorKeys
3
+ */
4
+
5
+ /**
6
+ * @type {VisitorKeys}
7
+ */
8
+ const KEYS = {
9
+ ArrayExpression: [
10
+ "elements"
11
+ ],
12
+ ArrayPattern: [
13
+ "elements"
14
+ ],
15
+ ArrowFunctionExpression: [
16
+ "params",
17
+ "body"
18
+ ],
19
+ AssignmentExpression: [
20
+ "left",
21
+ "right"
22
+ ],
23
+ AssignmentPattern: [
24
+ "left",
25
+ "right"
26
+ ],
27
+ AwaitExpression: [
28
+ "argument"
29
+ ],
30
+ BinaryExpression: [
31
+ "left",
32
+ "right"
33
+ ],
34
+ BlockStatement: [
35
+ "body"
36
+ ],
37
+ BreakStatement: [
38
+ "label"
39
+ ],
40
+ CallExpression: [
41
+ "callee",
42
+ "arguments"
43
+ ],
44
+ CatchClause: [
45
+ "param",
46
+ "body"
47
+ ],
48
+ ChainExpression: [
49
+ "expression"
50
+ ],
51
+ ClassBody: [
52
+ "body"
53
+ ],
54
+ ClassDeclaration: [
55
+ "id",
56
+ "superClass",
57
+ "body"
58
+ ],
59
+ ClassExpression: [
60
+ "id",
61
+ "superClass",
62
+ "body"
63
+ ],
64
+ ConditionalExpression: [
65
+ "test",
66
+ "consequent",
67
+ "alternate"
68
+ ],
69
+ ContinueStatement: [
70
+ "label"
71
+ ],
72
+ DebuggerStatement: [],
73
+ DoWhileStatement: [
74
+ "body",
75
+ "test"
76
+ ],
77
+ EmptyStatement: [],
78
+ ExperimentalRestProperty: [
79
+ "argument"
80
+ ],
81
+ ExperimentalSpreadProperty: [
82
+ "argument"
83
+ ],
84
+ ExportAllDeclaration: [
85
+ "exported",
86
+ "source"
87
+ ],
88
+ ExportDefaultDeclaration: [
89
+ "declaration"
90
+ ],
91
+ ExportNamedDeclaration: [
92
+ "declaration",
93
+ "specifiers",
94
+ "source"
95
+ ],
96
+ ExportSpecifier: [
97
+ "exported",
98
+ "local"
99
+ ],
100
+ ExpressionStatement: [
101
+ "expression"
102
+ ],
103
+ ForInStatement: [
104
+ "left",
105
+ "right",
106
+ "body"
107
+ ],
108
+ ForOfStatement: [
109
+ "left",
110
+ "right",
111
+ "body"
112
+ ],
113
+ ForStatement: [
114
+ "init",
115
+ "test",
116
+ "update",
117
+ "body"
118
+ ],
119
+ FunctionDeclaration: [
120
+ "id",
121
+ "params",
122
+ "body"
123
+ ],
124
+ FunctionExpression: [
125
+ "id",
126
+ "params",
127
+ "body"
128
+ ],
129
+ Identifier: [],
130
+ IfStatement: [
131
+ "test",
132
+ "consequent",
133
+ "alternate"
134
+ ],
135
+ ImportDeclaration: [
136
+ "specifiers",
137
+ "source"
138
+ ],
139
+ ImportDefaultSpecifier: [
140
+ "local"
141
+ ],
142
+ ImportExpression: [
143
+ "source"
144
+ ],
145
+ ImportNamespaceSpecifier: [
146
+ "local"
147
+ ],
148
+ ImportSpecifier: [
149
+ "imported",
150
+ "local"
151
+ ],
152
+ JSXAttribute: [
153
+ "name",
154
+ "value"
155
+ ],
156
+ JSXClosingElement: [
157
+ "name"
158
+ ],
159
+ JSXClosingFragment: [],
160
+ JSXElement: [
161
+ "openingElement",
162
+ "children",
163
+ "closingElement"
164
+ ],
165
+ JSXEmptyExpression: [],
166
+ JSXExpressionContainer: [
167
+ "expression"
168
+ ],
169
+ JSXFragment: [
170
+ "openingFragment",
171
+ "children",
172
+ "closingFragment"
173
+ ],
174
+ JSXIdentifier: [],
175
+ JSXMemberExpression: [
176
+ "object",
177
+ "property"
178
+ ],
179
+ JSXNamespacedName: [
180
+ "namespace",
181
+ "name"
182
+ ],
183
+ JSXOpeningElement: [
184
+ "name",
185
+ "attributes"
186
+ ],
187
+ JSXOpeningFragment: [],
188
+ JSXSpreadAttribute: [
189
+ "argument"
190
+ ],
191
+ JSXSpreadChild: [
192
+ "expression"
193
+ ],
194
+ JSXText: [],
195
+ LabeledStatement: [
196
+ "label",
197
+ "body"
198
+ ],
199
+ Literal: [],
200
+ LogicalExpression: [
201
+ "left",
202
+ "right"
203
+ ],
204
+ MemberExpression: [
205
+ "object",
206
+ "property"
207
+ ],
208
+ MetaProperty: [
209
+ "meta",
210
+ "property"
211
+ ],
212
+ MethodDefinition: [
213
+ "key",
214
+ "value"
215
+ ],
216
+ NewExpression: [
217
+ "callee",
218
+ "arguments"
219
+ ],
220
+ ObjectExpression: [
221
+ "properties"
222
+ ],
223
+ ObjectPattern: [
224
+ "properties"
225
+ ],
226
+ PrivateIdentifier: [],
227
+ Program: [
228
+ "body"
229
+ ],
230
+ Property: [
231
+ "key",
232
+ "value"
233
+ ],
234
+ PropertyDefinition: [
235
+ "key",
236
+ "value"
237
+ ],
238
+ RestElement: [
239
+ "argument"
240
+ ],
241
+ ReturnStatement: [
242
+ "argument"
243
+ ],
244
+ SequenceExpression: [
245
+ "expressions"
246
+ ],
247
+ SpreadElement: [
248
+ "argument"
249
+ ],
250
+ StaticBlock: [
251
+ "body"
252
+ ],
253
+ Super: [],
254
+ SwitchCase: [
255
+ "test",
256
+ "consequent"
257
+ ],
258
+ SwitchStatement: [
259
+ "discriminant",
260
+ "cases"
261
+ ],
262
+ TaggedTemplateExpression: [
263
+ "tag",
264
+ "quasi"
265
+ ],
266
+ TemplateElement: [],
267
+ TemplateLiteral: [
268
+ "quasis",
269
+ "expressions"
270
+ ],
271
+ ThisExpression: [],
272
+ ThrowStatement: [
273
+ "argument"
274
+ ],
275
+ TryStatement: [
276
+ "block",
277
+ "handler",
278
+ "finalizer"
279
+ ],
280
+ UnaryExpression: [
281
+ "argument"
282
+ ],
283
+ UpdateExpression: [
284
+ "argument"
285
+ ],
286
+ VariableDeclaration: [
287
+ "declarations"
288
+ ],
289
+ VariableDeclarator: [
290
+ "id",
291
+ "init"
292
+ ],
293
+ WhileStatement: [
294
+ "test",
295
+ "body"
296
+ ],
297
+ WithStatement: [
298
+ "object",
299
+ "body"
300
+ ],
301
+ YieldExpression: [
302
+ "argument"
303
+ ]
304
+ };
305
+
306
+ // Types.
307
+ const NODE_TYPES = Object.keys(KEYS);
308
+
309
+ // Freeze the keys.
310
+ for (const type of NODE_TYPES) {
311
+ Object.freeze(KEYS[type]);
312
+ }
313
+ Object.freeze(KEYS);
314
+
315
+ /**
316
+ * @author Toru Nagashima <https://github.com/mysticatea>
317
+ * See LICENSE file in root directory for full license.
318
+ */
319
+
320
+ /**
321
+ * @typedef {import('./visitor-keys.js').VisitorKeys} VisitorKeys
322
+ */
323
+
324
+ // List to ignore keys.
325
+ const KEY_BLACKLIST = new Set([
326
+ "parent",
327
+ "leadingComments",
328
+ "trailingComments"
329
+ ]);
330
+
331
+ /**
332
+ * Check whether a given key should be used or not.
333
+ * @param {string} key The key to check.
334
+ * @returns {boolean} `true` if the key should be used.
335
+ */
336
+ function filterKey(key) {
337
+ return !KEY_BLACKLIST.has(key) && key[0] !== "_";
338
+ }
339
+
340
+ /**
341
+ * Get visitor keys of a given node.
342
+ * @param {object} node The AST node to get keys.
343
+ * @returns {readonly string[]} Visitor keys of the node.
344
+ */
345
+ function getKeys(node) {
346
+ return Object.keys(node).filter(filterKey);
347
+ }
348
+
349
+ /** @typedef {import("eslint").Scope.Scope} Scope */
350
+ /** @typedef {import("estree").Node} Node */
351
+
352
+ /**
353
+ * Get the innermost scope which contains a given location.
354
+ * @param {Scope} initialScope The initial scope to search.
355
+ * @param {Node} node The location to search.
356
+ * @returns {Scope} The innermost scope.
357
+ */
358
+ function getInnermostScope(initialScope, node) {
359
+ const location = /** @type {[number, number]} */ (node.range)[0];
360
+
361
+ let scope = initialScope;
362
+ let found = false;
363
+ do {
364
+ found = false;
365
+ for (const childScope of scope.childScopes) {
366
+ const range = /** @type {[number, number]} */ (
367
+ childScope.block.range
368
+ );
369
+
370
+ if (range[0] <= location && location < range[1]) {
371
+ scope = childScope;
372
+ found = true;
373
+ break
374
+ }
375
+ }
376
+ } while (found)
377
+
378
+ return scope
379
+ }
380
+
381
+ /** @typedef {import("eslint").Scope.Scope} Scope */
382
+ /** @typedef {import("eslint").Scope.Variable} Variable */
383
+ /** @typedef {import("estree").Identifier} Identifier */
384
+
385
+ /**
386
+ * Find the variable of a given name.
387
+ * @param {Scope} initialScope The scope to start finding.
388
+ * @param {string|Identifier} nameOrNode The variable name to find. If this is a Node object then it should be an Identifier node.
389
+ * @returns {Variable|null} The found variable or null.
390
+ */
391
+ function findVariable(initialScope, nameOrNode) {
392
+ let name = "";
393
+ /** @type {Scope|null} */
394
+ let scope = initialScope;
395
+
396
+ if (typeof nameOrNode === "string") {
397
+ name = nameOrNode;
398
+ } else {
399
+ name = nameOrNode.name;
400
+ scope = getInnermostScope(scope, nameOrNode);
401
+ }
402
+
403
+ while (scope != null) {
404
+ const variable = scope.set.get(name);
405
+ if (variable != null) {
406
+ return variable
407
+ }
408
+ scope = scope.upper;
409
+ }
410
+
411
+ return null
412
+ }
413
+
414
+ /** @typedef {import("eslint").AST.Token} Token */
415
+ /** @typedef {import("estree").Comment} Comment */
416
+ /** @typedef {import("./types.mjs").ArrowToken} ArrowToken */
417
+ /** @typedef {import("./types.mjs").CommaToken} CommaToken */
418
+ /** @typedef {import("./types.mjs").SemicolonToken} SemicolonToken */
419
+ /** @typedef {import("./types.mjs").ColonToken} ColonToken */
420
+ /** @typedef {import("./types.mjs").OpeningParenToken} OpeningParenToken */
421
+ /** @typedef {import("./types.mjs").ClosingParenToken} ClosingParenToken */
422
+ /** @typedef {import("./types.mjs").OpeningBracketToken} OpeningBracketToken */
423
+ /** @typedef {import("./types.mjs").ClosingBracketToken} ClosingBracketToken */
424
+ /** @typedef {import("./types.mjs").OpeningBraceToken} OpeningBraceToken */
425
+ /** @typedef {import("./types.mjs").ClosingBraceToken} ClosingBraceToken */
426
+ /**
427
+ * @template {string} Value
428
+ * @typedef {import("./types.mjs").PunctuatorToken<Value>} PunctuatorToken
429
+ */
430
+
431
+ /** @typedef {Comment | Token} CommentOrToken */
432
+
433
+ /**
434
+ * Creates the negate function of the given function.
435
+ * @param {function(CommentOrToken):boolean} f - The function to negate.
436
+ * @returns {function(CommentOrToken):boolean} Negated function.
437
+ */
438
+ function negate(f) {
439
+ return (token) => !f(token)
440
+ }
441
+
442
+ /**
443
+ * Checks if the given token is a PunctuatorToken with the given value
444
+ * @template {string} Value
445
+ * @param {CommentOrToken} token - The token to check.
446
+ * @param {Value} value - The value to check.
447
+ * @returns {token is PunctuatorToken<Value>} `true` if the token is a PunctuatorToken with the given value.
448
+ */
449
+ function isPunctuatorTokenWithValue(token, value) {
450
+ return token.type === "Punctuator" && token.value === value
451
+ }
452
+
453
+ /**
454
+ * Checks if the given token is an arrow token or not.
455
+ * @param {CommentOrToken} token - The token to check.
456
+ * @returns {token is ArrowToken} `true` if the token is an arrow token.
457
+ */
458
+ function isArrowToken(token) {
459
+ return isPunctuatorTokenWithValue(token, "=>")
460
+ }
461
+
462
+ /**
463
+ * Checks if the given token is a comma token or not.
464
+ * @param {CommentOrToken} token - The token to check.
465
+ * @returns {token is CommaToken} `true` if the token is a comma token.
466
+ */
467
+ function isCommaToken(token) {
468
+ return isPunctuatorTokenWithValue(token, ",")
469
+ }
470
+
471
+ /**
472
+ * Checks if the given token is a semicolon token or not.
473
+ * @param {CommentOrToken} token - The token to check.
474
+ * @returns {token is SemicolonToken} `true` if the token is a semicolon token.
475
+ */
476
+ function isSemicolonToken(token) {
477
+ return isPunctuatorTokenWithValue(token, ";")
478
+ }
479
+
480
+ /**
481
+ * Checks if the given token is a colon token or not.
482
+ * @param {CommentOrToken} token - The token to check.
483
+ * @returns {token is ColonToken} `true` if the token is a colon token.
484
+ */
485
+ function isColonToken(token) {
486
+ return isPunctuatorTokenWithValue(token, ":")
487
+ }
488
+
489
+ /**
490
+ * Checks if the given token is an opening parenthesis token or not.
491
+ * @param {CommentOrToken} token - The token to check.
492
+ * @returns {token is OpeningParenToken} `true` if the token is an opening parenthesis token.
493
+ */
494
+ function isOpeningParenToken(token) {
495
+ return isPunctuatorTokenWithValue(token, "(")
496
+ }
497
+
498
+ /**
499
+ * Checks if the given token is a closing parenthesis token or not.
500
+ * @param {CommentOrToken} token - The token to check.
501
+ * @returns {token is ClosingParenToken} `true` if the token is a closing parenthesis token.
502
+ */
503
+ function isClosingParenToken(token) {
504
+ return isPunctuatorTokenWithValue(token, ")")
505
+ }
506
+
507
+ /**
508
+ * Checks if the given token is an opening square bracket token or not.
509
+ * @param {CommentOrToken} token - The token to check.
510
+ * @returns {token is OpeningBracketToken} `true` if the token is an opening square bracket token.
511
+ */
512
+ function isOpeningBracketToken(token) {
513
+ return isPunctuatorTokenWithValue(token, "[")
514
+ }
515
+
516
+ /**
517
+ * Checks if the given token is a closing square bracket token or not.
518
+ * @param {CommentOrToken} token - The token to check.
519
+ * @returns {token is ClosingBracketToken} `true` if the token is a closing square bracket token.
520
+ */
521
+ function isClosingBracketToken(token) {
522
+ return isPunctuatorTokenWithValue(token, "]")
523
+ }
524
+
525
+ /**
526
+ * Checks if the given token is an opening brace token or not.
527
+ * @param {CommentOrToken} token - The token to check.
528
+ * @returns {token is OpeningBraceToken} `true` if the token is an opening brace token.
529
+ */
530
+ function isOpeningBraceToken(token) {
531
+ return isPunctuatorTokenWithValue(token, "{")
532
+ }
533
+
534
+ /**
535
+ * Checks if the given token is a closing brace token or not.
536
+ * @param {CommentOrToken} token - The token to check.
537
+ * @returns {token is ClosingBraceToken} `true` if the token is a closing brace token.
538
+ */
539
+ function isClosingBraceToken(token) {
540
+ return isPunctuatorTokenWithValue(token, "}")
541
+ }
542
+
543
+ /**
544
+ * Checks if the given token is a comment token or not.
545
+ * @param {CommentOrToken} token - The token to check.
546
+ * @returns {token is Comment} `true` if the token is a comment token.
547
+ */
548
+ function isCommentToken(token) {
549
+ return ["Block", "Line", "Shebang"].includes(token.type)
550
+ }
551
+ const isNotSemicolonToken = negate(isSemicolonToken);
552
+
553
+ /** @typedef {import("eslint").Rule.Node} RuleNode */
554
+ /** @typedef {import("eslint").SourceCode} SourceCode */
555
+ /** @typedef {import("eslint").AST.Token} Token */
556
+ /** @typedef {import("estree").Function} FunctionNode */
557
+ /** @typedef {import("estree").FunctionDeclaration} FunctionDeclaration */
558
+ /** @typedef {import("estree").FunctionExpression} FunctionExpression */
559
+ /** @typedef {import("estree").SourceLocation} SourceLocation */
560
+ /** @typedef {import("estree").Position} Position */
561
+
562
+ /**
563
+ * Get the `(` token of the given function node.
564
+ * @param {FunctionExpression | FunctionDeclaration} node - The function node to get.
565
+ * @param {SourceCode} sourceCode - The source code object to get tokens.
566
+ * @returns {Token} `(` token.
567
+ */
568
+ function getOpeningParenOfParams(node, sourceCode) {
569
+ return node.id
570
+ ? /** @type {Token} */ (
571
+ sourceCode.getTokenAfter(node.id, isOpeningParenToken)
572
+ )
573
+ : /** @type {Token} */ (
574
+ sourceCode.getFirstToken(node, isOpeningParenToken)
575
+ )
576
+ }
577
+
578
+ /**
579
+ * Get the location of the given function node for reporting.
580
+ * @param {FunctionNode} node - The function node to get.
581
+ * @param {SourceCode} sourceCode - The source code object to get tokens.
582
+ * @returns {SourceLocation|null} The location of the function node for reporting.
583
+ */
584
+ function getFunctionHeadLocation(node, sourceCode) {
585
+ const parent = /** @type {RuleNode} */ (node).parent;
586
+
587
+ /** @type {Position|null} */
588
+ let start = null;
589
+ /** @type {Position|null} */
590
+ let end = null;
591
+
592
+ if (node.type === "ArrowFunctionExpression") {
593
+ const arrowToken = /** @type {Token} */ (
594
+ sourceCode.getTokenBefore(node.body, isArrowToken)
595
+ );
596
+
597
+ start = arrowToken.loc.start;
598
+ end = arrowToken.loc.end;
599
+ } else if (
600
+ parent &&
601
+ (parent.type === "Property" ||
602
+ parent.type === "MethodDefinition" ||
603
+ parent.type === "PropertyDefinition")
604
+ ) {
605
+ start = /** @type {SourceLocation} */ (parent.loc).start;
606
+ end = getOpeningParenOfParams(node, sourceCode).loc.start;
607
+ } else {
608
+ start = /** @type {SourceLocation} */ (node.loc).start;
609
+ end = getOpeningParenOfParams(node, sourceCode).loc.start;
610
+ }
611
+
612
+ return {
613
+ start: { ...start },
614
+ end: { ...end },
615
+ }
616
+ }
617
+
618
+ /* globals globalThis, global, self, window */
619
+ /** @typedef {import("./types.mjs").StaticValue} StaticValue */
620
+ /** @typedef {import("eslint").Scope.Scope} Scope */
621
+ /** @typedef {import("eslint").Scope.Variable} Variable */
622
+ /** @typedef {import("estree").Node} Node */
623
+ /** @typedef {import("@typescript-eslint/types").TSESTree.Node} TSESTreeNode */
624
+ /** @typedef {import("@typescript-eslint/types").TSESTree.AST_NODE_TYPES} TSESTreeNodeTypes */
625
+ /** @typedef {import("@typescript-eslint/types").TSESTree.MemberExpression} MemberExpression */
626
+ /** @typedef {import("@typescript-eslint/types").TSESTree.Property} Property */
627
+ /** @typedef {import("@typescript-eslint/types").TSESTree.RegExpLiteral} RegExpLiteral */
628
+ /** @typedef {import("@typescript-eslint/types").TSESTree.BigIntLiteral} BigIntLiteral */
629
+ /** @typedef {import("@typescript-eslint/types").TSESTree.Literal} Literal */
630
+
631
+ const globalObject =
632
+ typeof globalThis !== "undefined"
633
+ ? globalThis
634
+ : // @ts-ignore
635
+ typeof self !== "undefined"
636
+ ? // @ts-ignore
637
+ self
638
+ : // @ts-ignore
639
+ typeof window !== "undefined"
640
+ ? // @ts-ignore
641
+ window
642
+ : typeof global !== "undefined"
643
+ ? global
644
+ : {};
645
+
646
+ const builtinNames = Object.freeze(
647
+ new Set([
648
+ "Array",
649
+ "ArrayBuffer",
650
+ "BigInt",
651
+ "BigInt64Array",
652
+ "BigUint64Array",
653
+ "Boolean",
654
+ "DataView",
655
+ "Date",
656
+ "decodeURI",
657
+ "decodeURIComponent",
658
+ "encodeURI",
659
+ "encodeURIComponent",
660
+ "escape",
661
+ "Float32Array",
662
+ "Float64Array",
663
+ "Function",
664
+ "Infinity",
665
+ "Int16Array",
666
+ "Int32Array",
667
+ "Int8Array",
668
+ "isFinite",
669
+ "isNaN",
670
+ "isPrototypeOf",
671
+ "JSON",
672
+ "Map",
673
+ "Math",
674
+ "NaN",
675
+ "Number",
676
+ "Object",
677
+ "parseFloat",
678
+ "parseInt",
679
+ "Promise",
680
+ "Proxy",
681
+ "Reflect",
682
+ "RegExp",
683
+ "Set",
684
+ "String",
685
+ "Symbol",
686
+ "Uint16Array",
687
+ "Uint32Array",
688
+ "Uint8Array",
689
+ "Uint8ClampedArray",
690
+ "undefined",
691
+ "unescape",
692
+ "WeakMap",
693
+ "WeakSet",
694
+ ]),
695
+ );
696
+ const callAllowed = new Set(
697
+ [
698
+ Array.isArray,
699
+ Array.of,
700
+ Array.prototype.at,
701
+ Array.prototype.concat,
702
+ Array.prototype.entries,
703
+ Array.prototype.every,
704
+ Array.prototype.filter,
705
+ Array.prototype.find,
706
+ Array.prototype.findIndex,
707
+ Array.prototype.flat,
708
+ Array.prototype.includes,
709
+ Array.prototype.indexOf,
710
+ Array.prototype.join,
711
+ Array.prototype.keys,
712
+ Array.prototype.lastIndexOf,
713
+ Array.prototype.slice,
714
+ Array.prototype.some,
715
+ Array.prototype.toString,
716
+ Array.prototype.values,
717
+ typeof BigInt === "function" ? BigInt : undefined,
718
+ Boolean,
719
+ Date,
720
+ Date.parse,
721
+ decodeURI,
722
+ decodeURIComponent,
723
+ encodeURI,
724
+ encodeURIComponent,
725
+ escape,
726
+ isFinite,
727
+ isNaN,
728
+ // @ts-ignore
729
+ isPrototypeOf,
730
+ Map,
731
+ Map.prototype.entries,
732
+ Map.prototype.get,
733
+ Map.prototype.has,
734
+ Map.prototype.keys,
735
+ Map.prototype.values,
736
+ .../** @type {(keyof typeof Math)[]} */ (
737
+ Object.getOwnPropertyNames(Math)
738
+ )
739
+ .filter((k) => k !== "random")
740
+ .map((k) => Math[k])
741
+ .filter((f) => typeof f === "function"),
742
+ Number,
743
+ Number.isFinite,
744
+ Number.isNaN,
745
+ Number.parseFloat,
746
+ Number.parseInt,
747
+ Number.prototype.toExponential,
748
+ Number.prototype.toFixed,
749
+ Number.prototype.toPrecision,
750
+ Number.prototype.toString,
751
+ Object,
752
+ Object.entries,
753
+ Object.is,
754
+ Object.isExtensible,
755
+ Object.isFrozen,
756
+ Object.isSealed,
757
+ Object.keys,
758
+ Object.values,
759
+ parseFloat,
760
+ parseInt,
761
+ RegExp,
762
+ Set,
763
+ Set.prototype.entries,
764
+ Set.prototype.has,
765
+ Set.prototype.keys,
766
+ Set.prototype.values,
767
+ String,
768
+ String.fromCharCode,
769
+ String.fromCodePoint,
770
+ String.raw,
771
+ String.prototype.at,
772
+ String.prototype.charAt,
773
+ String.prototype.charCodeAt,
774
+ String.prototype.codePointAt,
775
+ String.prototype.concat,
776
+ String.prototype.endsWith,
777
+ String.prototype.includes,
778
+ String.prototype.indexOf,
779
+ String.prototype.lastIndexOf,
780
+ String.prototype.normalize,
781
+ String.prototype.padEnd,
782
+ String.prototype.padStart,
783
+ String.prototype.slice,
784
+ String.prototype.startsWith,
785
+ String.prototype.substr,
786
+ String.prototype.substring,
787
+ String.prototype.toLowerCase,
788
+ String.prototype.toString,
789
+ String.prototype.toUpperCase,
790
+ String.prototype.trim,
791
+ String.prototype.trimEnd,
792
+ String.prototype.trimLeft,
793
+ String.prototype.trimRight,
794
+ String.prototype.trimStart,
795
+ Symbol.for,
796
+ Symbol.keyFor,
797
+ unescape,
798
+ ].filter((f) => typeof f === "function"),
799
+ );
800
+ const callPassThrough = new Set([
801
+ Object.freeze,
802
+ Object.preventExtensions,
803
+ Object.seal,
804
+ ]);
805
+
806
+ /** @type {ReadonlyArray<readonly [Function, ReadonlySet<string>]>} */
807
+ const getterAllowed = [
808
+ [Map, new Set(["size"])],
809
+ [
810
+ RegExp,
811
+ new Set([
812
+ "dotAll",
813
+ "flags",
814
+ "global",
815
+ "hasIndices",
816
+ "ignoreCase",
817
+ "multiline",
818
+ "source",
819
+ "sticky",
820
+ "unicode",
821
+ ]),
822
+ ],
823
+ [Set, new Set(["size"])],
824
+ ];
825
+
826
+ /**
827
+ * Get the property descriptor.
828
+ * @param {object} object The object to get.
829
+ * @param {string|number|symbol} name The property name to get.
830
+ */
831
+ function getPropertyDescriptor(object, name) {
832
+ let x = object;
833
+ while ((typeof x === "object" || typeof x === "function") && x !== null) {
834
+ const d = Object.getOwnPropertyDescriptor(x, name);
835
+ if (d) {
836
+ return d
837
+ }
838
+ x = Object.getPrototypeOf(x);
839
+ }
840
+ return null
841
+ }
842
+
843
+ /**
844
+ * Check if a property is getter or not.
845
+ * @param {object} object The object to check.
846
+ * @param {string|number|symbol} name The property name to check.
847
+ */
848
+ function isGetter(object, name) {
849
+ const d = getPropertyDescriptor(object, name);
850
+ return d != null && d.get != null
851
+ }
852
+
853
+ /**
854
+ * Get the element values of a given node list.
855
+ * @param {(Node|TSESTreeNode|null)[]} nodeList The node list to get values.
856
+ * @param {Scope|undefined|null} initialScope The initial scope to find variables.
857
+ * @returns {any[]|null} The value list if all nodes are constant. Otherwise, null.
858
+ */
859
+ function getElementValues(nodeList, initialScope) {
860
+ const valueList = [];
861
+
862
+ for (let i = 0; i < nodeList.length; ++i) {
863
+ const elementNode = nodeList[i];
864
+
865
+ if (elementNode == null) {
866
+ valueList.length = i + 1;
867
+ } else if (elementNode.type === "SpreadElement") {
868
+ const argument = getStaticValueR(elementNode.argument, initialScope);
869
+ if (argument == null) {
870
+ return null
871
+ }
872
+ valueList.push(.../** @type {Iterable<any>} */ (argument.value));
873
+ } else {
874
+ const element = getStaticValueR(elementNode, initialScope);
875
+ if (element == null) {
876
+ return null
877
+ }
878
+ valueList.push(element.value);
879
+ }
880
+ }
881
+
882
+ return valueList
883
+ }
884
+
885
+ /**
886
+ * Checks if a variable is a built-in global.
887
+ * @param {Variable|null} variable The variable to check.
888
+ * @returns {variable is Variable & {defs:[]}}
889
+ */
890
+ function isBuiltinGlobal(variable) {
891
+ return (
892
+ variable != null &&
893
+ variable.defs.length === 0 &&
894
+ builtinNames.has(variable.name) &&
895
+ variable.name in globalObject
896
+ )
897
+ }
898
+
899
+ /**
900
+ * Checks if a variable can be considered as a constant.
901
+ * @param {Variable} variable
902
+ * @returns {variable is Variable & {defs: [import("eslint").Scope.Definition & { type: "Variable" }]}} True if the variable can be considered as a constant.
903
+ */
904
+ function canBeConsideredConst(variable) {
905
+ if (variable.defs.length !== 1) {
906
+ return false
907
+ }
908
+ const def = variable.defs[0];
909
+ return Boolean(
910
+ def.parent &&
911
+ def.type === "Variable" &&
912
+ (def.parent.kind === "const" || isEffectivelyConst(variable)),
913
+ )
914
+ }
915
+
916
+ /**
917
+ * Returns whether the given variable is never written to after initialization.
918
+ * @param {Variable} variable
919
+ * @returns {boolean}
920
+ */
921
+ function isEffectivelyConst(variable) {
922
+ const refs = variable.references;
923
+
924
+ const inits = refs.filter((r) => r.init).length;
925
+ const reads = refs.filter((r) => r.isReadOnly()).length;
926
+ if (inits === 1 && reads + inits === refs.length) {
927
+ // there is only one init and all other references only read
928
+ return true
929
+ }
930
+ return false
931
+ }
932
+
933
+ /**
934
+ * Checks if a variable has mutation in its property.
935
+ * @param {Variable} variable The variable to check.
936
+ * @param {Scope|null} initialScope The scope to start finding variable. Optional. If the node is a computed property node and this scope was given, this checks the computed property name by the `getStringIfConstant` function with the scope, and returns the value of it.
937
+ * @returns {boolean} True if the variable has mutation in its property.
938
+ */
939
+ function hasMutationInProperty(variable, initialScope) {
940
+ for (const ref of variable.references) {
941
+ let node = /** @type {TSESTreeNode} */ (ref.identifier);
942
+ while (node && node.parent && node.parent.type === "MemberExpression") {
943
+ node = node.parent;
944
+ }
945
+ if (!node || !node.parent) {
946
+ continue
947
+ }
948
+ if (
949
+ (node.parent.type === "AssignmentExpression" &&
950
+ node.parent.left === node) ||
951
+ (node.parent.type === "UpdateExpression" &&
952
+ node.parent.argument === node)
953
+ ) {
954
+ // This is a mutation.
955
+ return true
956
+ }
957
+ if (
958
+ node.parent.type === "CallExpression" &&
959
+ node.parent.callee === node &&
960
+ node.type === "MemberExpression"
961
+ ) {
962
+ const methodName = getStaticPropertyNameValue(node, initialScope);
963
+ if (isNameOfMutationArrayMethod(methodName)) {
964
+ // This is a mutation.
965
+ return true
966
+ }
967
+ }
968
+ }
969
+ return false
970
+
971
+ /**
972
+ * Checks if a method name is one of the mutation array methods.
973
+ * @param {StaticValue|null} methodName The method name to check.
974
+ * @returns {boolean} True if the method name is a mutation array method.
975
+ */
976
+ function isNameOfMutationArrayMethod(methodName) {
977
+ if (methodName == null || methodName.value == null) {
978
+ return false
979
+ }
980
+ const name = methodName.value;
981
+ return (
982
+ name === "copyWithin" ||
983
+ name === "fill" ||
984
+ name === "pop" ||
985
+ name === "push" ||
986
+ name === "reverse" ||
987
+ name === "shift" ||
988
+ name === "sort" ||
989
+ name === "splice" ||
990
+ name === "unshift"
991
+ )
992
+ }
993
+ }
994
+
995
+ /**
996
+ * @template {TSESTreeNodeTypes} T
997
+ * @callback VisitorCallback
998
+ * @param {TSESTreeNode & { type: T }} node
999
+ * @param {Scope|undefined|null} initialScope
1000
+ * @returns {StaticValue | null}
1001
+ */
1002
+ /**
1003
+ * @typedef { { [K in TSESTreeNodeTypes]?: VisitorCallback<K> } } Operations
1004
+ */
1005
+ /**
1006
+ * @type {Operations}
1007
+ */
1008
+ const operations = Object.freeze({
1009
+ ArrayExpression(node, initialScope) {
1010
+ const elements = getElementValues(node.elements, initialScope);
1011
+ return elements != null ? { value: elements } : null
1012
+ },
1013
+
1014
+ AssignmentExpression(node, initialScope) {
1015
+ if (node.operator === "=") {
1016
+ return getStaticValueR(node.right, initialScope)
1017
+ }
1018
+ return null
1019
+ },
1020
+
1021
+ //eslint-disable-next-line complexity
1022
+ BinaryExpression(node, initialScope) {
1023
+ if (node.operator === "in" || node.operator === "instanceof") {
1024
+ // Not supported.
1025
+ return null
1026
+ }
1027
+
1028
+ const left = getStaticValueR(node.left, initialScope);
1029
+ const right = getStaticValueR(node.right, initialScope);
1030
+ if (left != null && right != null) {
1031
+ switch (node.operator) {
1032
+ case "==":
1033
+ return { value: left.value == right.value } //eslint-disable-line eqeqeq
1034
+ case "!=":
1035
+ return { value: left.value != right.value } //eslint-disable-line eqeqeq
1036
+ case "===":
1037
+ return { value: left.value === right.value }
1038
+ case "!==":
1039
+ return { value: left.value !== right.value }
1040
+ case "<":
1041
+ return {
1042
+ value:
1043
+ /** @type {any} */ (left.value) <
1044
+ /** @type {any} */ (right.value),
1045
+ }
1046
+ case "<=":
1047
+ return {
1048
+ value:
1049
+ /** @type {any} */ (left.value) <=
1050
+ /** @type {any} */ (right.value),
1051
+ }
1052
+ case ">":
1053
+ return {
1054
+ value:
1055
+ /** @type {any} */ (left.value) >
1056
+ /** @type {any} */ (right.value),
1057
+ }
1058
+ case ">=":
1059
+ return {
1060
+ value:
1061
+ /** @type {any} */ (left.value) >=
1062
+ /** @type {any} */ (right.value),
1063
+ }
1064
+ case "<<":
1065
+ return {
1066
+ value:
1067
+ /** @type {any} */ (left.value) <<
1068
+ /** @type {any} */ (right.value),
1069
+ }
1070
+ case ">>":
1071
+ return {
1072
+ value:
1073
+ /** @type {any} */ (left.value) >>
1074
+ /** @type {any} */ (right.value),
1075
+ }
1076
+ case ">>>":
1077
+ return {
1078
+ value:
1079
+ /** @type {any} */ (left.value) >>>
1080
+ /** @type {any} */ (right.value),
1081
+ }
1082
+ case "+":
1083
+ return {
1084
+ value:
1085
+ /** @type {any} */ (left.value) +
1086
+ /** @type {any} */ (right.value),
1087
+ }
1088
+ case "-":
1089
+ return {
1090
+ value:
1091
+ /** @type {any} */ (left.value) -
1092
+ /** @type {any} */ (right.value),
1093
+ }
1094
+ case "*":
1095
+ return {
1096
+ value:
1097
+ /** @type {any} */ (left.value) *
1098
+ /** @type {any} */ (right.value),
1099
+ }
1100
+ case "/":
1101
+ return {
1102
+ value:
1103
+ /** @type {any} */ (left.value) /
1104
+ /** @type {any} */ (right.value),
1105
+ }
1106
+ case "%":
1107
+ return {
1108
+ value:
1109
+ /** @type {any} */ (left.value) %
1110
+ /** @type {any} */ (right.value),
1111
+ }
1112
+ case "**":
1113
+ return {
1114
+ value:
1115
+ /** @type {any} */ (left.value) **
1116
+ /** @type {any} */ (right.value),
1117
+ }
1118
+ case "|":
1119
+ return {
1120
+ value:
1121
+ /** @type {any} */ (left.value) |
1122
+ /** @type {any} */ (right.value),
1123
+ }
1124
+ case "^":
1125
+ return {
1126
+ value:
1127
+ /** @type {any} */ (left.value) ^
1128
+ /** @type {any} */ (right.value),
1129
+ }
1130
+ case "&":
1131
+ return {
1132
+ value:
1133
+ /** @type {any} */ (left.value) &
1134
+ /** @type {any} */ (right.value),
1135
+ }
1136
+
1137
+ // no default
1138
+ }
1139
+ }
1140
+
1141
+ return null
1142
+ },
1143
+
1144
+ CallExpression(node, initialScope) {
1145
+ const calleeNode = node.callee;
1146
+ const args = getElementValues(node.arguments, initialScope);
1147
+
1148
+ if (args != null) {
1149
+ if (calleeNode.type === "MemberExpression") {
1150
+ if (calleeNode.property.type === "PrivateIdentifier") {
1151
+ return null
1152
+ }
1153
+ const object = getStaticValueR(calleeNode.object, initialScope);
1154
+ if (object != null) {
1155
+ if (
1156
+ object.value == null &&
1157
+ (object.optional || node.optional)
1158
+ ) {
1159
+ return { value: undefined, optional: true }
1160
+ }
1161
+ const property = getStaticPropertyNameValue(
1162
+ calleeNode,
1163
+ initialScope,
1164
+ );
1165
+
1166
+ if (property != null) {
1167
+ const receiver =
1168
+ /** @type {Record<PropertyKey, (...args: any[]) => any>} */ (
1169
+ object.value
1170
+ );
1171
+ const methodName = /** @type {PropertyKey} */ (
1172
+ property.value
1173
+ );
1174
+ if (callAllowed.has(receiver[methodName])) {
1175
+ return {
1176
+ value: receiver[methodName](...args),
1177
+ }
1178
+ }
1179
+ if (callPassThrough.has(receiver[methodName])) {
1180
+ return { value: args[0] }
1181
+ }
1182
+ }
1183
+ }
1184
+ } else {
1185
+ const callee = getStaticValueR(calleeNode, initialScope);
1186
+ if (callee != null) {
1187
+ if (callee.value == null && node.optional) {
1188
+ return { value: undefined, optional: true }
1189
+ }
1190
+ const func = /** @type {(...args: any[]) => any} */ (
1191
+ callee.value
1192
+ );
1193
+ if (callAllowed.has(func)) {
1194
+ return { value: func(...args) }
1195
+ }
1196
+ if (callPassThrough.has(func)) {
1197
+ return { value: args[0] }
1198
+ }
1199
+ }
1200
+ }
1201
+ }
1202
+
1203
+ return null
1204
+ },
1205
+
1206
+ ConditionalExpression(node, initialScope) {
1207
+ const test = getStaticValueR(node.test, initialScope);
1208
+ if (test != null) {
1209
+ return test.value
1210
+ ? getStaticValueR(node.consequent, initialScope)
1211
+ : getStaticValueR(node.alternate, initialScope)
1212
+ }
1213
+ return null
1214
+ },
1215
+
1216
+ ExpressionStatement(node, initialScope) {
1217
+ return getStaticValueR(node.expression, initialScope)
1218
+ },
1219
+
1220
+ Identifier(node, initialScope) {
1221
+ if (initialScope != null) {
1222
+ const variable = findVariable(initialScope, node);
1223
+
1224
+ if (variable != null) {
1225
+ // Built-in globals.
1226
+ if (isBuiltinGlobal(variable)) {
1227
+ return { value: globalObject[variable.name] }
1228
+ }
1229
+
1230
+ // Constants.
1231
+ if (canBeConsideredConst(variable)) {
1232
+ const def = variable.defs[0];
1233
+ if (
1234
+ // TODO(mysticatea): don't support destructuring here.
1235
+ def.node.id.type === "Identifier"
1236
+ ) {
1237
+ const init = getStaticValueR(
1238
+ def.node.init,
1239
+ initialScope,
1240
+ );
1241
+ if (
1242
+ init &&
1243
+ typeof init.value === "object" &&
1244
+ init.value !== null
1245
+ ) {
1246
+ if (hasMutationInProperty(variable, initialScope)) {
1247
+ // This variable has mutation in its property.
1248
+ return null
1249
+ }
1250
+ }
1251
+ return init
1252
+ }
1253
+ }
1254
+ }
1255
+ }
1256
+ return null
1257
+ },
1258
+
1259
+ Literal(node) {
1260
+ const literal =
1261
+ /** @type {Partial<Literal> & Partial<RegExpLiteral> & Partial<BigIntLiteral>} */ (
1262
+ node
1263
+ );
1264
+ //istanbul ignore if : this is implementation-specific behavior.
1265
+ if (
1266
+ (literal.regex != null || literal.bigint != null) &&
1267
+ literal.value == null
1268
+ ) {
1269
+ // It was a RegExp/BigInt literal, but Node.js didn't support it.
1270
+ return null
1271
+ }
1272
+ return { value: literal.value }
1273
+ },
1274
+
1275
+ LogicalExpression(node, initialScope) {
1276
+ const left = getStaticValueR(node.left, initialScope);
1277
+ if (left != null) {
1278
+ if (
1279
+ (node.operator === "||" && Boolean(left.value) === true) ||
1280
+ (node.operator === "&&" && Boolean(left.value) === false) ||
1281
+ (node.operator === "??" && left.value != null)
1282
+ ) {
1283
+ return left
1284
+ }
1285
+
1286
+ const right = getStaticValueR(node.right, initialScope);
1287
+ if (right != null) {
1288
+ return right
1289
+ }
1290
+ }
1291
+
1292
+ return null
1293
+ },
1294
+
1295
+ MemberExpression(node, initialScope) {
1296
+ if (node.property.type === "PrivateIdentifier") {
1297
+ return null
1298
+ }
1299
+ const object = getStaticValueR(node.object, initialScope);
1300
+ if (object != null) {
1301
+ if (object.value == null && (object.optional || node.optional)) {
1302
+ return { value: undefined, optional: true }
1303
+ }
1304
+ const property = getStaticPropertyNameValue(node, initialScope);
1305
+
1306
+ if (property != null) {
1307
+ if (
1308
+ !isGetter(
1309
+ /** @type {object} */ (object.value),
1310
+ /** @type {PropertyKey} */ (property.value),
1311
+ )
1312
+ ) {
1313
+ return {
1314
+ value: /** @type {Record<PropertyKey, unknown>} */ (
1315
+ object.value
1316
+ )[/** @type {PropertyKey} */ (property.value)],
1317
+ }
1318
+ }
1319
+
1320
+ for (const [classFn, allowed] of getterAllowed) {
1321
+ if (
1322
+ object.value instanceof classFn &&
1323
+ allowed.has(/** @type {string} */ (property.value))
1324
+ ) {
1325
+ return {
1326
+ value: /** @type {Record<PropertyKey, unknown>} */ (
1327
+ object.value
1328
+ )[/** @type {PropertyKey} */ (property.value)],
1329
+ }
1330
+ }
1331
+ }
1332
+ }
1333
+ }
1334
+ return null
1335
+ },
1336
+
1337
+ ChainExpression(node, initialScope) {
1338
+ const expression = getStaticValueR(node.expression, initialScope);
1339
+ if (expression != null) {
1340
+ return { value: expression.value }
1341
+ }
1342
+ return null
1343
+ },
1344
+
1345
+ NewExpression(node, initialScope) {
1346
+ const callee = getStaticValueR(node.callee, initialScope);
1347
+ const args = getElementValues(node.arguments, initialScope);
1348
+
1349
+ if (callee != null && args != null) {
1350
+ const Func = /** @type {new (...args: any[]) => any} */ (
1351
+ callee.value
1352
+ );
1353
+ if (callAllowed.has(Func)) {
1354
+ return { value: new Func(...args) }
1355
+ }
1356
+ }
1357
+
1358
+ return null
1359
+ },
1360
+
1361
+ ObjectExpression(node, initialScope) {
1362
+ /** @type {Record<PropertyKey, unknown>} */
1363
+ const object = {};
1364
+
1365
+ for (const propertyNode of node.properties) {
1366
+ if (propertyNode.type === "Property") {
1367
+ if (propertyNode.kind !== "init") {
1368
+ return null
1369
+ }
1370
+ const key = getStaticPropertyNameValue(
1371
+ propertyNode,
1372
+ initialScope,
1373
+ );
1374
+ const value = getStaticValueR(propertyNode.value, initialScope);
1375
+ if (key == null || value == null) {
1376
+ return null
1377
+ }
1378
+ object[/** @type {PropertyKey} */ (key.value)] = value.value;
1379
+ } else if (
1380
+ propertyNode.type === "SpreadElement" ||
1381
+ // @ts-expect-error -- Backward compatibility
1382
+ propertyNode.type === "ExperimentalSpreadProperty"
1383
+ ) {
1384
+ const argument = getStaticValueR(
1385
+ propertyNode.argument,
1386
+ initialScope,
1387
+ );
1388
+ if (argument == null) {
1389
+ return null
1390
+ }
1391
+ Object.assign(object, argument.value);
1392
+ } else {
1393
+ return null
1394
+ }
1395
+ }
1396
+
1397
+ return { value: object }
1398
+ },
1399
+
1400
+ SequenceExpression(node, initialScope) {
1401
+ const last = node.expressions[node.expressions.length - 1];
1402
+ return getStaticValueR(last, initialScope)
1403
+ },
1404
+
1405
+ TaggedTemplateExpression(node, initialScope) {
1406
+ const tag = getStaticValueR(node.tag, initialScope);
1407
+ const expressions = getElementValues(
1408
+ node.quasi.expressions,
1409
+ initialScope,
1410
+ );
1411
+
1412
+ if (tag != null && expressions != null) {
1413
+ const func = /** @type {(...args: any[]) => any} */ (tag.value);
1414
+ /** @type {any[] & { raw?: string[] }} */
1415
+ const strings = node.quasi.quasis.map((q) => q.value.cooked);
1416
+ strings.raw = node.quasi.quasis.map((q) => q.value.raw);
1417
+
1418
+ if (func === String.raw) {
1419
+ return { value: func(strings, ...expressions) }
1420
+ }
1421
+ }
1422
+
1423
+ return null
1424
+ },
1425
+
1426
+ TemplateLiteral(node, initialScope) {
1427
+ const expressions = getElementValues(node.expressions, initialScope);
1428
+ if (expressions != null) {
1429
+ let value = node.quasis[0].value.cooked;
1430
+ for (let i = 0; i < expressions.length; ++i) {
1431
+ value += expressions[i];
1432
+ value += /** @type {string} */ (node.quasis[i + 1].value.cooked);
1433
+ }
1434
+ return { value }
1435
+ }
1436
+ return null
1437
+ },
1438
+
1439
+ UnaryExpression(node, initialScope) {
1440
+ if (node.operator === "delete") {
1441
+ // Not supported.
1442
+ return null
1443
+ }
1444
+ if (node.operator === "void") {
1445
+ return { value: undefined }
1446
+ }
1447
+
1448
+ const arg = getStaticValueR(node.argument, initialScope);
1449
+ if (arg != null) {
1450
+ switch (node.operator) {
1451
+ case "-":
1452
+ return { value: -(/** @type {any} */ (arg.value)) }
1453
+ case "+":
1454
+ return { value: +(/** @type {any} */ (arg.value)) } //eslint-disable-line no-implicit-coercion
1455
+ case "!":
1456
+ return { value: !arg.value }
1457
+ case "~":
1458
+ return { value: ~(/** @type {any} */ (arg.value)) }
1459
+ case "typeof":
1460
+ return { value: typeof arg.value }
1461
+
1462
+ // no default
1463
+ }
1464
+ }
1465
+
1466
+ return null
1467
+ },
1468
+ TSAsExpression(node, initialScope) {
1469
+ return getStaticValueR(node.expression, initialScope)
1470
+ },
1471
+ TSSatisfiesExpression(node, initialScope) {
1472
+ return getStaticValueR(node.expression, initialScope)
1473
+ },
1474
+ TSTypeAssertion(node, initialScope) {
1475
+ return getStaticValueR(node.expression, initialScope)
1476
+ },
1477
+ TSNonNullExpression(node, initialScope) {
1478
+ return getStaticValueR(node.expression, initialScope)
1479
+ },
1480
+ TSInstantiationExpression(node, initialScope) {
1481
+ return getStaticValueR(node.expression, initialScope)
1482
+ },
1483
+ });
1484
+
1485
+ /**
1486
+ * Get the value of a given node if it's a static value.
1487
+ * @param {Node|TSESTreeNode|null|undefined} node The node to get.
1488
+ * @param {Scope|undefined|null} initialScope The scope to start finding variable.
1489
+ * @returns {StaticValue|null} The static value of the node, or `null`.
1490
+ */
1491
+ function getStaticValueR(node, initialScope) {
1492
+ if (node != null && Object.hasOwnProperty.call(operations, node.type)) {
1493
+ return /** @type {VisitorCallback<any>} */ (operations[node.type])(
1494
+ /** @type {TSESTreeNode} */ (node),
1495
+ initialScope,
1496
+ )
1497
+ }
1498
+ return null
1499
+ }
1500
+
1501
+ /**
1502
+ * Get the static value of property name from a MemberExpression node or a Property node.
1503
+ * @param {MemberExpression|Property} node The node to get.
1504
+ * @param {Scope|null} [initialScope] The scope to start finding variable. Optional. If the node is a computed property node and this scope was given, this checks the computed property name by the `getStringIfConstant` function with the scope, and returns the value of it.
1505
+ * @returns {StaticValue|null} The static value of the property name of the node, or `null`.
1506
+ */
1507
+ function getStaticPropertyNameValue(node, initialScope) {
1508
+ const nameNode = node.type === "Property" ? node.key : node.property;
1509
+
1510
+ if (node.computed) {
1511
+ return getStaticValueR(nameNode, initialScope)
1512
+ }
1513
+
1514
+ if (nameNode.type === "Identifier") {
1515
+ return { value: nameNode.name }
1516
+ }
1517
+
1518
+ if (nameNode.type === "Literal") {
1519
+ if (/** @type {Partial<BigIntLiteral>} */ (nameNode).bigint) {
1520
+ return { value: /** @type {BigIntLiteral} */ (nameNode).bigint }
1521
+ }
1522
+ return { value: String(nameNode.value) }
1523
+ }
1524
+
1525
+ return null
1526
+ }
1527
+
1528
+ /**
1529
+ * Get the value of a given node if it's a static value.
1530
+ * @param {Node} node The node to get.
1531
+ * @param {Scope|null} [initialScope] The scope to start finding variable. Optional. If this scope was given, this tries to resolve identifier references which are in the given node as much as possible.
1532
+ * @returns {StaticValue | null} The static value of the node, or `null`.
1533
+ */
1534
+ function getStaticValue(node, initialScope = null) {
1535
+ try {
1536
+ return getStaticValueR(node, initialScope)
1537
+ } catch (_error) {
1538
+ return null
1539
+ }
1540
+ }
1541
+
1542
+ /** @typedef {import("eslint").Scope.Scope} Scope */
1543
+ /** @typedef {import("estree").Node} Node */
1544
+ /** @typedef {import("estree").RegExpLiteral} RegExpLiteral */
1545
+ /** @typedef {import("estree").BigIntLiteral} BigIntLiteral */
1546
+ /** @typedef {import("estree").SimpleLiteral} SimpleLiteral */
1547
+
1548
+ /**
1549
+ * Get the value of a given node if it's a literal or a template literal.
1550
+ * @param {Node} node The node to get.
1551
+ * @param {Scope|null} [initialScope] The scope to start finding variable. Optional. If the node is an Identifier node and this scope was given, this checks the variable of the identifier, and returns the value of it if the variable is a constant.
1552
+ * @returns {string|null} The value of the node, or `null`.
1553
+ */
1554
+ function getStringIfConstant(node, initialScope = null) {
1555
+ // Handle the literals that the platform doesn't support natively.
1556
+ if (node && node.type === "Literal" && node.value === null) {
1557
+ const literal =
1558
+ /** @type {Partial<SimpleLiteral> & Partial<RegExpLiteral> & Partial<BigIntLiteral>} */ (
1559
+ node
1560
+ );
1561
+ if (literal.regex) {
1562
+ return `/${literal.regex.pattern}/${literal.regex.flags}`
1563
+ }
1564
+ if (literal.bigint) {
1565
+ return literal.bigint
1566
+ }
1567
+ }
1568
+
1569
+ const evaluated = getStaticValue(node, initialScope);
1570
+
1571
+ if (evaluated) {
1572
+ // `String(Symbol.prototype)` throws error
1573
+ try {
1574
+ return String(evaluated.value)
1575
+ } catch {
1576
+ // No op
1577
+ }
1578
+ }
1579
+
1580
+ return null
1581
+ }
1582
+
1583
+ /** @typedef {import("eslint").Scope.Scope} Scope */
1584
+ /** @typedef {import("estree").MemberExpression} MemberExpression */
1585
+ /** @typedef {import("estree").MethodDefinition} MethodDefinition */
1586
+ /** @typedef {import("estree").Property} Property */
1587
+ /** @typedef {import("estree").PropertyDefinition} PropertyDefinition */
1588
+ /** @typedef {import("estree").Identifier} Identifier */
1589
+
1590
+ /**
1591
+ * Get the property name from a MemberExpression node or a Property node.
1592
+ * @param {MemberExpression | MethodDefinition | Property | PropertyDefinition} node The node to get.
1593
+ * @param {Scope} [initialScope] The scope to start finding variable. Optional. If the node is a computed property node and this scope was given, this checks the computed property name by the `getStringIfConstant` function with the scope, and returns the value of it.
1594
+ * @returns {string|null|undefined} The property name of the node.
1595
+ */
1596
+ function getPropertyName(node, initialScope) {
1597
+ switch (node.type) {
1598
+ case "MemberExpression":
1599
+ if (node.computed) {
1600
+ return getStringIfConstant(node.property, initialScope)
1601
+ }
1602
+ if (node.property.type === "PrivateIdentifier") {
1603
+ return null
1604
+ }
1605
+ return /** @type {Partial<Identifier>} */ (node.property).name
1606
+
1607
+ case "Property":
1608
+ case "MethodDefinition":
1609
+ case "PropertyDefinition":
1610
+ if (node.computed) {
1611
+ return getStringIfConstant(node.key, initialScope)
1612
+ }
1613
+ if (node.key.type === "Literal") {
1614
+ return String(node.key.value)
1615
+ }
1616
+ if (node.key.type === "PrivateIdentifier") {
1617
+ return null
1618
+ }
1619
+ return /** @type {Partial<Identifier>} */ (node.key).name
1620
+ }
1621
+
1622
+ return null
1623
+ }
1624
+
1625
+ /** @typedef {import("eslint").Rule.Node} RuleNode */
1626
+ /** @typedef {import("eslint").SourceCode} SourceCode */
1627
+ /** @typedef {import("estree").Function} FunctionNode */
1628
+ /** @typedef {import("estree").FunctionDeclaration} FunctionDeclaration */
1629
+ /** @typedef {import("estree").FunctionExpression} FunctionExpression */
1630
+ /** @typedef {import("estree").Identifier} Identifier */
1631
+
1632
+ /**
1633
+ * Get the name and kind of the given function node.
1634
+ * @param {FunctionNode} node - The function node to get.
1635
+ * @param {SourceCode} [sourceCode] The source code object to get the code of computed property keys.
1636
+ * @returns {string} The name and kind of the function node.
1637
+ */
1638
+ // eslint-disable-next-line complexity
1639
+ function getFunctionNameWithKind(node, sourceCode) {
1640
+ const parent = /** @type {RuleNode} */ (node).parent;
1641
+
1642
+ if (!parent) {
1643
+ return ""
1644
+ }
1645
+
1646
+ const tokens = [];
1647
+ const isObjectMethod = parent.type === "Property" && parent.value === node;
1648
+ const isClassMethod =
1649
+ parent.type === "MethodDefinition" && parent.value === node;
1650
+ const isClassFieldMethod =
1651
+ parent.type === "PropertyDefinition" && parent.value === node;
1652
+
1653
+ // Modifiers.
1654
+ if (isClassMethod || isClassFieldMethod) {
1655
+ if (parent.static) {
1656
+ tokens.push("static");
1657
+ }
1658
+ if (parent.key.type === "PrivateIdentifier") {
1659
+ tokens.push("private");
1660
+ }
1661
+ }
1662
+ if (node.async) {
1663
+ tokens.push("async");
1664
+ }
1665
+ if (node.generator) {
1666
+ tokens.push("generator");
1667
+ }
1668
+
1669
+ // Kinds.
1670
+ if (isObjectMethod || isClassMethod) {
1671
+ if (parent.kind === "constructor") {
1672
+ return "constructor"
1673
+ }
1674
+ if (parent.kind === "get") {
1675
+ tokens.push("getter");
1676
+ } else if (parent.kind === "set") {
1677
+ tokens.push("setter");
1678
+ } else {
1679
+ tokens.push("method");
1680
+ }
1681
+ } else if (isClassFieldMethod) {
1682
+ tokens.push("method");
1683
+ } else {
1684
+ if (node.type === "ArrowFunctionExpression") {
1685
+ tokens.push("arrow");
1686
+ }
1687
+ tokens.push("function");
1688
+ }
1689
+
1690
+ // Names.
1691
+ if (isObjectMethod || isClassMethod || isClassFieldMethod) {
1692
+ if (parent.key.type === "PrivateIdentifier") {
1693
+ tokens.push(`#${parent.key.name}`);
1694
+ } else {
1695
+ const name = getPropertyName(parent);
1696
+ if (name) {
1697
+ tokens.push(`'${name}'`);
1698
+ } else if (sourceCode) {
1699
+ const keyText = sourceCode.getText(parent.key);
1700
+ if (!keyText.includes("\n")) {
1701
+ tokens.push(`[${keyText}]`);
1702
+ }
1703
+ }
1704
+ }
1705
+ } else if (hasId(node)) {
1706
+ tokens.push(`'${node.id.name}'`);
1707
+ } else if (
1708
+ parent.type === "VariableDeclarator" &&
1709
+ parent.id &&
1710
+ parent.id.type === "Identifier"
1711
+ ) {
1712
+ tokens.push(`'${parent.id.name}'`);
1713
+ } else if (
1714
+ (parent.type === "AssignmentExpression" ||
1715
+ parent.type === "AssignmentPattern") &&
1716
+ parent.left &&
1717
+ parent.left.type === "Identifier"
1718
+ ) {
1719
+ tokens.push(`'${parent.left.name}'`);
1720
+ } else if (
1721
+ parent.type === "ExportDefaultDeclaration" &&
1722
+ parent.declaration === node
1723
+ ) {
1724
+ tokens.push("'default'");
1725
+ }
1726
+
1727
+ return tokens.join(" ")
1728
+ }
1729
+
1730
+ /**
1731
+ * @param {FunctionNode} node
1732
+ * @returns {node is FunctionDeclaration | FunctionExpression & { id: Identifier }}
1733
+ */
1734
+ function hasId(node) {
1735
+ return Boolean(
1736
+ /** @type {Partial<FunctionDeclaration | FunctionExpression>} */ (node)
1737
+ .id,
1738
+ )
1739
+ }
1740
+
1741
+ /** @typedef {import("estree").Node} Node */
1742
+ /** @typedef {import("eslint").SourceCode} SourceCode */
1743
+ /** @typedef {import("./types.mjs").HasSideEffectOptions} HasSideEffectOptions */
1744
+ /** @typedef {import("estree").BinaryExpression} BinaryExpression */
1745
+ /** @typedef {import("estree").MemberExpression} MemberExpression */
1746
+ /** @typedef {import("estree").MethodDefinition} MethodDefinition */
1747
+ /** @typedef {import("estree").Property} Property */
1748
+ /** @typedef {import("estree").PropertyDefinition} PropertyDefinition */
1749
+ /** @typedef {import("estree").UnaryExpression} UnaryExpression */
1750
+
1751
+ const typeConversionBinaryOps = Object.freeze(
1752
+ new Set([
1753
+ "==",
1754
+ "!=",
1755
+ "<",
1756
+ "<=",
1757
+ ">",
1758
+ ">=",
1759
+ "<<",
1760
+ ">>",
1761
+ ">>>",
1762
+ "+",
1763
+ "-",
1764
+ "*",
1765
+ "/",
1766
+ "%",
1767
+ "|",
1768
+ "^",
1769
+ "&",
1770
+ "in",
1771
+ ]),
1772
+ );
1773
+ const typeConversionUnaryOps = Object.freeze(new Set(["-", "+", "!", "~"]));
1774
+
1775
+ /**
1776
+ * Check whether the given value is an ASTNode or not.
1777
+ * @param {any} x The value to check.
1778
+ * @returns {x is Node} `true` if the value is an ASTNode.
1779
+ */
1780
+ function isNode(x) {
1781
+ return x !== null && typeof x === "object" && typeof x.type === "string"
1782
+ }
1783
+
1784
+ const visitor = Object.freeze(
1785
+ Object.assign(Object.create(null), {
1786
+ /**
1787
+ * @param {Node} node
1788
+ * @param {HasSideEffectOptions} options
1789
+ * @param {Record<string, string[]>} visitorKeys
1790
+ */
1791
+ $visit(node, options, visitorKeys) {
1792
+ const { type } = node;
1793
+
1794
+ if (typeof (/** @type {any} */ (this)[type]) === "function") {
1795
+ return /** @type {any} */ (this)[type](
1796
+ node,
1797
+ options,
1798
+ visitorKeys,
1799
+ )
1800
+ }
1801
+
1802
+ return this.$visitChildren(node, options, visitorKeys)
1803
+ },
1804
+
1805
+ /**
1806
+ * @param {Node} node
1807
+ * @param {HasSideEffectOptions} options
1808
+ * @param {Record<string, string[]>} visitorKeys
1809
+ */
1810
+ $visitChildren(node, options, visitorKeys) {
1811
+ const { type } = node;
1812
+
1813
+ for (const key of /** @type {(keyof Node)[]} */ (
1814
+ visitorKeys[type] || getKeys(node)
1815
+ )) {
1816
+ const value = node[key];
1817
+
1818
+ if (Array.isArray(value)) {
1819
+ for (const element of value) {
1820
+ if (
1821
+ isNode(element) &&
1822
+ this.$visit(element, options, visitorKeys)
1823
+ ) {
1824
+ return true
1825
+ }
1826
+ }
1827
+ } else if (
1828
+ isNode(value) &&
1829
+ this.$visit(value, options, visitorKeys)
1830
+ ) {
1831
+ return true
1832
+ }
1833
+ }
1834
+
1835
+ return false
1836
+ },
1837
+
1838
+ ArrowFunctionExpression() {
1839
+ return false
1840
+ },
1841
+ AssignmentExpression() {
1842
+ return true
1843
+ },
1844
+ AwaitExpression() {
1845
+ return true
1846
+ },
1847
+ /**
1848
+ * @param {BinaryExpression} node
1849
+ * @param {HasSideEffectOptions} options
1850
+ * @param {Record<string, string[]>} visitorKeys
1851
+ */
1852
+ BinaryExpression(node, options, visitorKeys) {
1853
+ if (
1854
+ options.considerImplicitTypeConversion &&
1855
+ typeConversionBinaryOps.has(node.operator) &&
1856
+ (node.left.type !== "Literal" || node.right.type !== "Literal")
1857
+ ) {
1858
+ return true
1859
+ }
1860
+ return this.$visitChildren(node, options, visitorKeys)
1861
+ },
1862
+ CallExpression() {
1863
+ return true
1864
+ },
1865
+ FunctionExpression() {
1866
+ return false
1867
+ },
1868
+ ImportExpression() {
1869
+ return true
1870
+ },
1871
+ /**
1872
+ * @param {MemberExpression} node
1873
+ * @param {HasSideEffectOptions} options
1874
+ * @param {Record<string, string[]>} visitorKeys
1875
+ */
1876
+ MemberExpression(node, options, visitorKeys) {
1877
+ if (options.considerGetters) {
1878
+ return true
1879
+ }
1880
+ if (
1881
+ options.considerImplicitTypeConversion &&
1882
+ node.computed &&
1883
+ node.property.type !== "Literal"
1884
+ ) {
1885
+ return true
1886
+ }
1887
+ return this.$visitChildren(node, options, visitorKeys)
1888
+ },
1889
+ /**
1890
+ * @param {MethodDefinition} node
1891
+ * @param {HasSideEffectOptions} options
1892
+ * @param {Record<string, string[]>} visitorKeys
1893
+ */
1894
+ MethodDefinition(node, options, visitorKeys) {
1895
+ if (
1896
+ options.considerImplicitTypeConversion &&
1897
+ node.computed &&
1898
+ node.key.type !== "Literal"
1899
+ ) {
1900
+ return true
1901
+ }
1902
+ return this.$visitChildren(node, options, visitorKeys)
1903
+ },
1904
+ NewExpression() {
1905
+ return true
1906
+ },
1907
+ /**
1908
+ * @param {Property} node
1909
+ * @param {HasSideEffectOptions} options
1910
+ * @param {Record<string, string[]>} visitorKeys
1911
+ */
1912
+ Property(node, options, visitorKeys) {
1913
+ if (
1914
+ options.considerImplicitTypeConversion &&
1915
+ node.computed &&
1916
+ node.key.type !== "Literal"
1917
+ ) {
1918
+ return true
1919
+ }
1920
+ return this.$visitChildren(node, options, visitorKeys)
1921
+ },
1922
+ /**
1923
+ * @param {PropertyDefinition} node
1924
+ * @param {HasSideEffectOptions} options
1925
+ * @param {Record<string, string[]>} visitorKeys
1926
+ */
1927
+ PropertyDefinition(node, options, visitorKeys) {
1928
+ if (
1929
+ options.considerImplicitTypeConversion &&
1930
+ node.computed &&
1931
+ node.key.type !== "Literal"
1932
+ ) {
1933
+ return true
1934
+ }
1935
+ return this.$visitChildren(node, options, visitorKeys)
1936
+ },
1937
+ /**
1938
+ * @param {UnaryExpression} node
1939
+ * @param {HasSideEffectOptions} options
1940
+ * @param {Record<string, string[]>} visitorKeys
1941
+ */
1942
+ UnaryExpression(node, options, visitorKeys) {
1943
+ if (node.operator === "delete") {
1944
+ return true
1945
+ }
1946
+ if (
1947
+ options.considerImplicitTypeConversion &&
1948
+ typeConversionUnaryOps.has(node.operator) &&
1949
+ node.argument.type !== "Literal"
1950
+ ) {
1951
+ return true
1952
+ }
1953
+ return this.$visitChildren(node, options, visitorKeys)
1954
+ },
1955
+ UpdateExpression() {
1956
+ return true
1957
+ },
1958
+ YieldExpression() {
1959
+ return true
1960
+ },
1961
+ }),
1962
+ );
1963
+
1964
+ /**
1965
+ * Check whether a given node has any side effect or not.
1966
+ * @param {Node} node The node to get.
1967
+ * @param {SourceCode} sourceCode The source code object.
1968
+ * @param {HasSideEffectOptions} [options] The option object.
1969
+ * @returns {boolean} `true` if the node has a certain side effect.
1970
+ */
1971
+ function hasSideEffect(node, sourceCode, options = {}) {
1972
+ const { considerGetters = false, considerImplicitTypeConversion = false } =
1973
+ options;
1974
+ return visitor.$visit(
1975
+ node,
1976
+ { considerGetters, considerImplicitTypeConversion },
1977
+ sourceCode.visitorKeys || KEYS,
1978
+ )
1979
+ }
1980
+
1981
+ /** @typedef {import("estree").Node} Node */
1982
+ /** @typedef {import("@typescript-eslint/types").TSESTree.NewExpression} TSNewExpression */
1983
+ /** @typedef {import("@typescript-eslint/types").TSESTree.CallExpression} TSCallExpression */
1984
+ /** @typedef {import("eslint").SourceCode} SourceCode */
1985
+ /** @typedef {import("eslint").AST.Token} Token */
1986
+ /** @typedef {import("eslint").Rule.Node} RuleNode */
1987
+
1988
+ /**
1989
+ * Get the left parenthesis of the parent node syntax if it exists.
1990
+ * E.g., `if (a) {}` then the `(`.
1991
+ * @param {Node} node The AST node to check.
1992
+ * @param {SourceCode} sourceCode The source code object to get tokens.
1993
+ * @returns {Token|null} The left parenthesis of the parent node syntax
1994
+ */
1995
+ // eslint-disable-next-line complexity
1996
+ function getParentSyntaxParen(node, sourceCode) {
1997
+ const parent = /** @type {RuleNode} */ (node).parent;
1998
+
1999
+ if (!parent) {
2000
+ return null
2001
+ }
2002
+
2003
+ switch (parent.type) {
2004
+ case "CallExpression":
2005
+ case "NewExpression":
2006
+ if (parent.arguments.length === 1 && parent.arguments[0] === node) {
2007
+ return sourceCode.getTokenAfter(
2008
+ // @ts-expect-error https://github.com/typescript-eslint/typescript-eslint/pull/5384
2009
+ parent.typeArguments ||
2010
+ /** @type {RuleNode} */ (
2011
+ /** @type {unknown} */ (
2012
+ /** @type {TSNewExpression | TSCallExpression} */ (
2013
+ parent
2014
+ ).typeParameters
2015
+ )
2016
+ ) ||
2017
+ parent.callee,
2018
+ isOpeningParenToken,
2019
+ )
2020
+ }
2021
+ return null
2022
+
2023
+ case "DoWhileStatement":
2024
+ if (parent.test === node) {
2025
+ return sourceCode.getTokenAfter(
2026
+ parent.body,
2027
+ isOpeningParenToken,
2028
+ )
2029
+ }
2030
+ return null
2031
+
2032
+ case "IfStatement":
2033
+ case "WhileStatement":
2034
+ if (parent.test === node) {
2035
+ return sourceCode.getFirstToken(parent, 1)
2036
+ }
2037
+ return null
2038
+
2039
+ case "ImportExpression":
2040
+ if (parent.source === node) {
2041
+ return sourceCode.getFirstToken(parent, 1)
2042
+ }
2043
+ return null
2044
+
2045
+ case "SwitchStatement":
2046
+ if (parent.discriminant === node) {
2047
+ return sourceCode.getFirstToken(parent, 1)
2048
+ }
2049
+ return null
2050
+
2051
+ case "WithStatement":
2052
+ if (parent.object === node) {
2053
+ return sourceCode.getFirstToken(parent, 1)
2054
+ }
2055
+ return null
2056
+
2057
+ default:
2058
+ return null
2059
+ }
2060
+ }
2061
+
2062
+ /**
2063
+ * Check whether a given node is parenthesized or not.
2064
+ * @param {number} times The number of parantheses.
2065
+ * @param {Node} node The AST node to check.
2066
+ * @param {SourceCode} sourceCode The source code object to get tokens.
2067
+ * @returns {boolean} `true` if the node is parenthesized the given times.
2068
+ */
2069
+ /**
2070
+ * Check whether a given node is parenthesized or not.
2071
+ * @param {Node} node The AST node to check.
2072
+ * @param {SourceCode} sourceCode The source code object to get tokens.
2073
+ * @returns {boolean} `true` if the node is parenthesized.
2074
+ */
2075
+ /**
2076
+ * Check whether a given node is parenthesized or not.
2077
+ * @param {Node|number} timesOrNode The first parameter.
2078
+ * @param {Node|SourceCode} nodeOrSourceCode The second parameter.
2079
+ * @param {SourceCode} [optionalSourceCode] The third parameter.
2080
+ * @returns {boolean} `true` if the node is parenthesized.
2081
+ */
2082
+ function isParenthesized(
2083
+ timesOrNode,
2084
+ nodeOrSourceCode,
2085
+ optionalSourceCode,
2086
+ ) {
2087
+ /** @type {number} */
2088
+ let times,
2089
+ /** @type {RuleNode} */
2090
+ node,
2091
+ /** @type {SourceCode} */
2092
+ sourceCode,
2093
+ maybeLeftParen,
2094
+ maybeRightParen;
2095
+ if (typeof timesOrNode === "number") {
2096
+ times = timesOrNode | 0;
2097
+ node = /** @type {RuleNode} */ (nodeOrSourceCode);
2098
+ sourceCode = /** @type {SourceCode} */ (optionalSourceCode);
2099
+ if (!(times >= 1)) {
2100
+ throw new TypeError("'times' should be a positive integer.")
2101
+ }
2102
+ } else {
2103
+ times = 1;
2104
+ node = /** @type {RuleNode} */ (timesOrNode);
2105
+ sourceCode = /** @type {SourceCode} */ (nodeOrSourceCode);
2106
+ }
2107
+
2108
+ if (
2109
+ node == null ||
2110
+ // `Program` can't be parenthesized
2111
+ node.parent == null ||
2112
+ // `CatchClause.param` can't be parenthesized, example `try {} catch (error) {}`
2113
+ (node.parent.type === "CatchClause" && node.parent.param === node)
2114
+ ) {
2115
+ return false
2116
+ }
2117
+
2118
+ maybeLeftParen = maybeRightParen = node;
2119
+ do {
2120
+ maybeLeftParen = sourceCode.getTokenBefore(maybeLeftParen);
2121
+ maybeRightParen = sourceCode.getTokenAfter(maybeRightParen);
2122
+ } while (
2123
+ maybeLeftParen != null &&
2124
+ maybeRightParen != null &&
2125
+ isOpeningParenToken(maybeLeftParen) &&
2126
+ isClosingParenToken(maybeRightParen) &&
2127
+ // Avoid false positive such as `if (a) {}`
2128
+ maybeLeftParen !== getParentSyntaxParen(node, sourceCode) &&
2129
+ --times > 0
2130
+ )
2131
+
2132
+ return times === 0
2133
+ }
2134
+
2135
+ /** @typedef {import("eslint").Scope.Scope} Scope */
2136
+ /** @typedef {import("eslint").Scope.Variable} Variable */
2137
+ /** @typedef {import("eslint").Rule.Node} RuleNode */
2138
+ /** @typedef {import("estree").Node} Node */
2139
+ /** @typedef {import("estree").Expression} Expression */
2140
+ /** @typedef {import("estree").Pattern} Pattern */
2141
+ /** @typedef {import("estree").Identifier} Identifier */
2142
+ /** @typedef {import("estree").SimpleCallExpression} CallExpression */
2143
+ /** @typedef {import("estree").Program} Program */
2144
+ /** @typedef {import("estree").ImportDeclaration} ImportDeclaration */
2145
+ /** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */
2146
+ /** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclaration */
2147
+ /** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */
2148
+ /** @typedef {import("estree").ImportSpecifier} ImportSpecifier */
2149
+ /** @typedef {import("estree").ImportDefaultSpecifier} ImportDefaultSpecifier */
2150
+ /** @typedef {import("estree").ImportNamespaceSpecifier} ImportNamespaceSpecifier */
2151
+ /** @typedef {import("estree").ExportSpecifier} ExportSpecifier */
2152
+ /** @typedef {import("estree").Property} Property */
2153
+ /** @typedef {import("estree").AssignmentProperty} AssignmentProperty */
2154
+ /** @typedef {import("estree").Literal} Literal */
2155
+ /** @typedef {import("@typescript-eslint/types").TSESTree.Node} TSESTreeNode */
2156
+ /** @typedef {import("./types.mjs").ReferenceTrackerOptions} ReferenceTrackerOptions */
2157
+ /**
2158
+ * @template T
2159
+ * @typedef {import("./types.mjs").TraceMap<T>} TraceMap
2160
+ */
2161
+ /**
2162
+ * @template T
2163
+ * @typedef {import("./types.mjs").TraceMapObject<T>} TraceMapObject
2164
+ */
2165
+ /**
2166
+ * @template T
2167
+ * @typedef {import("./types.mjs").TrackedReferences<T>} TrackedReferences
2168
+ */
2169
+
2170
+ const IMPORT_TYPE = /^(?:Import|Export(?:All|Default|Named))Declaration$/u;
2171
+
2172
+ /**
2173
+ * Check whether a given node is an import node or not.
2174
+ * @param {Node} node
2175
+ * @returns {node is ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration&{source: Literal}} `true` if the node is an import node.
2176
+ */
2177
+ function isHasSource(node) {
2178
+ return (
2179
+ IMPORT_TYPE.test(node.type) &&
2180
+ /** @type {ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration} */ (
2181
+ node
2182
+ ).source != null
2183
+ )
2184
+ }
2185
+ const has =
2186
+ /** @type {<T>(traceMap: TraceMap<unknown>, v: T) => v is (string extends T ? string : T)} */ (
2187
+ Function.call.bind(Object.hasOwnProperty)
2188
+ );
2189
+
2190
+ const READ = Symbol("read");
2191
+ const CALL = Symbol("call");
2192
+ const CONSTRUCT = Symbol("construct");
2193
+ const ESM = Symbol("esm");
2194
+
2195
+ const requireCall = { require: { [CALL]: true } };
2196
+
2197
+ /**
2198
+ * Check whether a given variable is modified or not.
2199
+ * @param {Variable|undefined} variable The variable to check.
2200
+ * @returns {boolean} `true` if the variable is modified.
2201
+ */
2202
+ function isModifiedGlobal(variable) {
2203
+ return (
2204
+ variable == null ||
2205
+ variable.defs.length !== 0 ||
2206
+ variable.references.some((r) => r.isWrite())
2207
+ )
2208
+ }
2209
+
2210
+ /**
2211
+ * Check if the value of a given node is passed through to the parent syntax as-is.
2212
+ * For example, `a` and `b` in (`a || b` and `c ? a : b`) are passed through.
2213
+ * @param {Node} node A node to check.
2214
+ * @returns {node is RuleNode & {parent: Expression}} `true` if the node is passed through.
2215
+ */
2216
+ function isPassThrough(node) {
2217
+ const parent = /** @type {TSESTreeNode} */ (node).parent;
2218
+
2219
+ if (parent) {
2220
+ switch (parent.type) {
2221
+ case "ConditionalExpression":
2222
+ return parent.consequent === node || parent.alternate === node
2223
+ case "LogicalExpression":
2224
+ return true
2225
+ case "SequenceExpression":
2226
+ return (
2227
+ parent.expressions[parent.expressions.length - 1] === node
2228
+ )
2229
+ case "ChainExpression":
2230
+ return true
2231
+ case "TSAsExpression":
2232
+ case "TSSatisfiesExpression":
2233
+ case "TSTypeAssertion":
2234
+ case "TSNonNullExpression":
2235
+ case "TSInstantiationExpression":
2236
+ return true
2237
+
2238
+ default:
2239
+ return false
2240
+ }
2241
+ }
2242
+ return false
2243
+ }
2244
+
2245
+ /**
2246
+ * The reference tracker.
2247
+ */
2248
+ class ReferenceTracker {
2249
+ /**
2250
+ * Initialize this tracker.
2251
+ * @param {Scope} globalScope The global scope.
2252
+ * @param {object} [options] The options.
2253
+ * @param {"legacy"|"strict"} [options.mode="strict"] The mode to determine the ImportDeclaration's behavior for CJS modules.
2254
+ * @param {string[]} [options.globalObjectNames=["global","globalThis","self","window"]] The variable names for Global Object.
2255
+ */
2256
+ constructor(globalScope, options = {}) {
2257
+ const {
2258
+ mode = "strict",
2259
+ globalObjectNames = ["global", "globalThis", "self", "window"],
2260
+ } = options;
2261
+ /** @private @type {Variable[]} */
2262
+ this.variableStack = [];
2263
+ /** @private */
2264
+ this.globalScope = globalScope;
2265
+ /** @private */
2266
+ this.mode = mode;
2267
+ /** @private */
2268
+ this.globalObjectNames = globalObjectNames.slice(0);
2269
+ }
2270
+
2271
+ /**
2272
+ * Iterate the references of global variables.
2273
+ * @template T
2274
+ * @param {TraceMap<T>} traceMap The trace map.
2275
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate references.
2276
+ */
2277
+ *iterateGlobalReferences(traceMap) {
2278
+ for (const key of Object.keys(traceMap)) {
2279
+ const nextTraceMap = traceMap[key];
2280
+ const path = [key];
2281
+ const variable = this.globalScope.set.get(key);
2282
+
2283
+ if (isModifiedGlobal(variable)) {
2284
+ continue
2285
+ }
2286
+
2287
+ yield* this._iterateVariableReferences(
2288
+ /** @type {Variable} */ (variable),
2289
+ path,
2290
+ nextTraceMap,
2291
+ true,
2292
+ );
2293
+ }
2294
+
2295
+ for (const key of this.globalObjectNames) {
2296
+ /** @type {string[]} */
2297
+ const path = [];
2298
+ const variable = this.globalScope.set.get(key);
2299
+
2300
+ if (isModifiedGlobal(variable)) {
2301
+ continue
2302
+ }
2303
+
2304
+ yield* this._iterateVariableReferences(
2305
+ /** @type {Variable} */ (variable),
2306
+ path,
2307
+ traceMap,
2308
+ false,
2309
+ );
2310
+ }
2311
+ }
2312
+
2313
+ /**
2314
+ * Iterate the references of CommonJS modules.
2315
+ * @template T
2316
+ * @param {TraceMap<T>} traceMap The trace map.
2317
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate references.
2318
+ */
2319
+ *iterateCjsReferences(traceMap) {
2320
+ for (const { node } of this.iterateGlobalReferences(requireCall)) {
2321
+ const key = getStringIfConstant(
2322
+ /** @type {CallExpression} */ (node).arguments[0],
2323
+ );
2324
+ if (key == null || !has(traceMap, key)) {
2325
+ continue
2326
+ }
2327
+
2328
+ const nextTraceMap = traceMap[key];
2329
+ const path = [key];
2330
+
2331
+ if (nextTraceMap[READ]) {
2332
+ yield {
2333
+ node,
2334
+ path,
2335
+ type: READ,
2336
+ info: nextTraceMap[READ],
2337
+ };
2338
+ }
2339
+ yield* this._iteratePropertyReferences(
2340
+ /** @type {CallExpression} */ (node),
2341
+ path,
2342
+ nextTraceMap,
2343
+ );
2344
+ }
2345
+ }
2346
+
2347
+ /**
2348
+ * Iterate the references of ES modules.
2349
+ * @template T
2350
+ * @param {TraceMap<T>} traceMap The trace map.
2351
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate references.
2352
+ */
2353
+ *iterateEsmReferences(traceMap) {
2354
+ const programNode = /** @type {Program} */ (this.globalScope.block);
2355
+
2356
+ for (const node of programNode.body) {
2357
+ if (!isHasSource(node)) {
2358
+ continue
2359
+ }
2360
+ const moduleId = /** @type {string} */ (node.source.value);
2361
+
2362
+ if (!has(traceMap, moduleId)) {
2363
+ continue
2364
+ }
2365
+ const nextTraceMap = traceMap[moduleId];
2366
+ const path = [moduleId];
2367
+
2368
+ if (nextTraceMap[READ]) {
2369
+ yield {
2370
+ // eslint-disable-next-line object-shorthand -- apply type
2371
+ node: /** @type {RuleNode} */ (node),
2372
+ path,
2373
+ type: READ,
2374
+ info: nextTraceMap[READ],
2375
+ };
2376
+ }
2377
+
2378
+ if (node.type === "ExportAllDeclaration") {
2379
+ for (const key of Object.keys(nextTraceMap)) {
2380
+ const exportTraceMap = nextTraceMap[key];
2381
+ if (exportTraceMap[READ]) {
2382
+ yield {
2383
+ // eslint-disable-next-line object-shorthand -- apply type
2384
+ node: /** @type {RuleNode} */ (node),
2385
+ path: path.concat(key),
2386
+ type: READ,
2387
+ info: exportTraceMap[READ],
2388
+ };
2389
+ }
2390
+ }
2391
+ } else {
2392
+ for (const specifier of node.specifiers) {
2393
+ const esm = has(nextTraceMap, ESM);
2394
+ const it = this._iterateImportReferences(
2395
+ specifier,
2396
+ path,
2397
+ esm
2398
+ ? nextTraceMap
2399
+ : this.mode === "legacy"
2400
+ ? { default: nextTraceMap, ...nextTraceMap }
2401
+ : { default: nextTraceMap },
2402
+ );
2403
+
2404
+ if (esm) {
2405
+ yield* it;
2406
+ } else {
2407
+ for (const report of it) {
2408
+ report.path = report.path.filter(exceptDefault);
2409
+ if (
2410
+ report.path.length >= 2 ||
2411
+ report.type !== READ
2412
+ ) {
2413
+ yield report;
2414
+ }
2415
+ }
2416
+ }
2417
+ }
2418
+ }
2419
+ }
2420
+ }
2421
+
2422
+ /**
2423
+ * Iterate the property references for a given expression AST node.
2424
+ * @template T
2425
+ * @param {Expression} node The expression AST node to iterate property references.
2426
+ * @param {TraceMap<T>} traceMap The trace map.
2427
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate property references.
2428
+ */
2429
+ *iteratePropertyReferences(node, traceMap) {
2430
+ yield* this._iteratePropertyReferences(node, [], traceMap);
2431
+ }
2432
+
2433
+ /**
2434
+ * Iterate the references for a given variable.
2435
+ * @private
2436
+ * @template T
2437
+ * @param {Variable} variable The variable to iterate that references.
2438
+ * @param {string[]} path The current path.
2439
+ * @param {TraceMapObject<T>} traceMap The trace map.
2440
+ * @param {boolean} shouldReport = The flag to report those references.
2441
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate references.
2442
+ */
2443
+ *_iterateVariableReferences(variable, path, traceMap, shouldReport) {
2444
+ if (this.variableStack.includes(variable)) {
2445
+ return
2446
+ }
2447
+ this.variableStack.push(variable);
2448
+ try {
2449
+ for (const reference of variable.references) {
2450
+ if (!reference.isRead()) {
2451
+ continue
2452
+ }
2453
+ const node = /** @type {RuleNode & Identifier} */ (
2454
+ reference.identifier
2455
+ );
2456
+
2457
+ if (shouldReport && traceMap[READ]) {
2458
+ yield { node, path, type: READ, info: traceMap[READ] };
2459
+ }
2460
+ yield* this._iteratePropertyReferences(node, path, traceMap);
2461
+ }
2462
+ } finally {
2463
+ this.variableStack.pop();
2464
+ }
2465
+ }
2466
+
2467
+ /**
2468
+ * Iterate the references for a given AST node.
2469
+ * @private
2470
+ * @template T
2471
+ * @param {Expression} rootNode The AST node to iterate references.
2472
+ * @param {string[]} path The current path.
2473
+ * @param {TraceMapObject<T>} traceMap The trace map.
2474
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate references.
2475
+ */
2476
+ //eslint-disable-next-line complexity
2477
+ *_iteratePropertyReferences(rootNode, path, traceMap) {
2478
+ let node = rootNode;
2479
+ while (isPassThrough(node)) {
2480
+ node = node.parent;
2481
+ }
2482
+
2483
+ const parent = /** @type {RuleNode} */ (node).parent;
2484
+ if (!parent) {
2485
+ return
2486
+ }
2487
+ if (parent.type === "MemberExpression") {
2488
+ if (parent.object === node) {
2489
+ const key = getPropertyName(parent);
2490
+ if (key == null || !has(traceMap, key)) {
2491
+ return
2492
+ }
2493
+
2494
+ path = path.concat(key); //eslint-disable-line no-param-reassign
2495
+ const nextTraceMap = traceMap[key];
2496
+ if (nextTraceMap[READ]) {
2497
+ yield {
2498
+ node: parent,
2499
+ path,
2500
+ type: READ,
2501
+ info: nextTraceMap[READ],
2502
+ };
2503
+ }
2504
+ yield* this._iteratePropertyReferences(
2505
+ parent,
2506
+ path,
2507
+ nextTraceMap,
2508
+ );
2509
+ }
2510
+ return
2511
+ }
2512
+ if (parent.type === "CallExpression") {
2513
+ if (parent.callee === node && traceMap[CALL]) {
2514
+ yield { node: parent, path, type: CALL, info: traceMap[CALL] };
2515
+ }
2516
+ return
2517
+ }
2518
+ if (parent.type === "NewExpression") {
2519
+ if (parent.callee === node && traceMap[CONSTRUCT]) {
2520
+ yield {
2521
+ node: parent,
2522
+ path,
2523
+ type: CONSTRUCT,
2524
+ info: traceMap[CONSTRUCT],
2525
+ };
2526
+ }
2527
+ return
2528
+ }
2529
+ if (parent.type === "AssignmentExpression") {
2530
+ if (parent.right === node) {
2531
+ yield* this._iterateLhsReferences(parent.left, path, traceMap);
2532
+ yield* this._iteratePropertyReferences(parent, path, traceMap);
2533
+ }
2534
+ return
2535
+ }
2536
+ if (parent.type === "AssignmentPattern") {
2537
+ if (parent.right === node) {
2538
+ yield* this._iterateLhsReferences(parent.left, path, traceMap);
2539
+ }
2540
+ return
2541
+ }
2542
+ if (parent.type === "VariableDeclarator") {
2543
+ if (parent.init === node) {
2544
+ yield* this._iterateLhsReferences(parent.id, path, traceMap);
2545
+ }
2546
+ }
2547
+ }
2548
+
2549
+ /**
2550
+ * Iterate the references for a given Pattern node.
2551
+ * @private
2552
+ * @template T
2553
+ * @param {Pattern} patternNode The Pattern node to iterate references.
2554
+ * @param {string[]} path The current path.
2555
+ * @param {TraceMapObject<T>} traceMap The trace map.
2556
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate references.
2557
+ */
2558
+ *_iterateLhsReferences(patternNode, path, traceMap) {
2559
+ if (patternNode.type === "Identifier") {
2560
+ const variable = findVariable(this.globalScope, patternNode);
2561
+ if (variable != null) {
2562
+ yield* this._iterateVariableReferences(
2563
+ variable,
2564
+ path,
2565
+ traceMap,
2566
+ false,
2567
+ );
2568
+ }
2569
+ return
2570
+ }
2571
+ if (patternNode.type === "ObjectPattern") {
2572
+ for (const property of patternNode.properties) {
2573
+ const key = getPropertyName(
2574
+ /** @type {AssignmentProperty} */ (property),
2575
+ );
2576
+
2577
+ if (key == null || !has(traceMap, key)) {
2578
+ continue
2579
+ }
2580
+
2581
+ const nextPath = path.concat(key);
2582
+ const nextTraceMap = traceMap[key];
2583
+ if (nextTraceMap[READ]) {
2584
+ yield {
2585
+ node: /** @type {RuleNode} */ (property),
2586
+ path: nextPath,
2587
+ type: READ,
2588
+ info: nextTraceMap[READ],
2589
+ };
2590
+ }
2591
+ yield* this._iterateLhsReferences(
2592
+ /** @type {AssignmentProperty} */ (property).value,
2593
+ nextPath,
2594
+ nextTraceMap,
2595
+ );
2596
+ }
2597
+ return
2598
+ }
2599
+ if (patternNode.type === "AssignmentPattern") {
2600
+ yield* this._iterateLhsReferences(patternNode.left, path, traceMap);
2601
+ }
2602
+ }
2603
+
2604
+ /**
2605
+ * Iterate the references for a given ModuleSpecifier node.
2606
+ * @private
2607
+ * @template T
2608
+ * @param {ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier} specifierNode The ModuleSpecifier node to iterate references.
2609
+ * @param {string[]} path The current path.
2610
+ * @param {TraceMapObject<T>} traceMap The trace map.
2611
+ * @returns {IterableIterator<TrackedReferences<T>>} The iterator to iterate references.
2612
+ */
2613
+ *_iterateImportReferences(specifierNode, path, traceMap) {
2614
+ const type = specifierNode.type;
2615
+
2616
+ if (type === "ImportSpecifier" || type === "ImportDefaultSpecifier") {
2617
+ const key =
2618
+ type === "ImportDefaultSpecifier"
2619
+ ? "default"
2620
+ : specifierNode.imported.type === "Identifier"
2621
+ ? specifierNode.imported.name
2622
+ : specifierNode.imported.value;
2623
+ if (!has(traceMap, key)) {
2624
+ return
2625
+ }
2626
+
2627
+ path = path.concat(key); //eslint-disable-line no-param-reassign
2628
+ const nextTraceMap = traceMap[key];
2629
+ if (nextTraceMap[READ]) {
2630
+ yield {
2631
+ node: /** @type {RuleNode} */ (specifierNode),
2632
+ path,
2633
+ type: READ,
2634
+ info: nextTraceMap[READ],
2635
+ };
2636
+ }
2637
+ yield* this._iterateVariableReferences(
2638
+ /** @type {Variable} */ (
2639
+ findVariable(this.globalScope, specifierNode.local)
2640
+ ),
2641
+ path,
2642
+ nextTraceMap,
2643
+ false,
2644
+ );
2645
+
2646
+ return
2647
+ }
2648
+
2649
+ if (type === "ImportNamespaceSpecifier") {
2650
+ yield* this._iterateVariableReferences(
2651
+ /** @type {Variable} */ (
2652
+ findVariable(this.globalScope, specifierNode.local)
2653
+ ),
2654
+ path,
2655
+ traceMap,
2656
+ false,
2657
+ );
2658
+ return
2659
+ }
2660
+
2661
+ if (type === "ExportSpecifier") {
2662
+ const key =
2663
+ specifierNode.local.type === "Identifier"
2664
+ ? specifierNode.local.name
2665
+ : specifierNode.local.value;
2666
+ if (!has(traceMap, key)) {
2667
+ return
2668
+ }
2669
+
2670
+ path = path.concat(key); //eslint-disable-line no-param-reassign
2671
+ const nextTraceMap = traceMap[key];
2672
+ if (nextTraceMap[READ]) {
2673
+ yield {
2674
+ node: /** @type {RuleNode} */ (specifierNode),
2675
+ path,
2676
+ type: READ,
2677
+ info: nextTraceMap[READ],
2678
+ };
2679
+ }
2680
+ }
2681
+ }
2682
+ }
2683
+
2684
+ ReferenceTracker.READ = READ;
2685
+ ReferenceTracker.CALL = CALL;
2686
+ ReferenceTracker.CONSTRUCT = CONSTRUCT;
2687
+ ReferenceTracker.ESM = ESM;
2688
+
2689
+ /**
2690
+ * This is a predicate function for Array#filter.
2691
+ * @param {string} name A name part.
2692
+ * @param {number} index The index of the name.
2693
+ * @returns {boolean} `false` if it's default.
2694
+ */
2695
+ function exceptDefault(name, index) {
2696
+ return !(index === 1 && name === "default")
2697
+ }
2698
+
2699
+ export { CONSTRUCT as C, ReferenceTracker as R, isClosingParenToken as a, isCommaToken as b, isSemicolonToken as c, isOpeningBraceToken as d, getFunctionNameWithKind as e, findVariable as f, getStaticValue as g, getFunctionHeadLocation as h, isOpeningParenToken as i, getStringIfConstant as j, hasSideEffect as k, isNotSemicolonToken as l, getPropertyName as m, isColonToken as n, isParenthesized as o, isOpeningBracketToken as p, isClosingBracketToken as q, isArrowToken as r, isClosingBraceToken as s, READ as t, CALL as u, isCommentToken as v };