@tanstack/eslint-plugin-query 5.0.0-beta.0 → 5.0.0-beta.10

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 (82) hide show
  1. package/build/legacy/__tests__/configs.test.cjs +21 -0
  2. package/build/legacy/__tests__/configs.test.cjs.map +1 -0
  3. package/build/legacy/__tests__/configs.test.d.cts +2 -0
  4. package/build/legacy/__tests__/configs.test.d.ts +2 -0
  5. package/build/legacy/__tests__/configs.test.js +19 -0
  6. package/build/legacy/__tests__/configs.test.js.map +1 -0
  7. package/build/legacy/__tests__/exhaustive-deps.test.cjs +722 -0
  8. package/build/legacy/__tests__/exhaustive-deps.test.cjs.map +1 -0
  9. package/build/legacy/__tests__/exhaustive-deps.test.d.cts +2 -0
  10. package/build/legacy/__tests__/exhaustive-deps.test.d.ts +2 -0
  11. package/build/legacy/__tests__/exhaustive-deps.test.js +720 -0
  12. package/build/legacy/__tests__/exhaustive-deps.test.js.map +1 -0
  13. package/build/legacy/configs.cjs +46 -0
  14. package/build/legacy/configs.cjs.map +1 -0
  15. package/build/legacy/configs.d.cts +8 -0
  16. package/build/legacy/configs.d.ts +8 -0
  17. package/build/legacy/configs.js +21 -0
  18. package/build/legacy/configs.js.map +1 -0
  19. package/build/legacy/index.d.cts +3 -14
  20. package/build/legacy/index.d.ts +3 -14
  21. package/build/legacy/rules/exhaustive-deps.rule.cjs +141 -0
  22. package/build/legacy/rules/exhaustive-deps.rule.cjs.map +1 -0
  23. package/build/legacy/rules/exhaustive-deps.rule.d.cts +6 -0
  24. package/build/legacy/rules/exhaustive-deps.rule.d.ts +6 -0
  25. package/build/legacy/rules/exhaustive-deps.rule.js +115 -0
  26. package/build/legacy/rules/exhaustive-deps.rule.js.map +1 -0
  27. package/build/legacy/rules/exhaustive-deps.utils.cjs +52 -0
  28. package/build/legacy/rules/exhaustive-deps.utils.cjs.map +1 -0
  29. package/build/legacy/rules/exhaustive-deps.utils.d.cts +12 -0
  30. package/build/legacy/rules/exhaustive-deps.utils.d.ts +12 -0
  31. package/build/legacy/rules/exhaustive-deps.utils.js +27 -0
  32. package/build/legacy/rules/exhaustive-deps.utils.js.map +1 -0
  33. package/build/legacy/rules.cjs +44 -0
  34. package/build/legacy/rules.cjs.map +1 -0
  35. package/build/legacy/rules.d.cts +7 -0
  36. package/build/legacy/rules.d.ts +7 -0
  37. package/build/legacy/rules.js +9 -0
  38. package/build/legacy/rules.js.map +1 -0
  39. package/build/legacy/utils/ast-utils.cjs +247 -0
  40. package/build/legacy/utils/ast-utils.cjs.map +1 -0
  41. package/build/legacy/utils/ast-utils.d.cts +41 -0
  42. package/build/legacy/utils/ast-utils.d.ts +41 -0
  43. package/build/legacy/utils/ast-utils.js +222 -0
  44. package/build/legacy/utils/ast-utils.js.map +1 -0
  45. package/build/legacy/utils/create-rule.cjs +39 -0
  46. package/build/legacy/utils/create-rule.cjs.map +1 -0
  47. package/build/legacy/utils/create-rule.d.cts +10 -0
  48. package/build/legacy/utils/create-rule.d.ts +10 -0
  49. package/build/legacy/utils/create-rule.js +14 -0
  50. package/build/legacy/utils/create-rule.js.map +1 -0
  51. package/build/legacy/utils/detect-react-query-imports.cjs +70 -0
  52. package/build/legacy/utils/detect-react-query-imports.cjs.map +1 -0
  53. package/build/legacy/utils/detect-react-query-imports.d.cts +12 -0
  54. package/build/legacy/utils/detect-react-query-imports.d.ts +12 -0
  55. package/build/legacy/utils/detect-react-query-imports.js +45 -0
  56. package/build/legacy/utils/detect-react-query-imports.js.map +1 -0
  57. package/build/legacy/utils/object-utils.cjs +33 -0
  58. package/build/legacy/utils/object-utils.cjs.map +1 -0
  59. package/build/legacy/utils/object-utils.d.cts +3 -0
  60. package/build/legacy/utils/object-utils.d.ts +3 -0
  61. package/build/legacy/utils/object-utils.js +8 -0
  62. package/build/legacy/utils/object-utils.js.map +1 -0
  63. package/build/legacy/utils/test-utils.cjs +36 -0
  64. package/build/legacy/utils/test-utils.cjs.map +1 -0
  65. package/build/legacy/utils/test-utils.d.cts +3 -0
  66. package/build/legacy/utils/test-utils.d.ts +3 -0
  67. package/build/legacy/utils/test-utils.js +11 -0
  68. package/build/legacy/utils/test-utils.js.map +1 -0
  69. package/build/legacy/utils/unique-by.cjs +33 -0
  70. package/build/legacy/utils/unique-by.cjs.map +1 -0
  71. package/build/legacy/utils/unique-by.d.cts +3 -0
  72. package/build/legacy/utils/unique-by.d.ts +3 -0
  73. package/build/legacy/utils/unique-by.js +8 -0
  74. package/build/legacy/utils/unique-by.js.map +1 -0
  75. package/package.json +2 -2
  76. package/src/{configs/index.test.ts → __tests__/configs.test.ts} +1 -1
  77. package/src/{rules/exhaustive-deps → __tests__}/exhaustive-deps.test.ts +2 -2
  78. package/src/{configs/index.ts → configs.ts} +1 -1
  79. package/src/rules/{exhaustive-deps/exhaustive-deps.rule.ts → exhaustive-deps.rule.ts} +3 -3
  80. package/src/rules/{exhaustive-deps/exhaustive-deps.utils.ts → exhaustive-deps.utils.ts} +1 -1
  81. package/src/rules.ts +5 -0
  82. package/src/rules/index.ts +0 -5
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name2 in all)
10
+ __defProp(target, name2, { get: all[name2], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/rules.ts
31
+ var rules_exports = {};
32
+ __export(rules_exports, {
33
+ rules: () => rules
34
+ });
35
+ module.exports = __toCommonJS(rules_exports);
36
+ var exhaustiveDeps = __toESM(require("./rules/exhaustive-deps.rule.cjs"), 1);
37
+ var rules = {
38
+ [exhaustiveDeps.name]: exhaustiveDeps.rule
39
+ };
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ rules
43
+ });
44
+ //# sourceMappingURL=rules.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/rules.ts"],"sourcesContent":["import * as exhaustiveDeps from './rules/exhaustive-deps.rule'\n\nexport const rules = {\n [exhaustiveDeps.name]: exhaustiveDeps.rule,\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAgC;AAEzB,IAAM,QAAQ;AAAA,EACnB,CAAgB,mBAAI,GAAkB;AACxC;","names":[]}
@@ -0,0 +1,7 @@
1
+ import * as _typescript_eslint_utils_dist_ts_eslint_Rule from '@typescript-eslint/utils/dist/ts-eslint/Rule';
2
+
3
+ declare const rules: {
4
+ "exhaustive-deps": _typescript_eslint_utils_dist_ts_eslint_Rule.RuleModule<string, readonly unknown[], _typescript_eslint_utils_dist_ts_eslint_Rule.RuleListener>;
5
+ };
6
+
7
+ export { rules };
@@ -0,0 +1,7 @@
1
+ import * as _typescript_eslint_utils_dist_ts_eslint_Rule from '@typescript-eslint/utils/dist/ts-eslint/Rule';
2
+
3
+ declare const rules: {
4
+ "exhaustive-deps": _typescript_eslint_utils_dist_ts_eslint_Rule.RuleModule<string, readonly unknown[], _typescript_eslint_utils_dist_ts_eslint_Rule.RuleListener>;
5
+ };
6
+
7
+ export { rules };
@@ -0,0 +1,9 @@
1
+ // src/rules.ts
2
+ import * as exhaustiveDeps from "./rules/exhaustive-deps.rule.js";
3
+ var rules = {
4
+ [exhaustiveDeps.name]: exhaustiveDeps.rule
5
+ };
6
+ export {
7
+ rules
8
+ };
9
+ //# sourceMappingURL=rules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/rules.ts"],"sourcesContent":["import * as exhaustiveDeps from './rules/exhaustive-deps.rule'\n\nexport const rules = {\n [exhaustiveDeps.name]: exhaustiveDeps.rule,\n}\n"],"mappings":";AAAA,YAAY,oBAAoB;AAEzB,IAAM,QAAQ;AAAA,EACnB,CAAgB,mBAAI,GAAkB;AACxC;","names":[]}
@@ -0,0 +1,247 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/utils/ast-utils.ts
21
+ var ast_utils_exports = {};
22
+ __export(ast_utils_exports, {
23
+ ASTUtils: () => ASTUtils
24
+ });
25
+ module.exports = __toCommonJS(ast_utils_exports);
26
+ var import_utils = require("@typescript-eslint/utils");
27
+ var import_unique_by = require("./unique-by.cjs");
28
+ var ASTUtils = {
29
+ isNodeOfOneOf(node, types) {
30
+ return types.includes(node.type);
31
+ },
32
+ isIdentifier(node) {
33
+ return node.type === import_utils.AST_NODE_TYPES.Identifier;
34
+ },
35
+ isIdentifierWithName(node, name) {
36
+ return ASTUtils.isIdentifier(node) && node.name === name;
37
+ },
38
+ isIdentifierWithOneOfNames(node, name) {
39
+ return ASTUtils.isIdentifier(node) && name.includes(node.name);
40
+ },
41
+ isProperty(node) {
42
+ return node.type === import_utils.AST_NODE_TYPES.Property;
43
+ },
44
+ isObjectExpression(node) {
45
+ return node.type === import_utils.AST_NODE_TYPES.ObjectExpression;
46
+ },
47
+ isPropertyWithIdentifierKey(node, key) {
48
+ return ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key);
49
+ },
50
+ findPropertyWithIdentifierKey(properties, key) {
51
+ return properties.find(
52
+ (x) => ASTUtils.isPropertyWithIdentifierKey(x, key)
53
+ );
54
+ },
55
+ getNestedIdentifiers(node) {
56
+ const identifiers = [];
57
+ if (ASTUtils.isIdentifier(node)) {
58
+ identifiers.push(node);
59
+ }
60
+ if ("arguments" in node) {
61
+ node.arguments.forEach((x) => {
62
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
63
+ });
64
+ }
65
+ if ("elements" in node) {
66
+ node.elements.forEach((x) => {
67
+ if (x !== null) {
68
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
69
+ }
70
+ });
71
+ }
72
+ if ("properties" in node) {
73
+ node.properties.forEach((x) => {
74
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
75
+ });
76
+ }
77
+ if ("expressions" in node) {
78
+ node.expressions.forEach((x) => {
79
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
80
+ });
81
+ }
82
+ if (node.type === import_utils.AST_NODE_TYPES.Property) {
83
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.value));
84
+ }
85
+ if (node.type === import_utils.AST_NODE_TYPES.SpreadElement) {
86
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
87
+ }
88
+ if (node.type === import_utils.AST_NODE_TYPES.MemberExpression) {
89
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.object));
90
+ }
91
+ if (node.type === import_utils.AST_NODE_TYPES.UnaryExpression) {
92
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
93
+ }
94
+ if (node.type === import_utils.AST_NODE_TYPES.ChainExpression) {
95
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
96
+ }
97
+ if (node.type === import_utils.AST_NODE_TYPES.TSNonNullExpression) {
98
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
99
+ }
100
+ return identifiers;
101
+ },
102
+ isAncestorIsCallee(identifier) {
103
+ let previousNode = identifier;
104
+ let currentNode = identifier.parent;
105
+ while (currentNode !== void 0) {
106
+ if (currentNode.type === import_utils.AST_NODE_TYPES.CallExpression && currentNode.callee === previousNode) {
107
+ return true;
108
+ }
109
+ if (currentNode.type !== import_utils.AST_NODE_TYPES.MemberExpression) {
110
+ return false;
111
+ }
112
+ previousNode = currentNode;
113
+ currentNode = currentNode.parent;
114
+ }
115
+ return false;
116
+ },
117
+ traverseUpOnly(identifier, allowedNodeTypes) {
118
+ const parent = identifier.parent;
119
+ if (parent !== void 0 && allowedNodeTypes.includes(parent.type)) {
120
+ return ASTUtils.traverseUpOnly(parent, allowedNodeTypes);
121
+ }
122
+ return identifier;
123
+ },
124
+ isDeclaredInNode(params) {
125
+ const { functionNode, reference, scopeManager } = params;
126
+ const scope = scopeManager.acquire(functionNode);
127
+ if (scope === null) {
128
+ return false;
129
+ }
130
+ return scope.set.has(reference.identifier.name);
131
+ },
132
+ getExternalRefs(params) {
133
+ const { scopeManager, sourceCode, node } = params;
134
+ const scope = scopeManager.acquire(node);
135
+ if (scope === null) {
136
+ return [];
137
+ }
138
+ const references = scope.references.filter((x) => x.isRead() && !scope.set.has(x.identifier.name)).map((x) => {
139
+ const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [
140
+ import_utils.AST_NODE_TYPES.MemberExpression,
141
+ import_utils.AST_NODE_TYPES.Identifier
142
+ ]);
143
+ return {
144
+ variable: x,
145
+ node: referenceNode,
146
+ text: sourceCode.getText(referenceNode)
147
+ };
148
+ });
149
+ const localRefIds = new Set(
150
+ [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0]))
151
+ );
152
+ const externalRefs = references.filter(
153
+ (x) => x.variable.resolved === null || !localRefIds.has(x.text)
154
+ );
155
+ return (0, import_unique_by.uniqueBy)(externalRefs, (x) => x.text).map((x) => x.variable);
156
+ },
157
+ mapKeyNodeToText(node, sourceCode) {
158
+ return sourceCode.getText(
159
+ ASTUtils.traverseUpOnly(node, [
160
+ import_utils.AST_NODE_TYPES.MemberExpression,
161
+ import_utils.AST_NODE_TYPES.Identifier
162
+ ])
163
+ );
164
+ },
165
+ isValidReactComponentOrHookName(identifier) {
166
+ return identifier !== null && /^(use|[A-Z])/.test(identifier.name);
167
+ },
168
+ getFunctionAncestor(context) {
169
+ return context.getAncestors().find((x) => {
170
+ var _a;
171
+ if (x.type === import_utils.AST_NODE_TYPES.FunctionDeclaration) {
172
+ return true;
173
+ }
174
+ return ((_a = x.parent) == null ? void 0 : _a.type) === import_utils.AST_NODE_TYPES.VariableDeclarator && x.parent.id.type === import_utils.AST_NODE_TYPES.Identifier && ASTUtils.isNodeOfOneOf(x, [
175
+ import_utils.AST_NODE_TYPES.FunctionDeclaration,
176
+ import_utils.AST_NODE_TYPES.FunctionExpression,
177
+ import_utils.AST_NODE_TYPES.ArrowFunctionExpression
178
+ ]);
179
+ });
180
+ },
181
+ getReferencedExpressionByIdentifier(params) {
182
+ var _a, _b, _c;
183
+ const { node, context } = params;
184
+ const resolvedNode = (_c = (_b = (_a = context.getScope().references.find((ref) => ref.identifier === node)) == null ? void 0 : _a.resolved) == null ? void 0 : _b.defs[0]) == null ? void 0 : _c.node;
185
+ if ((resolvedNode == null ? void 0 : resolvedNode.type) !== import_utils.AST_NODE_TYPES.VariableDeclarator) {
186
+ return null;
187
+ }
188
+ return resolvedNode.init;
189
+ },
190
+ getNestedReturnStatements(node) {
191
+ const returnStatements = [];
192
+ if (node.type === import_utils.AST_NODE_TYPES.ReturnStatement) {
193
+ returnStatements.push(node);
194
+ }
195
+ if ("body" in node && node.body !== void 0 && node.body !== null) {
196
+ Array.isArray(node.body) ? node.body.forEach((x) => {
197
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
198
+ }) : returnStatements.push(
199
+ ...ASTUtils.getNestedReturnStatements(node.body)
200
+ );
201
+ }
202
+ if ("consequent" in node) {
203
+ Array.isArray(node.consequent) ? node.consequent.forEach((x) => {
204
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
205
+ }) : returnStatements.push(
206
+ ...ASTUtils.getNestedReturnStatements(node.consequent)
207
+ );
208
+ }
209
+ if ("alternate" in node && node.alternate !== null) {
210
+ Array.isArray(node.alternate) ? node.alternate.forEach((x) => {
211
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
212
+ }) : returnStatements.push(
213
+ ...ASTUtils.getNestedReturnStatements(node.alternate)
214
+ );
215
+ }
216
+ if ("cases" in node) {
217
+ node.cases.forEach((x) => {
218
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
219
+ });
220
+ }
221
+ if ("block" in node) {
222
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block));
223
+ }
224
+ if ("handler" in node && node.handler !== null) {
225
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler));
226
+ }
227
+ if ("finalizer" in node && node.finalizer !== null) {
228
+ returnStatements.push(
229
+ ...ASTUtils.getNestedReturnStatements(node.finalizer)
230
+ );
231
+ }
232
+ if ("expression" in node && node.expression !== true && node.expression !== false) {
233
+ returnStatements.push(
234
+ ...ASTUtils.getNestedReturnStatements(node.expression)
235
+ );
236
+ }
237
+ if ("test" in node && node.test !== null) {
238
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test));
239
+ }
240
+ return returnStatements;
241
+ }
242
+ };
243
+ // Annotate the CommonJS export names for ESM import in node:
244
+ 0 && (module.exports = {
245
+ ASTUtils
246
+ });
247
+ //# sourceMappingURL=ast-utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/ast-utils.ts"],"sourcesContent":["import { AST_NODE_TYPES } from '@typescript-eslint/utils'\nimport { uniqueBy } from './unique-by'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\nimport type TSESLintScopeManager from '@typescript-eslint/scope-manager'\nimport type { RuleContext } from '@typescript-eslint/utils/dist/ts-eslint'\n\nexport const ASTUtils = {\n isNodeOfOneOf<T extends AST_NODE_TYPES>(\n node: TSESTree.Node,\n types: readonly T[],\n ): node is TSESTree.Node & { type: T } {\n return types.includes(node.type as T)\n },\n isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {\n return node.type === AST_NODE_TYPES.Identifier\n },\n isIdentifierWithName(\n node: TSESTree.Node,\n name: string,\n ): node is TSESTree.Identifier {\n return ASTUtils.isIdentifier(node) && node.name === name\n },\n isIdentifierWithOneOfNames<T extends string[]>(\n node: TSESTree.Node,\n name: T,\n ): node is TSESTree.Identifier & { name: T[number] } {\n return ASTUtils.isIdentifier(node) && name.includes(node.name)\n },\n isProperty(node: TSESTree.Node): node is TSESTree.Property {\n return node.type === AST_NODE_TYPES.Property\n },\n isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression {\n return node.type === AST_NODE_TYPES.ObjectExpression\n },\n isPropertyWithIdentifierKey(\n node: TSESTree.Node,\n key: string,\n ): node is TSESTree.Property {\n return (\n ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key)\n )\n },\n findPropertyWithIdentifierKey(\n properties: TSESTree.ObjectLiteralElement[],\n key: string,\n ): TSESTree.Property | undefined {\n return properties.find((x) =>\n ASTUtils.isPropertyWithIdentifierKey(x, key),\n ) as TSESTree.Property | undefined\n },\n getNestedIdentifiers(node: TSESTree.Node): TSESTree.Identifier[] {\n const identifiers: TSESTree.Identifier[] = []\n\n if (ASTUtils.isIdentifier(node)) {\n identifiers.push(node)\n }\n\n if ('arguments' in node) {\n node.arguments.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('elements' in node) {\n node.elements.forEach((x) => {\n if (x !== null) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n }\n })\n }\n\n if ('properties' in node) {\n node.properties.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('expressions' in node) {\n node.expressions.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if (node.type === AST_NODE_TYPES.Property) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.value))\n }\n\n if (node.type === AST_NODE_TYPES.SpreadElement) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.MemberExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.object))\n }\n\n if (node.type === AST_NODE_TYPES.UnaryExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.ChainExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n if (node.type === AST_NODE_TYPES.TSNonNullExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n return identifiers\n },\n isAncestorIsCallee(identifier: TSESTree.Node) {\n let previousNode = identifier\n let currentNode = identifier.parent\n\n while (currentNode !== undefined) {\n if (\n currentNode.type === AST_NODE_TYPES.CallExpression &&\n currentNode.callee === previousNode\n ) {\n return true\n }\n\n if (currentNode.type !== AST_NODE_TYPES.MemberExpression) {\n return false\n }\n\n previousNode = currentNode\n currentNode = currentNode.parent\n }\n\n return false\n },\n traverseUpOnly(\n identifier: TSESTree.Node,\n allowedNodeTypes: AST_NODE_TYPES[],\n ): TSESTree.Node {\n const parent = identifier.parent\n\n if (parent !== undefined && allowedNodeTypes.includes(parent.type)) {\n return ASTUtils.traverseUpOnly(parent, allowedNodeTypes)\n }\n\n return identifier\n },\n isDeclaredInNode(params: {\n functionNode: TSESTree.Node\n reference: TSESLintScopeManager.Reference\n scopeManager: TSESLint.Scope.ScopeManager\n }) {\n const { functionNode, reference, scopeManager } = params\n const scope = scopeManager.acquire(functionNode)\n\n if (scope === null) {\n return false\n }\n\n return scope.set.has(reference.identifier.name)\n },\n getExternalRefs(params: {\n scopeManager: TSESLint.Scope.ScopeManager\n sourceCode: Readonly<TSESLint.SourceCode>\n node: TSESTree.Node\n }): TSESLint.Scope.Reference[] {\n const { scopeManager, sourceCode, node } = params\n const scope = scopeManager.acquire(node)\n\n if (scope === null) {\n return []\n }\n\n const references = scope.references\n .filter((x) => x.isRead() && !scope.set.has(x.identifier.name))\n .map((x) => {\n const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ])\n\n return {\n variable: x,\n node: referenceNode,\n text: sourceCode.getText(referenceNode),\n }\n })\n\n const localRefIds = new Set(\n [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0])),\n )\n\n const externalRefs = references.filter(\n (x) => x.variable.resolved === null || !localRefIds.has(x.text),\n )\n\n return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable)\n },\n mapKeyNodeToText(\n node: TSESTree.Node,\n sourceCode: Readonly<TSESLint.SourceCode>,\n ) {\n return sourceCode.getText(\n ASTUtils.traverseUpOnly(node, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ]),\n )\n },\n isValidReactComponentOrHookName(identifier: TSESTree.Identifier | null) {\n return identifier !== null && /^(use|[A-Z])/.test(identifier.name)\n },\n getFunctionAncestor(\n context: Readonly<RuleContext<string, readonly unknown[]>>,\n ) {\n return context.getAncestors().find((x) => {\n if (x.type === AST_NODE_TYPES.FunctionDeclaration) {\n return true\n }\n\n return (\n x.parent?.type === AST_NODE_TYPES.VariableDeclarator &&\n x.parent.id.type === AST_NODE_TYPES.Identifier &&\n ASTUtils.isNodeOfOneOf(x, [\n AST_NODE_TYPES.FunctionDeclaration,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ArrowFunctionExpression,\n ])\n )\n })\n },\n getReferencedExpressionByIdentifier(params: {\n node: TSESTree.Node\n context: Readonly<RuleContext<string, readonly unknown[]>>\n }) {\n const { node, context } = params\n\n const resolvedNode = context\n .getScope()\n .references.find((ref) => ref.identifier === node)?.resolved\n ?.defs[0]?.node\n\n if (resolvedNode?.type !== AST_NODE_TYPES.VariableDeclarator) {\n return null\n }\n\n return resolvedNode.init\n },\n getNestedReturnStatements(node: TSESTree.Node): TSESTree.ReturnStatement[] {\n const returnStatements: TSESTree.ReturnStatement[] = []\n\n if (node.type === AST_NODE_TYPES.ReturnStatement) {\n returnStatements.push(node)\n }\n\n if ('body' in node && node.body !== undefined && node.body !== null) {\n Array.isArray(node.body)\n ? node.body.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.body),\n )\n }\n\n if ('consequent' in node) {\n Array.isArray(node.consequent)\n ? node.consequent.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.consequent),\n )\n }\n\n if ('alternate' in node && node.alternate !== null) {\n Array.isArray(node.alternate)\n ? node.alternate.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.alternate),\n )\n }\n\n if ('cases' in node) {\n node.cases.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n }\n\n if ('block' in node) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block))\n }\n\n if ('handler' in node && node.handler !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler))\n }\n\n if ('finalizer' in node && node.finalizer !== null) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.finalizer),\n )\n }\n\n if (\n 'expression' in node &&\n node.expression !== true &&\n node.expression !== false\n ) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.expression),\n )\n }\n\n if ('test' in node && node.test !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test))\n }\n\n return returnStatements\n },\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA+B;AAC/B,uBAAyB;AAKlB,IAAM,WAAW;AAAA,EACtB,cACE,MACA,OACqC;AACrC,WAAO,MAAM,SAAS,KAAK,IAAS;AAAA,EACtC;AAAA,EACA,aAAa,MAAkD;AAC7D,WAAO,KAAK,SAAS,4BAAe;AAAA,EACtC;AAAA,EACA,qBACE,MACA,MAC6B;AAC7B,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS;AAAA,EACtD;AAAA,EACA,2BACE,MACA,MACmD;AACnD,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS,KAAK,IAAI;AAAA,EAC/D;AAAA,EACA,WAAW,MAAgD;AACzD,WAAO,KAAK,SAAS,4BAAe;AAAA,EACtC;AAAA,EACA,mBAAmB,MAAwD;AACzE,WAAO,KAAK,SAAS,4BAAe;AAAA,EACtC;AAAA,EACA,4BACE,MACA,KAC2B;AAC3B,WACE,SAAS,WAAW,IAAI,KAAK,SAAS,qBAAqB,KAAK,KAAK,GAAG;AAAA,EAE5E;AAAA,EACA,8BACE,YACA,KAC+B;AAC/B,WAAO,WAAW;AAAA,MAAK,CAAC,MACtB,SAAS,4BAA4B,GAAG,GAAG;AAAA,IAC7C;AAAA,EACF;AAAA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E,CAAC;AAAA,IACH;AAEA,QAAI,WAAW,MAAM;AACnB,uBAAiB,KAAK,GAAG,SAAS,0BAA0B,KAAK,KAAK,CAAC;AAAA,IACzE;AAEA,QAAI,aAAa,QAAQ,KAAK,YAAY,MAAM;AAC9C,uBAAiB,KAAK,GAAG,SAAS,0BAA0B,KAAK,OAAO,CAAC;AAAA,IAC3E;AAEA,QAAI,eAAe,QAAQ,KAAK,cAAc,MAAM;AAClD,uBAAiB;AAAA,QACf,GAAG,SAAS,0BAA0B,KAAK,SAAS;AAAA,MACtD;AAAA,IACF;AAEA,QACE,gBAAgB,QAChB,KAAK,eAAe,QACpB,KAAK,eAAe,OACpB;AACA,uBAAiB;AAAA,QACf,GAAG,SAAS,0BAA0B,KAAK,UAAU;AAAA,MACvD;AAAA,IACF;AAEA,QAAI,UAAU,QAAQ,KAAK,SAAS,MAAM;AACxC,uBAAiB,KAAK,GAAG,SAAS,0BAA0B,KAAK,IAAI,CAAC;AAAA,IACxE;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -0,0 +1,41 @@
1
+ import { AST_NODE_TYPES, TSESTree, TSESLint } from '@typescript-eslint/utils';
2
+ import TSESLintScopeManager from '@typescript-eslint/scope-manager';
3
+ import { RuleContext } from '@typescript-eslint/utils/dist/ts-eslint';
4
+
5
+ declare const ASTUtils: {
6
+ isNodeOfOneOf<T extends AST_NODE_TYPES>(node: TSESTree.Node, types: readonly T[]): node is TSESTree.Node & {
7
+ type: T;
8
+ };
9
+ isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier;
10
+ isIdentifierWithName(node: TSESTree.Node, name: string): node is TSESTree.Identifier;
11
+ isIdentifierWithOneOfNames<T_1 extends string[]>(node: TSESTree.Node, name: T_1): node is TSESTree.Identifier & {
12
+ name: T_1[number];
13
+ };
14
+ isProperty(node: TSESTree.Node): node is TSESTree.Property;
15
+ isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression;
16
+ isPropertyWithIdentifierKey(node: TSESTree.Node, key: string): node is TSESTree.Property;
17
+ findPropertyWithIdentifierKey(properties: TSESTree.ObjectLiteralElement[], key: string): TSESTree.Property | undefined;
18
+ getNestedIdentifiers(node: TSESTree.Node): TSESTree.Identifier[];
19
+ isAncestorIsCallee(identifier: TSESTree.Node): boolean;
20
+ traverseUpOnly(identifier: TSESTree.Node, allowedNodeTypes: AST_NODE_TYPES[]): TSESTree.Node;
21
+ isDeclaredInNode(params: {
22
+ functionNode: TSESTree.Node;
23
+ reference: TSESLintScopeManager.Reference;
24
+ scopeManager: TSESLint.Scope.ScopeManager;
25
+ }): boolean;
26
+ getExternalRefs(params: {
27
+ scopeManager: TSESLint.Scope.ScopeManager;
28
+ sourceCode: Readonly<TSESLint.SourceCode>;
29
+ node: TSESTree.Node;
30
+ }): TSESLint.Scope.Reference[];
31
+ mapKeyNodeToText(node: TSESTree.Node, sourceCode: Readonly<TSESLint.SourceCode>): string;
32
+ isValidReactComponentOrHookName(identifier: TSESTree.Identifier | null): boolean;
33
+ getFunctionAncestor(context: Readonly<RuleContext<string, readonly unknown[]>>): TSESTree.Node | undefined;
34
+ getReferencedExpressionByIdentifier(params: {
35
+ node: TSESTree.Node;
36
+ context: Readonly<RuleContext<string, readonly unknown[]>>;
37
+ }): TSESTree.Expression | null;
38
+ getNestedReturnStatements(node: TSESTree.Node): TSESTree.ReturnStatement[];
39
+ };
40
+
41
+ export { ASTUtils };
@@ -0,0 +1,41 @@
1
+ import { AST_NODE_TYPES, TSESTree, TSESLint } from '@typescript-eslint/utils';
2
+ import TSESLintScopeManager from '@typescript-eslint/scope-manager';
3
+ import { RuleContext } from '@typescript-eslint/utils/dist/ts-eslint';
4
+
5
+ declare const ASTUtils: {
6
+ isNodeOfOneOf<T extends AST_NODE_TYPES>(node: TSESTree.Node, types: readonly T[]): node is TSESTree.Node & {
7
+ type: T;
8
+ };
9
+ isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier;
10
+ isIdentifierWithName(node: TSESTree.Node, name: string): node is TSESTree.Identifier;
11
+ isIdentifierWithOneOfNames<T_1 extends string[]>(node: TSESTree.Node, name: T_1): node is TSESTree.Identifier & {
12
+ name: T_1[number];
13
+ };
14
+ isProperty(node: TSESTree.Node): node is TSESTree.Property;
15
+ isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression;
16
+ isPropertyWithIdentifierKey(node: TSESTree.Node, key: string): node is TSESTree.Property;
17
+ findPropertyWithIdentifierKey(properties: TSESTree.ObjectLiteralElement[], key: string): TSESTree.Property | undefined;
18
+ getNestedIdentifiers(node: TSESTree.Node): TSESTree.Identifier[];
19
+ isAncestorIsCallee(identifier: TSESTree.Node): boolean;
20
+ traverseUpOnly(identifier: TSESTree.Node, allowedNodeTypes: AST_NODE_TYPES[]): TSESTree.Node;
21
+ isDeclaredInNode(params: {
22
+ functionNode: TSESTree.Node;
23
+ reference: TSESLintScopeManager.Reference;
24
+ scopeManager: TSESLint.Scope.ScopeManager;
25
+ }): boolean;
26
+ getExternalRefs(params: {
27
+ scopeManager: TSESLint.Scope.ScopeManager;
28
+ sourceCode: Readonly<TSESLint.SourceCode>;
29
+ node: TSESTree.Node;
30
+ }): TSESLint.Scope.Reference[];
31
+ mapKeyNodeToText(node: TSESTree.Node, sourceCode: Readonly<TSESLint.SourceCode>): string;
32
+ isValidReactComponentOrHookName(identifier: TSESTree.Identifier | null): boolean;
33
+ getFunctionAncestor(context: Readonly<RuleContext<string, readonly unknown[]>>): TSESTree.Node | undefined;
34
+ getReferencedExpressionByIdentifier(params: {
35
+ node: TSESTree.Node;
36
+ context: Readonly<RuleContext<string, readonly unknown[]>>;
37
+ }): TSESTree.Expression | null;
38
+ getNestedReturnStatements(node: TSESTree.Node): TSESTree.ReturnStatement[];
39
+ };
40
+
41
+ export { ASTUtils };
@@ -0,0 +1,222 @@
1
+ // src/utils/ast-utils.ts
2
+ import { AST_NODE_TYPES } from "@typescript-eslint/utils";
3
+ import { uniqueBy } from "./unique-by.js";
4
+ var ASTUtils = {
5
+ isNodeOfOneOf(node, types) {
6
+ return types.includes(node.type);
7
+ },
8
+ isIdentifier(node) {
9
+ return node.type === AST_NODE_TYPES.Identifier;
10
+ },
11
+ isIdentifierWithName(node, name) {
12
+ return ASTUtils.isIdentifier(node) && node.name === name;
13
+ },
14
+ isIdentifierWithOneOfNames(node, name) {
15
+ return ASTUtils.isIdentifier(node) && name.includes(node.name);
16
+ },
17
+ isProperty(node) {
18
+ return node.type === AST_NODE_TYPES.Property;
19
+ },
20
+ isObjectExpression(node) {
21
+ return node.type === AST_NODE_TYPES.ObjectExpression;
22
+ },
23
+ isPropertyWithIdentifierKey(node, key) {
24
+ return ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key);
25
+ },
26
+ findPropertyWithIdentifierKey(properties, key) {
27
+ return properties.find(
28
+ (x) => ASTUtils.isPropertyWithIdentifierKey(x, key)
29
+ );
30
+ },
31
+ getNestedIdentifiers(node) {
32
+ const identifiers = [];
33
+ if (ASTUtils.isIdentifier(node)) {
34
+ identifiers.push(node);
35
+ }
36
+ if ("arguments" in node) {
37
+ node.arguments.forEach((x) => {
38
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
39
+ });
40
+ }
41
+ if ("elements" in node) {
42
+ node.elements.forEach((x) => {
43
+ if (x !== null) {
44
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
45
+ }
46
+ });
47
+ }
48
+ if ("properties" in node) {
49
+ node.properties.forEach((x) => {
50
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
51
+ });
52
+ }
53
+ if ("expressions" in node) {
54
+ node.expressions.forEach((x) => {
55
+ identifiers.push(...ASTUtils.getNestedIdentifiers(x));
56
+ });
57
+ }
58
+ if (node.type === AST_NODE_TYPES.Property) {
59
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.value));
60
+ }
61
+ if (node.type === AST_NODE_TYPES.SpreadElement) {
62
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
63
+ }
64
+ if (node.type === AST_NODE_TYPES.MemberExpression) {
65
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.object));
66
+ }
67
+ if (node.type === AST_NODE_TYPES.UnaryExpression) {
68
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
69
+ }
70
+ if (node.type === AST_NODE_TYPES.ChainExpression) {
71
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
72
+ }
73
+ if (node.type === AST_NODE_TYPES.TSNonNullExpression) {
74
+ identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
75
+ }
76
+ return identifiers;
77
+ },
78
+ isAncestorIsCallee(identifier) {
79
+ let previousNode = identifier;
80
+ let currentNode = identifier.parent;
81
+ while (currentNode !== void 0) {
82
+ if (currentNode.type === AST_NODE_TYPES.CallExpression && currentNode.callee === previousNode) {
83
+ return true;
84
+ }
85
+ if (currentNode.type !== AST_NODE_TYPES.MemberExpression) {
86
+ return false;
87
+ }
88
+ previousNode = currentNode;
89
+ currentNode = currentNode.parent;
90
+ }
91
+ return false;
92
+ },
93
+ traverseUpOnly(identifier, allowedNodeTypes) {
94
+ const parent = identifier.parent;
95
+ if (parent !== void 0 && allowedNodeTypes.includes(parent.type)) {
96
+ return ASTUtils.traverseUpOnly(parent, allowedNodeTypes);
97
+ }
98
+ return identifier;
99
+ },
100
+ isDeclaredInNode(params) {
101
+ const { functionNode, reference, scopeManager } = params;
102
+ const scope = scopeManager.acquire(functionNode);
103
+ if (scope === null) {
104
+ return false;
105
+ }
106
+ return scope.set.has(reference.identifier.name);
107
+ },
108
+ getExternalRefs(params) {
109
+ const { scopeManager, sourceCode, node } = params;
110
+ const scope = scopeManager.acquire(node);
111
+ if (scope === null) {
112
+ return [];
113
+ }
114
+ const references = scope.references.filter((x) => x.isRead() && !scope.set.has(x.identifier.name)).map((x) => {
115
+ const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [
116
+ AST_NODE_TYPES.MemberExpression,
117
+ AST_NODE_TYPES.Identifier
118
+ ]);
119
+ return {
120
+ variable: x,
121
+ node: referenceNode,
122
+ text: sourceCode.getText(referenceNode)
123
+ };
124
+ });
125
+ const localRefIds = new Set(
126
+ [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0]))
127
+ );
128
+ const externalRefs = references.filter(
129
+ (x) => x.variable.resolved === null || !localRefIds.has(x.text)
130
+ );
131
+ return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable);
132
+ },
133
+ mapKeyNodeToText(node, sourceCode) {
134
+ return sourceCode.getText(
135
+ ASTUtils.traverseUpOnly(node, [
136
+ AST_NODE_TYPES.MemberExpression,
137
+ AST_NODE_TYPES.Identifier
138
+ ])
139
+ );
140
+ },
141
+ isValidReactComponentOrHookName(identifier) {
142
+ return identifier !== null && /^(use|[A-Z])/.test(identifier.name);
143
+ },
144
+ getFunctionAncestor(context) {
145
+ return context.getAncestors().find((x) => {
146
+ var _a;
147
+ if (x.type === AST_NODE_TYPES.FunctionDeclaration) {
148
+ return true;
149
+ }
150
+ return ((_a = x.parent) == null ? void 0 : _a.type) === AST_NODE_TYPES.VariableDeclarator && x.parent.id.type === AST_NODE_TYPES.Identifier && ASTUtils.isNodeOfOneOf(x, [
151
+ AST_NODE_TYPES.FunctionDeclaration,
152
+ AST_NODE_TYPES.FunctionExpression,
153
+ AST_NODE_TYPES.ArrowFunctionExpression
154
+ ]);
155
+ });
156
+ },
157
+ getReferencedExpressionByIdentifier(params) {
158
+ var _a, _b, _c;
159
+ const { node, context } = params;
160
+ const resolvedNode = (_c = (_b = (_a = context.getScope().references.find((ref) => ref.identifier === node)) == null ? void 0 : _a.resolved) == null ? void 0 : _b.defs[0]) == null ? void 0 : _c.node;
161
+ if ((resolvedNode == null ? void 0 : resolvedNode.type) !== AST_NODE_TYPES.VariableDeclarator) {
162
+ return null;
163
+ }
164
+ return resolvedNode.init;
165
+ },
166
+ getNestedReturnStatements(node) {
167
+ const returnStatements = [];
168
+ if (node.type === AST_NODE_TYPES.ReturnStatement) {
169
+ returnStatements.push(node);
170
+ }
171
+ if ("body" in node && node.body !== void 0 && node.body !== null) {
172
+ Array.isArray(node.body) ? node.body.forEach((x) => {
173
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
174
+ }) : returnStatements.push(
175
+ ...ASTUtils.getNestedReturnStatements(node.body)
176
+ );
177
+ }
178
+ if ("consequent" in node) {
179
+ Array.isArray(node.consequent) ? node.consequent.forEach((x) => {
180
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
181
+ }) : returnStatements.push(
182
+ ...ASTUtils.getNestedReturnStatements(node.consequent)
183
+ );
184
+ }
185
+ if ("alternate" in node && node.alternate !== null) {
186
+ Array.isArray(node.alternate) ? node.alternate.forEach((x) => {
187
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
188
+ }) : returnStatements.push(
189
+ ...ASTUtils.getNestedReturnStatements(node.alternate)
190
+ );
191
+ }
192
+ if ("cases" in node) {
193
+ node.cases.forEach((x) => {
194
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(x));
195
+ });
196
+ }
197
+ if ("block" in node) {
198
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block));
199
+ }
200
+ if ("handler" in node && node.handler !== null) {
201
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler));
202
+ }
203
+ if ("finalizer" in node && node.finalizer !== null) {
204
+ returnStatements.push(
205
+ ...ASTUtils.getNestedReturnStatements(node.finalizer)
206
+ );
207
+ }
208
+ if ("expression" in node && node.expression !== true && node.expression !== false) {
209
+ returnStatements.push(
210
+ ...ASTUtils.getNestedReturnStatements(node.expression)
211
+ );
212
+ }
213
+ if ("test" in node && node.test !== null) {
214
+ returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test));
215
+ }
216
+ return returnStatements;
217
+ }
218
+ };
219
+ export {
220
+ ASTUtils
221
+ };
222
+ //# sourceMappingURL=ast-utils.js.map