@eslint-react/ast 1.52.3-next.8 → 2.0.0-beta.0
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.
- package/dist/index.d.ts +40 -43
- package/dist/index.js +223 -264
- package/package.json +6 -14
- package/dist/index.d.mts +0 -284
- package/dist/index.mjs +0 -589
package/dist/index.js
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var typescriptEstree = require('@typescript-eslint/typescript-estree');
|
|
7
|
-
var stringTs = require('string-ts');
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
2
|
+
import { or, dual, unit } from '@eslint-react/eff';
|
|
3
|
+
import { ASTUtils } from '@typescript-eslint/utils';
|
|
4
|
+
import { simpleTraverse } from '@typescript-eslint/typescript-estree';
|
|
5
|
+
import { toLowerCase, delimiterCase, replace } from 'string-ts';
|
|
8
6
|
|
|
9
7
|
// src/ast-array-method.ts
|
|
10
8
|
function isArrayFromCall(node, loose = true) {
|
|
11
|
-
if (node.type !==
|
|
12
|
-
if (node.callee.type !==
|
|
13
|
-
if (node.callee.property.type !==
|
|
9
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
10
|
+
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) return false;
|
|
11
|
+
if (node.callee.property.type !== AST_NODE_TYPES.Identifier) return false;
|
|
14
12
|
const name = node.callee.property.name;
|
|
15
13
|
return name === "from" || loose && name.startsWith("from");
|
|
16
14
|
}
|
|
17
15
|
function isArrayMapCall(node, loose = true) {
|
|
18
|
-
if (node.type !==
|
|
19
|
-
if (node.callee.type !==
|
|
20
|
-
if (node.callee.property.type !==
|
|
16
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
17
|
+
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) return false;
|
|
18
|
+
if (node.callee.property.type !== AST_NODE_TYPES.Identifier) return false;
|
|
21
19
|
const name = node.callee.property.name;
|
|
22
20
|
return name === "map" || loose && name.endsWith("Map");
|
|
23
21
|
}
|
|
@@ -42,153 +40,153 @@ function getArrayMethodCallbackIndexParamPosition(methodName) {
|
|
|
42
40
|
}
|
|
43
41
|
function getClassId(node) {
|
|
44
42
|
if (node.id != null) return node.id;
|
|
45
|
-
if (node.parent.type ===
|
|
43
|
+
if (node.parent.type === AST_NODE_TYPES.VariableDeclarator && node.parent.id.type === AST_NODE_TYPES.Identifier) {
|
|
46
44
|
return node.parent.id;
|
|
47
45
|
}
|
|
48
|
-
return
|
|
46
|
+
return unit;
|
|
49
47
|
}
|
|
50
|
-
var is =
|
|
51
|
-
var isOneOf =
|
|
48
|
+
var is = ASTUtils.isNodeOfType;
|
|
49
|
+
var isOneOf = ASTUtils.isNodeOfTypes;
|
|
52
50
|
var isFunction = isOneOf([
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
52
|
+
AST_NODE_TYPES.FunctionDeclaration,
|
|
53
|
+
AST_NODE_TYPES.FunctionExpression
|
|
56
54
|
]);
|
|
57
55
|
var isFunctionType = isOneOf([
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
57
|
+
AST_NODE_TYPES.FunctionDeclaration,
|
|
58
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
59
|
+
AST_NODE_TYPES.TSCallSignatureDeclaration,
|
|
60
|
+
AST_NODE_TYPES.TSConstructSignatureDeclaration,
|
|
61
|
+
AST_NODE_TYPES.TSDeclareFunction,
|
|
62
|
+
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
|
|
63
|
+
AST_NODE_TYPES.TSFunctionType,
|
|
64
|
+
AST_NODE_TYPES.TSMethodSignature
|
|
67
65
|
]);
|
|
68
|
-
var isClass = isOneOf([
|
|
66
|
+
var isClass = isOneOf([AST_NODE_TYPES.ClassDeclaration, AST_NODE_TYPES.ClassExpression]);
|
|
69
67
|
var isMethodOrProperty = isOneOf([
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
AST_NODE_TYPES.PropertyDefinition,
|
|
69
|
+
AST_NODE_TYPES.MethodDefinition
|
|
72
70
|
]);
|
|
73
71
|
var isLoop = isOneOf([
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
AST_NODE_TYPES.DoWhileStatement,
|
|
73
|
+
AST_NODE_TYPES.ForInStatement,
|
|
74
|
+
AST_NODE_TYPES.ForOfStatement,
|
|
75
|
+
AST_NODE_TYPES.ForStatement,
|
|
76
|
+
AST_NODE_TYPES.WhileStatement
|
|
79
77
|
]);
|
|
80
|
-
var isControlFlow =
|
|
78
|
+
var isControlFlow = or(
|
|
81
79
|
isLoop,
|
|
82
80
|
isOneOf([
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
AST_NODE_TYPES.IfStatement,
|
|
82
|
+
AST_NODE_TYPES.SwitchStatement
|
|
85
83
|
])
|
|
86
84
|
);
|
|
87
|
-
var isConditional =
|
|
85
|
+
var isConditional = or(
|
|
88
86
|
isControlFlow,
|
|
89
87
|
isOneOf([
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
AST_NODE_TYPES.LogicalExpression,
|
|
89
|
+
AST_NODE_TYPES.ConditionalExpression
|
|
92
90
|
])
|
|
93
91
|
);
|
|
94
|
-
var isArrayTupleType = isOneOf([
|
|
92
|
+
var isArrayTupleType = isOneOf([AST_NODE_TYPES.TSArrayType, AST_NODE_TYPES.TSTupleType]);
|
|
95
93
|
var isProperty = isOneOf([
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
94
|
+
AST_NODE_TYPES.PropertyDefinition,
|
|
95
|
+
AST_NODE_TYPES.TSIndexSignature,
|
|
96
|
+
AST_NODE_TYPES.TSParameterProperty,
|
|
97
|
+
AST_NODE_TYPES.TSPropertySignature
|
|
100
98
|
]);
|
|
101
|
-
var isJSXElement = is(
|
|
102
|
-
var isJSXFragment = is(
|
|
99
|
+
var isJSXElement = is(AST_NODE_TYPES.JSXElement);
|
|
100
|
+
var isJSXFragment = is(AST_NODE_TYPES.JSXFragment);
|
|
103
101
|
var isJSXTagNameExpression = isOneOf([
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
102
|
+
AST_NODE_TYPES.JSXIdentifier,
|
|
103
|
+
AST_NODE_TYPES.JSXMemberExpression,
|
|
104
|
+
AST_NODE_TYPES.JSXNamespacedName
|
|
107
105
|
]);
|
|
108
106
|
var isJSX = isOneOf([
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
107
|
+
AST_NODE_TYPES.JSXAttribute,
|
|
108
|
+
AST_NODE_TYPES.JSXClosingElement,
|
|
109
|
+
AST_NODE_TYPES.JSXClosingFragment,
|
|
110
|
+
AST_NODE_TYPES.JSXElement,
|
|
111
|
+
AST_NODE_TYPES.JSXEmptyExpression,
|
|
112
|
+
AST_NODE_TYPES.JSXExpressionContainer,
|
|
113
|
+
AST_NODE_TYPES.JSXFragment,
|
|
114
|
+
AST_NODE_TYPES.JSXIdentifier,
|
|
115
|
+
AST_NODE_TYPES.JSXMemberExpression,
|
|
116
|
+
AST_NODE_TYPES.JSXNamespacedName,
|
|
117
|
+
AST_NODE_TYPES.JSXOpeningElement,
|
|
118
|
+
AST_NODE_TYPES.JSXOpeningFragment,
|
|
119
|
+
AST_NODE_TYPES.JSXSpreadAttribute,
|
|
120
|
+
AST_NODE_TYPES.JSXSpreadChild,
|
|
121
|
+
AST_NODE_TYPES.JSXText
|
|
124
122
|
]);
|
|
125
123
|
var isDestructuringPattern = isOneOf([
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
124
|
+
AST_NODE_TYPES.ArrayPattern,
|
|
125
|
+
AST_NODE_TYPES.AssignmentPattern,
|
|
126
|
+
AST_NODE_TYPES.ObjectPattern,
|
|
127
|
+
AST_NODE_TYPES.RestElement
|
|
130
128
|
]);
|
|
131
129
|
var isTypeDeclaration = isOneOf([
|
|
132
|
-
|
|
133
|
-
|
|
130
|
+
AST_NODE_TYPES.TSInterfaceDeclaration,
|
|
131
|
+
AST_NODE_TYPES.TSTypeAliasDeclaration
|
|
134
132
|
]);
|
|
135
133
|
var isLeftHandSideExpression = isOneOf([
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
134
|
+
AST_NODE_TYPES.ArrayExpression,
|
|
135
|
+
AST_NODE_TYPES.ArrayPattern,
|
|
136
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
137
|
+
AST_NODE_TYPES.CallExpression,
|
|
138
|
+
AST_NODE_TYPES.ClassExpression,
|
|
139
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
140
|
+
AST_NODE_TYPES.Identifier,
|
|
141
|
+
AST_NODE_TYPES.JSXElement,
|
|
142
|
+
AST_NODE_TYPES.JSXFragment,
|
|
143
|
+
AST_NODE_TYPES.Literal,
|
|
144
|
+
AST_NODE_TYPES.TemplateLiteral,
|
|
145
|
+
AST_NODE_TYPES.MemberExpression,
|
|
146
|
+
AST_NODE_TYPES.MetaProperty,
|
|
147
|
+
AST_NODE_TYPES.ObjectExpression,
|
|
148
|
+
AST_NODE_TYPES.ObjectPattern,
|
|
149
|
+
AST_NODE_TYPES.SequenceExpression,
|
|
150
|
+
AST_NODE_TYPES.Super,
|
|
151
|
+
AST_NODE_TYPES.TaggedTemplateExpression,
|
|
152
|
+
AST_NODE_TYPES.ThisExpression
|
|
155
153
|
]);
|
|
156
154
|
var isLeftHandSideExpressionType = isOneOf([
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
155
|
+
AST_NODE_TYPES.ArrayExpression,
|
|
156
|
+
AST_NODE_TYPES.ArrayPattern,
|
|
157
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
158
|
+
AST_NODE_TYPES.CallExpression,
|
|
159
|
+
AST_NODE_TYPES.ClassExpression,
|
|
160
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
161
|
+
AST_NODE_TYPES.Identifier,
|
|
162
|
+
AST_NODE_TYPES.JSXElement,
|
|
163
|
+
AST_NODE_TYPES.JSXFragment,
|
|
164
|
+
AST_NODE_TYPES.Literal,
|
|
165
|
+
AST_NODE_TYPES.TemplateLiteral,
|
|
166
|
+
AST_NODE_TYPES.MemberExpression,
|
|
167
|
+
AST_NODE_TYPES.MetaProperty,
|
|
168
|
+
AST_NODE_TYPES.ObjectExpression,
|
|
169
|
+
AST_NODE_TYPES.ObjectPattern,
|
|
170
|
+
AST_NODE_TYPES.SequenceExpression,
|
|
171
|
+
AST_NODE_TYPES.Super,
|
|
172
|
+
AST_NODE_TYPES.TaggedTemplateExpression,
|
|
173
|
+
AST_NODE_TYPES.ThisExpression,
|
|
174
|
+
AST_NODE_TYPES.TSAsExpression,
|
|
175
|
+
AST_NODE_TYPES.TSNonNullExpression,
|
|
176
|
+
AST_NODE_TYPES.TSTypeAssertion
|
|
179
177
|
]);
|
|
180
178
|
var isTypeExpression = isOneOf([
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
179
|
+
AST_NODE_TYPES.TSAsExpression,
|
|
180
|
+
AST_NODE_TYPES.TSTypeAssertion,
|
|
181
|
+
AST_NODE_TYPES.TSNonNullExpression,
|
|
182
|
+
AST_NODE_TYPES.TSSatisfiesExpression,
|
|
183
|
+
AST_NODE_TYPES.TSInstantiationExpression
|
|
186
184
|
]);
|
|
187
185
|
var isTypeAssertionExpression = isOneOf([
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
186
|
+
AST_NODE_TYPES.TSAsExpression,
|
|
187
|
+
AST_NODE_TYPES.TSTypeAssertion,
|
|
188
|
+
AST_NODE_TYPES.TSNonNullExpression,
|
|
189
|
+
AST_NODE_TYPES.TSSatisfiesExpression
|
|
192
190
|
]);
|
|
193
191
|
|
|
194
192
|
// src/ast-expression.ts
|
|
@@ -199,7 +197,7 @@ function getJSExpression(node) {
|
|
|
199
197
|
return node;
|
|
200
198
|
}
|
|
201
199
|
function isThisExpression(node) {
|
|
202
|
-
return getJSExpression(node).type ===
|
|
200
|
+
return getJSExpression(node).type === AST_NODE_TYPES.ThisExpression;
|
|
203
201
|
}
|
|
204
202
|
function getFunctionId(node) {
|
|
205
203
|
switch (true) {
|
|
@@ -207,24 +205,24 @@ function getFunctionId(node) {
|
|
|
207
205
|
case ("id" in node && node.id != null):
|
|
208
206
|
return node.id;
|
|
209
207
|
// const whatever = function MaybeComponent() {};
|
|
210
|
-
case (node.parent.type ===
|
|
208
|
+
case (node.parent.type === AST_NODE_TYPES.VariableDeclarator && node.parent.init === node && node.parent.id.type === AST_NODE_TYPES.Identifier):
|
|
211
209
|
return node.parent.id;
|
|
212
210
|
// MaybeComponent = () => {};
|
|
213
|
-
case (node.parent.type ===
|
|
211
|
+
case (node.parent.type === AST_NODE_TYPES.AssignmentExpression && node.parent.right === node && node.parent.operator === "=" && node.parent.left.type === AST_NODE_TYPES.Identifier):
|
|
214
212
|
return node.parent.left;
|
|
215
213
|
// {MaybeComponent: () => {}}
|
|
216
214
|
// {MaybeComponent() {}}
|
|
217
|
-
case (node.parent.type ===
|
|
215
|
+
case (node.parent.type === AST_NODE_TYPES.Property && node.parent.value === node && !node.parent.computed && node.parent.key.type === AST_NODE_TYPES.Identifier):
|
|
218
216
|
return node.parent.key;
|
|
219
217
|
// class {MaybeComponent = () => {}}
|
|
220
218
|
// class {MaybeComponent() {}}
|
|
221
|
-
case (isMethodOrProperty(node.parent) && node.parent.value === node && node.parent.key.type ===
|
|
219
|
+
case (isMethodOrProperty(node.parent) && node.parent.value === node && node.parent.key.type === AST_NODE_TYPES.Identifier):
|
|
222
220
|
return node.parent.key;
|
|
223
221
|
// Follow spec convention for `IsAnonymousFunctionDefinition()` usage.
|
|
224
222
|
//
|
|
225
223
|
// const {MaybeComponent = () => {}} = {};
|
|
226
224
|
// ({MaybeComponent = () => {}} = {});
|
|
227
|
-
case (node.parent.type ===
|
|
225
|
+
case (node.parent.type === AST_NODE_TYPES.AssignmentPattern && node.parent.right === node && node.parent.left.type === AST_NODE_TYPES.Identifier):
|
|
228
226
|
return node.parent.left;
|
|
229
227
|
// const MaybeComponent = (() => {})!;
|
|
230
228
|
// const MaybeComponent = (() => {}) as FunctionComponent;
|
|
@@ -232,38 +230,38 @@ function getFunctionId(node) {
|
|
|
232
230
|
case isTypeAssertionExpression(node.parent):
|
|
233
231
|
return getFunctionId(node.parent);
|
|
234
232
|
}
|
|
235
|
-
return
|
|
233
|
+
return unit;
|
|
236
234
|
}
|
|
237
235
|
function getFunctionInitPath(node) {
|
|
238
|
-
if (node.type ===
|
|
236
|
+
if (node.type === AST_NODE_TYPES.FunctionDeclaration) {
|
|
239
237
|
return [node];
|
|
240
238
|
}
|
|
241
239
|
const { parent } = node;
|
|
242
240
|
switch (true) {
|
|
243
|
-
case parent.type ===
|
|
241
|
+
case parent.type === AST_NODE_TYPES.VariableDeclarator:
|
|
244
242
|
return [parent.parent, parent, node];
|
|
245
|
-
case (parent.type ===
|
|
243
|
+
case (parent.type === AST_NODE_TYPES.CallExpression && parent.parent.type === AST_NODE_TYPES.VariableDeclarator):
|
|
246
244
|
return [parent.parent.parent, parent.parent, parent, node];
|
|
247
|
-
case (parent.type ===
|
|
245
|
+
case (parent.type === AST_NODE_TYPES.CallExpression && parent.parent.type === AST_NODE_TYPES.CallExpression && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator):
|
|
248
246
|
return [parent.parent.parent.parent, parent.parent.parent, parent.parent, parent, node];
|
|
249
|
-
case (parent.type ===
|
|
247
|
+
case (parent.type === AST_NODE_TYPES.Property && parent.parent.type === AST_NODE_TYPES.ObjectExpression && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator):
|
|
250
248
|
return [parent.parent.parent.parent, parent.parent.parent, parent.parent, parent, node];
|
|
251
|
-
case (parent.type ===
|
|
249
|
+
case (parent.type === AST_NODE_TYPES.MethodDefinition && parent.parent.parent.type === AST_NODE_TYPES.ClassDeclaration):
|
|
252
250
|
return [parent.parent.parent, parent.parent, parent, node];
|
|
253
|
-
case (parent.type ===
|
|
251
|
+
case (parent.type === AST_NODE_TYPES.PropertyDefinition && parent.parent.parent.type === AST_NODE_TYPES.ClassDeclaration):
|
|
254
252
|
return [parent.parent.parent, parent.parent, parent, node];
|
|
255
253
|
}
|
|
256
|
-
return
|
|
254
|
+
return unit;
|
|
257
255
|
}
|
|
258
256
|
function hasCallInFunctionInitPath(callName, initPath) {
|
|
259
257
|
return initPath.some((n) => {
|
|
260
|
-
if (n.type !==
|
|
258
|
+
if (n.type !== AST_NODE_TYPES.CallExpression) {
|
|
261
259
|
return false;
|
|
262
260
|
}
|
|
263
261
|
switch (n.callee.type) {
|
|
264
|
-
case
|
|
262
|
+
case AST_NODE_TYPES.Identifier:
|
|
265
263
|
return n.callee.name === callName;
|
|
266
|
-
case
|
|
264
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
267
265
|
return "name" in n.callee.property && n.callee.property.name === callName;
|
|
268
266
|
default:
|
|
269
267
|
return false;
|
|
@@ -271,22 +269,25 @@ function hasCallInFunctionInitPath(callName, initPath) {
|
|
|
271
269
|
});
|
|
272
270
|
}
|
|
273
271
|
function isEmptyFunction(node) {
|
|
274
|
-
return node.body.type ===
|
|
272
|
+
return node.body.type === AST_NODE_TYPES.BlockStatement && node.body.body.length === 0;
|
|
273
|
+
}
|
|
274
|
+
function isImmediatelyInvokedFunction(node) {
|
|
275
|
+
return node.type !== AST_NODE_TYPES.FunctionDeclaration && node.parent.type === AST_NODE_TYPES.CallExpression && node.parent.callee === node;
|
|
275
276
|
}
|
|
276
277
|
function findParentNode(node, test) {
|
|
277
|
-
if (node == null) return
|
|
278
|
+
if (node == null) return unit;
|
|
278
279
|
let parent = node.parent;
|
|
279
|
-
while (parent != null && parent.type !==
|
|
280
|
+
while (parent != null && parent.type !== AST_NODE_TYPES.Program) {
|
|
280
281
|
if (test(parent)) {
|
|
281
282
|
return parent;
|
|
282
283
|
}
|
|
283
284
|
parent = parent.parent;
|
|
284
285
|
}
|
|
285
|
-
return
|
|
286
|
+
return unit;
|
|
286
287
|
}
|
|
287
288
|
function getNestedIdentifiers(node) {
|
|
288
289
|
const identifiers = [];
|
|
289
|
-
if (node.type ===
|
|
290
|
+
if (node.type === AST_NODE_TYPES.Identifier) {
|
|
290
291
|
identifiers.push(node);
|
|
291
292
|
}
|
|
292
293
|
if ("arguments" in node) {
|
|
@@ -313,35 +314,35 @@ function getNestedIdentifiers(node) {
|
|
|
313
314
|
const chunk = getNestedIdentifiers(node.right);
|
|
314
315
|
identifiers.push(...chunk);
|
|
315
316
|
}
|
|
316
|
-
if (node.type ===
|
|
317
|
+
if (node.type === AST_NODE_TYPES.Property) {
|
|
317
318
|
const chunk = getNestedIdentifiers(node.value);
|
|
318
319
|
identifiers.push(...chunk);
|
|
319
320
|
}
|
|
320
|
-
if (node.type ===
|
|
321
|
+
if (node.type === AST_NODE_TYPES.SpreadElement) {
|
|
321
322
|
const chunk = getNestedIdentifiers(node.argument);
|
|
322
323
|
identifiers.push(...chunk);
|
|
323
324
|
}
|
|
324
|
-
if (node.type ===
|
|
325
|
+
if (node.type === AST_NODE_TYPES.MemberExpression) {
|
|
325
326
|
const chunk = getNestedIdentifiers(node.object);
|
|
326
327
|
identifiers.push(...chunk);
|
|
327
328
|
}
|
|
328
|
-
if (node.type ===
|
|
329
|
+
if (node.type === AST_NODE_TYPES.UnaryExpression) {
|
|
329
330
|
const chunk = getNestedIdentifiers(node.argument);
|
|
330
331
|
identifiers.push(...chunk);
|
|
331
332
|
}
|
|
332
|
-
if (node.type ===
|
|
333
|
+
if (node.type === AST_NODE_TYPES.ChainExpression) {
|
|
333
334
|
const chunk = getNestedIdentifiers(node.expression);
|
|
334
335
|
identifiers.push(...chunk);
|
|
335
336
|
}
|
|
336
|
-
if (node.type ===
|
|
337
|
+
if (node.type === AST_NODE_TYPES.TSNonNullExpression) {
|
|
337
338
|
const chunk = getNestedIdentifiers(node.expression);
|
|
338
339
|
identifiers.push(...chunk);
|
|
339
340
|
}
|
|
340
|
-
if (node.type ===
|
|
341
|
+
if (node.type === AST_NODE_TYPES.TSAsExpression) {
|
|
341
342
|
const chunk = getNestedIdentifiers(node.expression);
|
|
342
343
|
identifiers.push(...chunk);
|
|
343
344
|
}
|
|
344
|
-
if (node.type ===
|
|
345
|
+
if (node.type === AST_NODE_TYPES.TSSatisfiesExpression) {
|
|
345
346
|
const chunk = getNestedIdentifiers(node.expression);
|
|
346
347
|
identifiers.push(...chunk);
|
|
347
348
|
}
|
|
@@ -350,9 +351,9 @@ function getNestedIdentifiers(node) {
|
|
|
350
351
|
function getNestedReturnStatements(node) {
|
|
351
352
|
const statements = [];
|
|
352
353
|
const boundaryNode = isFunction(node) ? node : findParentNode(node, isFunction);
|
|
353
|
-
|
|
354
|
+
simpleTraverse(node, {
|
|
354
355
|
enter(node2) {
|
|
355
|
-
if (node2.type !==
|
|
356
|
+
if (node2.type !== AST_NODE_TYPES.ReturnStatement) {
|
|
356
357
|
return;
|
|
357
358
|
}
|
|
358
359
|
const parentFunction = findParentNode(node2, isFunction);
|
|
@@ -412,51 +413,51 @@ function getNestedExpressionsOfType(type) {
|
|
|
412
413
|
const chunk = node.expressions.map(boundGetNestedExpressionsOfType).flat(1);
|
|
413
414
|
expressions.push(...chunk);
|
|
414
415
|
}
|
|
415
|
-
if (node.type ===
|
|
416
|
+
if (node.type === AST_NODE_TYPES.Property) {
|
|
416
417
|
const chunk = boundGetNestedExpressionsOfType(node.value);
|
|
417
418
|
expressions.push(...chunk);
|
|
418
419
|
}
|
|
419
|
-
if (node.type ===
|
|
420
|
+
if (node.type === AST_NODE_TYPES.SpreadElement) {
|
|
420
421
|
const chunk = boundGetNestedExpressionsOfType(node.argument);
|
|
421
422
|
expressions.push(...chunk);
|
|
422
423
|
}
|
|
423
|
-
if (node.type ===
|
|
424
|
+
if (node.type === AST_NODE_TYPES.MemberExpression) {
|
|
424
425
|
const chunk = boundGetNestedExpressionsOfType(node.object);
|
|
425
426
|
expressions.push(...chunk);
|
|
426
427
|
}
|
|
427
|
-
if (node.type ===
|
|
428
|
+
if (node.type === AST_NODE_TYPES.UnaryExpression) {
|
|
428
429
|
const chunk = boundGetNestedExpressionsOfType(node.argument);
|
|
429
430
|
expressions.push(...chunk);
|
|
430
431
|
}
|
|
431
|
-
if (node.type ===
|
|
432
|
+
if (node.type === AST_NODE_TYPES.ChainExpression) {
|
|
432
433
|
const chunk = boundGetNestedExpressionsOfType(node.expression);
|
|
433
434
|
expressions.push(...chunk);
|
|
434
435
|
}
|
|
435
|
-
if (node.type ===
|
|
436
|
+
if (node.type === AST_NODE_TYPES.TSNonNullExpression) {
|
|
436
437
|
const chunk = boundGetNestedExpressionsOfType(node.expression);
|
|
437
438
|
expressions.push(...chunk);
|
|
438
439
|
}
|
|
439
|
-
if (node.type ===
|
|
440
|
+
if (node.type === AST_NODE_TYPES.TSAsExpression) {
|
|
440
441
|
const chunk = boundGetNestedExpressionsOfType(node.expression);
|
|
441
442
|
expressions.push(...chunk);
|
|
442
443
|
}
|
|
443
|
-
if (node.type ===
|
|
444
|
+
if (node.type === AST_NODE_TYPES.TSSatisfiesExpression) {
|
|
444
445
|
const chunk = boundGetNestedExpressionsOfType(node.expression);
|
|
445
446
|
expressions.push(...chunk);
|
|
446
447
|
}
|
|
447
448
|
return expressions;
|
|
448
449
|
};
|
|
449
450
|
}
|
|
450
|
-
var getNestedNewExpressions = getNestedExpressionsOfType(
|
|
451
|
-
var getNestedCallExpressions = getNestedExpressionsOfType(
|
|
451
|
+
var getNestedNewExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.NewExpression);
|
|
452
|
+
var getNestedCallExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.CallExpression);
|
|
452
453
|
function isMultiLine(node) {
|
|
453
454
|
return node.loc.start.line !== node.loc.end.line;
|
|
454
455
|
}
|
|
455
456
|
function isLineBreak(node) {
|
|
456
|
-
return isOneOf([
|
|
457
|
+
return isOneOf([AST_NODE_TYPES.Literal, AST_NODE_TYPES.JSXText])(node) && typeof node.value === "string" && node.value.trim() === "" && isMultiLine(node);
|
|
457
458
|
}
|
|
458
459
|
function isLiteral(node, type) {
|
|
459
|
-
if (node.type !==
|
|
460
|
+
if (node.type !== AST_NODE_TYPES.Literal) return false;
|
|
460
461
|
if (type == null) return true;
|
|
461
462
|
switch (type) {
|
|
462
463
|
case "boolean":
|
|
@@ -471,17 +472,17 @@ function isLiteral(node, type) {
|
|
|
471
472
|
return typeof node.value === "string";
|
|
472
473
|
}
|
|
473
474
|
}
|
|
474
|
-
var isNodeEqual =
|
|
475
|
+
var isNodeEqual = dual(2, (a, b) => {
|
|
475
476
|
switch (true) {
|
|
476
477
|
case a === b:
|
|
477
478
|
return true;
|
|
478
479
|
case a.type !== b.type:
|
|
479
480
|
return false;
|
|
480
|
-
case (a.type ===
|
|
481
|
+
case (a.type === AST_NODE_TYPES.Literal && b.type === AST_NODE_TYPES.Literal):
|
|
481
482
|
return a.value === b.value;
|
|
482
|
-
case (a.type ===
|
|
483
|
+
case (a.type === AST_NODE_TYPES.TemplateElement && b.type === AST_NODE_TYPES.TemplateElement):
|
|
483
484
|
return a.value.raw === b.value.raw;
|
|
484
|
-
case (a.type ===
|
|
485
|
+
case (a.type === AST_NODE_TYPES.TemplateLiteral && b.type === AST_NODE_TYPES.TemplateLiteral): {
|
|
485
486
|
if (a.quasis.length !== b.quasis.length || a.expressions.length !== b.expressions.length) {
|
|
486
487
|
return false;
|
|
487
488
|
}
|
|
@@ -501,13 +502,13 @@ var isNodeEqual = eff.dual(2, (a, b) => {
|
|
|
501
502
|
}
|
|
502
503
|
return true;
|
|
503
504
|
}
|
|
504
|
-
case (a.type ===
|
|
505
|
+
case (a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier):
|
|
505
506
|
return a.name === b.name;
|
|
506
|
-
case (a.type ===
|
|
507
|
+
case (a.type === AST_NODE_TYPES.PrivateIdentifier && b.type === AST_NODE_TYPES.PrivateIdentifier):
|
|
507
508
|
return a.name === b.name;
|
|
508
|
-
case (a.type ===
|
|
509
|
+
case (a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression):
|
|
509
510
|
return isNodeEqual(a.property, b.property) && isNodeEqual(a.object, b.object);
|
|
510
|
-
case (a.type ===
|
|
511
|
+
case (a.type === AST_NODE_TYPES.JSXAttribute && b.type === AST_NODE_TYPES.JSXAttribute): {
|
|
511
512
|
if (a.name.name !== b.name.name) {
|
|
512
513
|
return false;
|
|
513
514
|
}
|
|
@@ -516,18 +517,53 @@ var isNodeEqual = eff.dual(2, (a, b) => {
|
|
|
516
517
|
}
|
|
517
518
|
return isNodeEqual(a.value, b.value);
|
|
518
519
|
}
|
|
519
|
-
case (a.type ===
|
|
520
|
+
case (a.type === AST_NODE_TYPES.ThisExpression && b.type === AST_NODE_TYPES.ThisExpression):
|
|
520
521
|
return true;
|
|
521
522
|
default:
|
|
522
523
|
return false;
|
|
523
524
|
}
|
|
524
525
|
});
|
|
526
|
+
function getLiteralValueType(input) {
|
|
527
|
+
if (input === null) return "null";
|
|
528
|
+
return typeof input;
|
|
529
|
+
}
|
|
530
|
+
function toDelimiterFormat(node, delimiter = " ") {
|
|
531
|
+
if (node.type === AST_NODE_TYPES.Literal) {
|
|
532
|
+
if ("regex" in node) {
|
|
533
|
+
return "RegExp literal";
|
|
534
|
+
}
|
|
535
|
+
return `${getLiteralValueType(node.value)} literal`;
|
|
536
|
+
}
|
|
537
|
+
if (isJSX(node)) {
|
|
538
|
+
return `JSX ${toLowerCase(delimiterCase(replace(node.type, "JSX", ""), delimiter))}`;
|
|
539
|
+
}
|
|
540
|
+
return toLowerCase(delimiterCase(node.type, delimiter));
|
|
541
|
+
}
|
|
542
|
+
function toStringFormat(node, getText) {
|
|
543
|
+
switch (node.type) {
|
|
544
|
+
case AST_NODE_TYPES.Identifier:
|
|
545
|
+
case AST_NODE_TYPES.JSXIdentifier:
|
|
546
|
+
case AST_NODE_TYPES.PrivateIdentifier:
|
|
547
|
+
return node.name;
|
|
548
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
549
|
+
case AST_NODE_TYPES.JSXMemberExpression:
|
|
550
|
+
return `${toStringFormat(node.object, getText)}.${toStringFormat(node.property, getText)}`;
|
|
551
|
+
case AST_NODE_TYPES.JSXNamespacedName:
|
|
552
|
+
return `${node.namespace.name}:${node.name.name}`;
|
|
553
|
+
case AST_NODE_TYPES.JSXText:
|
|
554
|
+
return node.value;
|
|
555
|
+
case AST_NODE_TYPES.Literal:
|
|
556
|
+
return node.raw;
|
|
557
|
+
default:
|
|
558
|
+
return getText(node);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
525
561
|
function isProcessEnvNodeEnv(node) {
|
|
526
|
-
return node != null && node.type ===
|
|
562
|
+
return node != null && node.type === AST_NODE_TYPES.MemberExpression && node.object.type === AST_NODE_TYPES.MemberExpression && node.object.object.type === AST_NODE_TYPES.Identifier && node.object.object.name === "process" && node.object.property.type === AST_NODE_TYPES.Identifier && node.object.property.name === "env" && node.property.type === AST_NODE_TYPES.Identifier && node.property.name === "NODE_ENV";
|
|
527
563
|
}
|
|
528
564
|
function isProcessEnvNodeEnvCompare(node, operator, value) {
|
|
529
565
|
if (node == null) return false;
|
|
530
|
-
if (node.type !==
|
|
566
|
+
if (node.type !== AST_NODE_TYPES.BinaryExpression) return false;
|
|
531
567
|
if (node.operator !== operator) return false;
|
|
532
568
|
if (isProcessEnvNodeEnv(node.left) && isLiteral(node.right, "string")) {
|
|
533
569
|
return node.right.value === value;
|
|
@@ -541,96 +577,19 @@ function getPropertyName(node) {
|
|
|
541
577
|
if (isTypeExpression(node)) {
|
|
542
578
|
return getPropertyName(getJSExpression(node));
|
|
543
579
|
}
|
|
544
|
-
if (node.type ===
|
|
580
|
+
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.PrivateIdentifier) {
|
|
545
581
|
return node.name;
|
|
546
582
|
}
|
|
547
|
-
if (node.type ===
|
|
583
|
+
if (node.type === AST_NODE_TYPES.Literal) {
|
|
548
584
|
return String(node.value);
|
|
549
585
|
}
|
|
550
|
-
if (node.type ===
|
|
586
|
+
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
|
|
551
587
|
return node.quasis[0]?.value.raw;
|
|
552
588
|
}
|
|
553
|
-
return
|
|
554
|
-
}
|
|
555
|
-
function getLiteralValueType(input) {
|
|
556
|
-
if (input === null) return "null";
|
|
557
|
-
return typeof input;
|
|
589
|
+
return unit;
|
|
558
590
|
}
|
|
559
|
-
function
|
|
560
|
-
|
|
561
|
-
if ("regex" in node) {
|
|
562
|
-
return "RegExp literal";
|
|
563
|
-
}
|
|
564
|
-
return `${getLiteralValueType(node.value)} literal`;
|
|
565
|
-
}
|
|
566
|
-
if (isJSX(node)) {
|
|
567
|
-
return `JSX ${stringTs.toLowerCase(stringTs.delimiterCase(stringTs.replace(node.type, "JSX", ""), delimiter))}`;
|
|
568
|
-
}
|
|
569
|
-
return stringTs.toLowerCase(stringTs.delimiterCase(node.type, delimiter));
|
|
570
|
-
}
|
|
571
|
-
function toString(node, getText) {
|
|
572
|
-
switch (node.type) {
|
|
573
|
-
case types.AST_NODE_TYPES.Identifier:
|
|
574
|
-
case types.AST_NODE_TYPES.JSXIdentifier:
|
|
575
|
-
case types.AST_NODE_TYPES.PrivateIdentifier:
|
|
576
|
-
return node.name;
|
|
577
|
-
case types.AST_NODE_TYPES.MemberExpression:
|
|
578
|
-
case types.AST_NODE_TYPES.JSXMemberExpression:
|
|
579
|
-
return `${toString(node.object, getText)}.${toString(node.property, getText)}`;
|
|
580
|
-
case types.AST_NODE_TYPES.JSXNamespacedName:
|
|
581
|
-
return `${node.namespace.name}:${node.name.name}`;
|
|
582
|
-
case types.AST_NODE_TYPES.JSXText:
|
|
583
|
-
return node.value;
|
|
584
|
-
case types.AST_NODE_TYPES.Literal:
|
|
585
|
-
return node.raw;
|
|
586
|
-
default:
|
|
587
|
-
return getText(node);
|
|
588
|
-
}
|
|
591
|
+
function isThenCall(node) {
|
|
592
|
+
return node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "then";
|
|
589
593
|
}
|
|
590
594
|
|
|
591
|
-
|
|
592
|
-
exports.getArrayMethodCallbackIndexParamPosition = getArrayMethodCallbackIndexParamPosition;
|
|
593
|
-
exports.getClassId = getClassId;
|
|
594
|
-
exports.getFunctionId = getFunctionId;
|
|
595
|
-
exports.getFunctionInitPath = getFunctionInitPath;
|
|
596
|
-
exports.getJSExpression = getJSExpression;
|
|
597
|
-
exports.getNestedCallExpressions = getNestedCallExpressions;
|
|
598
|
-
exports.getNestedExpressionsOfType = getNestedExpressionsOfType;
|
|
599
|
-
exports.getNestedIdentifiers = getNestedIdentifiers;
|
|
600
|
-
exports.getNestedNewExpressions = getNestedNewExpressions;
|
|
601
|
-
exports.getNestedReturnStatements = getNestedReturnStatements;
|
|
602
|
-
exports.getPropertyName = getPropertyName;
|
|
603
|
-
exports.hasCallInFunctionInitPath = hasCallInFunctionInitPath;
|
|
604
|
-
exports.is = is;
|
|
605
|
-
exports.isArrayFromCall = isArrayFromCall;
|
|
606
|
-
exports.isArrayMapCall = isArrayMapCall;
|
|
607
|
-
exports.isArrayTupleType = isArrayTupleType;
|
|
608
|
-
exports.isClass = isClass;
|
|
609
|
-
exports.isConditional = isConditional;
|
|
610
|
-
exports.isControlFlow = isControlFlow;
|
|
611
|
-
exports.isDestructuringPattern = isDestructuringPattern;
|
|
612
|
-
exports.isEmptyFunction = isEmptyFunction;
|
|
613
|
-
exports.isFunction = isFunction;
|
|
614
|
-
exports.isFunctionType = isFunctionType;
|
|
615
|
-
exports.isJSX = isJSX;
|
|
616
|
-
exports.isJSXElement = isJSXElement;
|
|
617
|
-
exports.isJSXFragment = isJSXFragment;
|
|
618
|
-
exports.isJSXTagNameExpression = isJSXTagNameExpression;
|
|
619
|
-
exports.isLeftHandSideExpression = isLeftHandSideExpression;
|
|
620
|
-
exports.isLeftHandSideExpressionType = isLeftHandSideExpressionType;
|
|
621
|
-
exports.isLineBreak = isLineBreak;
|
|
622
|
-
exports.isLiteral = isLiteral;
|
|
623
|
-
exports.isLoop = isLoop;
|
|
624
|
-
exports.isMethodOrProperty = isMethodOrProperty;
|
|
625
|
-
exports.isMultiLine = isMultiLine;
|
|
626
|
-
exports.isNodeEqual = isNodeEqual;
|
|
627
|
-
exports.isOneOf = isOneOf;
|
|
628
|
-
exports.isProcessEnvNodeEnv = isProcessEnvNodeEnv;
|
|
629
|
-
exports.isProcessEnvNodeEnvCompare = isProcessEnvNodeEnvCompare;
|
|
630
|
-
exports.isProperty = isProperty;
|
|
631
|
-
exports.isThisExpression = isThisExpression;
|
|
632
|
-
exports.isTypeAssertionExpression = isTypeAssertionExpression;
|
|
633
|
-
exports.isTypeDeclaration = isTypeDeclaration;
|
|
634
|
-
exports.isTypeExpression = isTypeExpression;
|
|
635
|
-
exports.toDelimiterCaseType = toDelimiterCaseType;
|
|
636
|
-
exports.toString = toString;
|
|
595
|
+
export { findParentNode, getArrayMethodCallbackIndexParamPosition, getClassId, getFunctionId, getFunctionInitPath, getJSExpression, getNestedCallExpressions, getNestedExpressionsOfType, getNestedIdentifiers, getNestedNewExpressions, getNestedReturnStatements, getPropertyName, hasCallInFunctionInitPath, is, isArrayFromCall, isArrayMapCall, isArrayTupleType, isClass, isConditional, isControlFlow, isDestructuringPattern, isEmptyFunction, isFunction, isFunctionType, isImmediatelyInvokedFunction, isJSX, isJSXElement, isJSXFragment, isJSXTagNameExpression, isLeftHandSideExpression, isLeftHandSideExpressionType, isLineBreak, isLiteral, isLoop, isMethodOrProperty, isMultiLine, isNodeEqual, isOneOf, isProcessEnvNodeEnv, isProcessEnvNodeEnvCompare, isProperty, isThenCall, isThisExpression, isTypeAssertionExpression, isTypeDeclaration, isTypeExpression, toDelimiterFormat, toStringFormat };
|