@eslint-react/core 2.0.0-next.0 → 2.0.0-next.2
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.js +195 -340
- package/package.json +9 -17
- package/dist/index.d.mts +0 -544
- package/dist/index.mjs +0 -927
package/dist/index.js
CHANGED
|
@@ -1,57 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var tsPattern = require('ts-pattern');
|
|
10
|
-
var birecord = require('birecord');
|
|
11
|
-
|
|
12
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
|
-
|
|
14
|
-
function _interopNamespace(e) {
|
|
15
|
-
if (e && e.__esModule) return e;
|
|
16
|
-
var n = Object.create(null);
|
|
17
|
-
if (e) {
|
|
18
|
-
Object.keys(e).forEach(function (k) {
|
|
19
|
-
if (k !== 'default') {
|
|
20
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
21
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function () { return e[k]; }
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
n.default = e;
|
|
29
|
-
return Object.freeze(n);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var AST14__namespace = /*#__PURE__*/_interopNamespace(AST14);
|
|
33
|
-
var VAR3__namespace = /*#__PURE__*/_interopNamespace(VAR3);
|
|
34
|
-
var birecord__default = /*#__PURE__*/_interopDefault(birecord);
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
2
|
+
import * as AST14 from '@eslint-react/ast';
|
|
3
|
+
import { flip, dual, constFalse, identity, constTrue, _ } from '@eslint-react/eff';
|
|
4
|
+
import { RegExp, Selector } from '@eslint-react/kit';
|
|
5
|
+
import { coerceSettings, DEFAULT_ESLINT_REACT_SETTINGS, getId } from '@eslint-react/shared';
|
|
6
|
+
import * as VAR3 from '@eslint-react/var';
|
|
7
|
+
import { isMatching, P, match } from 'ts-pattern';
|
|
8
|
+
import birecord from 'birecord';
|
|
35
9
|
|
|
36
10
|
// src/component/component-children.ts
|
|
37
11
|
function isReactAPI(api) {
|
|
38
12
|
const func = (context, node) => {
|
|
39
13
|
if (node == null) return false;
|
|
40
14
|
const getText = (n) => context.sourceCode.getText(n);
|
|
41
|
-
const name =
|
|
15
|
+
const name = AST14.toString(node, getText);
|
|
42
16
|
if (name === api) return true;
|
|
43
17
|
if (name.substring(name.indexOf(".") + 1) === api) return true;
|
|
44
18
|
return false;
|
|
45
19
|
};
|
|
46
|
-
return
|
|
20
|
+
return dual(2, func);
|
|
47
21
|
}
|
|
48
22
|
function isReactAPICall(api) {
|
|
49
23
|
const func = (context, node) => {
|
|
50
24
|
if (node == null) return false;
|
|
51
|
-
if (node.type !==
|
|
25
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
52
26
|
return isReactAPI(api)(context, node.callee);
|
|
53
27
|
};
|
|
54
|
-
return
|
|
28
|
+
return dual(2, func);
|
|
55
29
|
}
|
|
56
30
|
var isCaptureOwnerStack = isReactAPI("captureOwnerStack");
|
|
57
31
|
var isChildrenCount = isReactAPI("Children.count");
|
|
@@ -83,51 +57,51 @@ var isLazyCall = isReactAPICall("lazy");
|
|
|
83
57
|
// src/component/component-children.ts
|
|
84
58
|
function isChildrenOfCreateElement(context, node) {
|
|
85
59
|
const parent = node.parent;
|
|
86
|
-
if (parent == null || parent.type !==
|
|
60
|
+
if (parent == null || parent.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
87
61
|
if (!isCreateElementCall(context, parent)) return false;
|
|
88
62
|
return parent.arguments.slice(2).some((arg) => arg === node);
|
|
89
63
|
}
|
|
90
64
|
function getInstanceId(node, prev) {
|
|
91
65
|
switch (true) {
|
|
92
|
-
case (node.type ===
|
|
66
|
+
case (node.type === AST_NODE_TYPES.VariableDeclarator && node.init === prev):
|
|
93
67
|
return node.id;
|
|
94
|
-
case (node.type ===
|
|
68
|
+
case (node.type === AST_NODE_TYPES.AssignmentExpression && node.right === prev):
|
|
95
69
|
return node.left;
|
|
96
|
-
case (node.type ===
|
|
70
|
+
case (node.type === AST_NODE_TYPES.PropertyDefinition && node.value === prev):
|
|
97
71
|
return node.key;
|
|
98
|
-
case (node.type ===
|
|
99
|
-
return
|
|
72
|
+
case (node.type === AST_NODE_TYPES.BlockStatement || node.type === AST_NODE_TYPES.Program || node.parent === node):
|
|
73
|
+
return _;
|
|
100
74
|
default:
|
|
101
75
|
return getInstanceId(node.parent, node);
|
|
102
76
|
}
|
|
103
77
|
}
|
|
104
78
|
function getRequireExpressionArguments(node) {
|
|
105
|
-
return
|
|
79
|
+
return match(node).with({ type: AST_NODE_TYPES.CallExpression, arguments: P.select(), callee: { type: AST_NODE_TYPES.Identifier, name: "require" } }, identity).with({ type: AST_NODE_TYPES.MemberExpression, object: P.select() }, getRequireExpressionArguments).otherwise(() => null);
|
|
106
80
|
}
|
|
107
81
|
function isInitializedFromReact(name, importSource, initialScope) {
|
|
108
82
|
if (name.toLowerCase() === "react") return true;
|
|
109
|
-
const latestDef =
|
|
83
|
+
const latestDef = VAR3.findVariable(name, initialScope)?.defs.at(-1);
|
|
110
84
|
if (latestDef == null) return false;
|
|
111
85
|
const { node, parent } = latestDef;
|
|
112
|
-
if (node.type ===
|
|
86
|
+
if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init != null) {
|
|
113
87
|
const { init } = node;
|
|
114
|
-
if (init.type ===
|
|
88
|
+
if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) {
|
|
115
89
|
return isInitializedFromReact(init.object.name, importSource, initialScope);
|
|
116
90
|
}
|
|
117
|
-
if (init.type ===
|
|
91
|
+
if (init.type === AST_NODE_TYPES.Identifier) {
|
|
118
92
|
return isInitializedFromReact(init.name, importSource, initialScope);
|
|
119
93
|
}
|
|
120
94
|
const args = getRequireExpressionArguments(init);
|
|
121
95
|
const arg0 = args?.[0];
|
|
122
|
-
if (arg0 == null || !
|
|
96
|
+
if (arg0 == null || !AST14.isLiteral(arg0, "string")) {
|
|
123
97
|
return false;
|
|
124
98
|
}
|
|
125
99
|
return arg0.value === importSource || arg0.value.startsWith(`${importSource}/`);
|
|
126
100
|
}
|
|
127
|
-
return parent?.type ===
|
|
101
|
+
return parent?.type === AST_NODE_TYPES.ImportDeclaration && parent.source.value === importSource;
|
|
128
102
|
}
|
|
129
103
|
function isInstanceIdEqual(context, a, b) {
|
|
130
|
-
return
|
|
104
|
+
return AST14.isNodeEqual(a, b) || VAR3.isNodeValueEqual(a, b, [
|
|
131
105
|
context.sourceCode.getScope(a),
|
|
132
106
|
context.sourceCode.getScope(b)
|
|
133
107
|
]);
|
|
@@ -141,34 +115,34 @@ function isReactHookName(name) {
|
|
|
141
115
|
// src/hook/hook-is.ts
|
|
142
116
|
function isReactHook(node) {
|
|
143
117
|
if (node == null) return false;
|
|
144
|
-
const id =
|
|
118
|
+
const id = AST14.getFunctionId(node);
|
|
145
119
|
return id?.name != null && isReactHookName(id.name);
|
|
146
120
|
}
|
|
147
121
|
function isReactHookCall(node) {
|
|
148
122
|
if (node == null) return false;
|
|
149
|
-
if (node.type !==
|
|
123
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) {
|
|
150
124
|
return false;
|
|
151
125
|
}
|
|
152
|
-
if (node.callee.type ===
|
|
126
|
+
if (node.callee.type === AST_NODE_TYPES.Identifier) {
|
|
153
127
|
return isReactHookName(node.callee.name);
|
|
154
128
|
}
|
|
155
|
-
if (node.callee.type ===
|
|
156
|
-
return node.callee.property.type ===
|
|
129
|
+
if (node.callee.type === AST_NODE_TYPES.MemberExpression) {
|
|
130
|
+
return node.callee.property.type === AST_NODE_TYPES.Identifier && isReactHookName(node.callee.property.name);
|
|
157
131
|
}
|
|
158
132
|
return false;
|
|
159
133
|
}
|
|
160
134
|
function isReactHookCallWithName(context, node) {
|
|
161
|
-
if (node == null || node.type !==
|
|
135
|
+
if (node == null || node.type !== AST_NODE_TYPES.CallExpression) return constFalse;
|
|
162
136
|
const {
|
|
163
|
-
importSource =
|
|
137
|
+
importSource = DEFAULT_ESLINT_REACT_SETTINGS.importSource,
|
|
164
138
|
skipImportCheck = true
|
|
165
|
-
} =
|
|
139
|
+
} = coerceSettings(context.settings);
|
|
166
140
|
const initialScope = context.sourceCode.getScope(node);
|
|
167
141
|
return (name) => {
|
|
168
142
|
switch (true) {
|
|
169
|
-
case (node.callee.type ===
|
|
143
|
+
case (node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === name):
|
|
170
144
|
return skipImportCheck || isInitializedFromReact(name, importSource, initialScope);
|
|
171
|
-
case (node.callee.type ===
|
|
145
|
+
case (node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === name && "name" in node.callee.object):
|
|
172
146
|
return skipImportCheck || isInitializedFromReact(node.callee.object.name, importSource, initialScope);
|
|
173
147
|
default:
|
|
174
148
|
return false;
|
|
@@ -176,13 +150,13 @@ function isReactHookCallWithName(context, node) {
|
|
|
176
150
|
};
|
|
177
151
|
}
|
|
178
152
|
function isReactHookCallWithNameLoose(node) {
|
|
179
|
-
if (node == null || node.type !==
|
|
153
|
+
if (node == null || node.type !== AST_NODE_TYPES.CallExpression) return constFalse;
|
|
180
154
|
return (name) => {
|
|
181
155
|
switch (node.callee.type) {
|
|
182
|
-
case
|
|
156
|
+
case AST_NODE_TYPES.Identifier:
|
|
183
157
|
return node.callee.name === name;
|
|
184
|
-
case
|
|
185
|
-
return node.callee.property.type ===
|
|
158
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
159
|
+
return node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === name;
|
|
186
160
|
default:
|
|
187
161
|
return false;
|
|
188
162
|
}
|
|
@@ -190,15 +164,15 @@ function isReactHookCallWithNameLoose(node) {
|
|
|
190
164
|
}
|
|
191
165
|
function isReactHookCallWithNameAlias(context, name, alias = []) {
|
|
192
166
|
const {
|
|
193
|
-
importSource =
|
|
167
|
+
importSource = DEFAULT_ESLINT_REACT_SETTINGS.importSource,
|
|
194
168
|
skipImportCheck = true
|
|
195
|
-
} =
|
|
169
|
+
} = coerceSettings(context.settings);
|
|
196
170
|
return (node) => {
|
|
197
171
|
const initialScope = context.sourceCode.getScope(node);
|
|
198
172
|
switch (true) {
|
|
199
|
-
case (node.callee.type ===
|
|
173
|
+
case (node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === name):
|
|
200
174
|
return skipImportCheck || isInitializedFromReact(name, importSource, initialScope);
|
|
201
|
-
case (node.callee.type ===
|
|
175
|
+
case (node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === name && "name" in node.callee.object):
|
|
202
176
|
return skipImportCheck || isInitializedFromReact(node.callee.object.name, importSource, initialScope);
|
|
203
177
|
default:
|
|
204
178
|
return alias.some(isReactHookCallWithNameLoose(node));
|
|
@@ -207,45 +181,45 @@ function isReactHookCallWithNameAlias(context, name, alias = []) {
|
|
|
207
181
|
}
|
|
208
182
|
function isUseEffectCallLoose(node) {
|
|
209
183
|
if (node == null) return false;
|
|
210
|
-
if (node.type !==
|
|
184
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) {
|
|
211
185
|
return false;
|
|
212
186
|
}
|
|
213
187
|
switch (node.callee.type) {
|
|
214
|
-
case
|
|
188
|
+
case AST_NODE_TYPES.Identifier:
|
|
215
189
|
return /^use\w*Effect$/u.test(node.callee.name);
|
|
216
|
-
case
|
|
217
|
-
return node.callee.property.type ===
|
|
190
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
191
|
+
return node.callee.property.type === AST_NODE_TYPES.Identifier && /^use\w*Effect$/u.test(node.callee.property.name);
|
|
218
192
|
default:
|
|
219
193
|
return false;
|
|
220
194
|
}
|
|
221
195
|
}
|
|
222
|
-
var isUseCall =
|
|
223
|
-
var isUseActionStateCall =
|
|
224
|
-
var isUseCallbackCall =
|
|
225
|
-
var isUseContextCall =
|
|
226
|
-
var isUseDebugValueCall =
|
|
227
|
-
var isUseDeferredValueCall =
|
|
228
|
-
var isUseEffectCall =
|
|
229
|
-
var isUseFormStatusCall =
|
|
230
|
-
var isUseIdCall =
|
|
231
|
-
var isUseImperativeHandleCall =
|
|
232
|
-
var isUseInsertionEffectCall =
|
|
233
|
-
var isUseLayoutEffectCall =
|
|
234
|
-
var isUseMemoCall =
|
|
235
|
-
var isUseOptimisticCall =
|
|
236
|
-
var isUseReducerCall =
|
|
237
|
-
var isUseRefCall =
|
|
238
|
-
var isUseStateCall =
|
|
239
|
-
var isUseSyncExternalStoreCall =
|
|
240
|
-
var isUseTransitionCall =
|
|
196
|
+
var isUseCall = flip(isReactHookCallWithName)("use");
|
|
197
|
+
var isUseActionStateCall = flip(isReactHookCallWithName)("useActionState");
|
|
198
|
+
var isUseCallbackCall = flip(isReactHookCallWithName)("useCallback");
|
|
199
|
+
var isUseContextCall = flip(isReactHookCallWithName)("useContext");
|
|
200
|
+
var isUseDebugValueCall = flip(isReactHookCallWithName)("useDebugValue");
|
|
201
|
+
var isUseDeferredValueCall = flip(isReactHookCallWithName)("useDeferredValue");
|
|
202
|
+
var isUseEffectCall = flip(isReactHookCallWithName)("useEffect");
|
|
203
|
+
var isUseFormStatusCall = flip(isReactHookCallWithName)("useFormStatus");
|
|
204
|
+
var isUseIdCall = flip(isReactHookCallWithName)("useId");
|
|
205
|
+
var isUseImperativeHandleCall = flip(isReactHookCallWithName)("useImperativeHandle");
|
|
206
|
+
var isUseInsertionEffectCall = flip(isReactHookCallWithName)("useInsertionEffect");
|
|
207
|
+
var isUseLayoutEffectCall = flip(isReactHookCallWithName)("useLayoutEffect");
|
|
208
|
+
var isUseMemoCall = flip(isReactHookCallWithName)("useMemo");
|
|
209
|
+
var isUseOptimisticCall = flip(isReactHookCallWithName)("useOptimistic");
|
|
210
|
+
var isUseReducerCall = flip(isReactHookCallWithName)("useReducer");
|
|
211
|
+
var isUseRefCall = flip(isReactHookCallWithName)("useRef");
|
|
212
|
+
var isUseStateCall = flip(isReactHookCallWithName)("useState");
|
|
213
|
+
var isUseSyncExternalStoreCall = flip(isReactHookCallWithName)("useSyncExternalStore");
|
|
214
|
+
var isUseTransitionCall = flip(isReactHookCallWithName)("useTransition");
|
|
241
215
|
|
|
242
216
|
// src/hook/hook-collector.ts
|
|
243
217
|
function useHookCollector() {
|
|
244
218
|
const hooks = /* @__PURE__ */ new Map();
|
|
245
219
|
const functionEntries = [];
|
|
246
220
|
const onFunctionEnter = (node) => {
|
|
247
|
-
const id =
|
|
248
|
-
const key =
|
|
221
|
+
const id = AST14.getFunctionId(node);
|
|
222
|
+
const key = getId();
|
|
249
223
|
const name = id?.name;
|
|
250
224
|
if (name != null && isReactHookName(name)) {
|
|
251
225
|
functionEntries.push({ key, node, isHook: true });
|
|
@@ -294,21 +268,21 @@ function useHookCollector() {
|
|
|
294
268
|
}
|
|
295
269
|
function stringifyJsx(node) {
|
|
296
270
|
switch (node.type) {
|
|
297
|
-
case
|
|
271
|
+
case AST_NODE_TYPES.JSXIdentifier:
|
|
298
272
|
return node.name;
|
|
299
|
-
case
|
|
273
|
+
case AST_NODE_TYPES.JSXNamespacedName:
|
|
300
274
|
return `${node.namespace.name}:${node.name.name}`;
|
|
301
|
-
case
|
|
275
|
+
case AST_NODE_TYPES.JSXMemberExpression:
|
|
302
276
|
return `${stringifyJsx(node.object)}.${stringifyJsx(node.property)}`;
|
|
303
|
-
case
|
|
277
|
+
case AST_NODE_TYPES.JSXText:
|
|
304
278
|
return node.value;
|
|
305
|
-
case
|
|
279
|
+
case AST_NODE_TYPES.JSXOpeningElement:
|
|
306
280
|
return `<${stringifyJsx(node.name)}>`;
|
|
307
|
-
case
|
|
281
|
+
case AST_NODE_TYPES.JSXClosingElement:
|
|
308
282
|
return `</${stringifyJsx(node.name)}>`;
|
|
309
|
-
case
|
|
283
|
+
case AST_NODE_TYPES.JSXOpeningFragment:
|
|
310
284
|
return "<>";
|
|
311
|
-
case
|
|
285
|
+
case AST_NODE_TYPES.JSXClosingFragment:
|
|
312
286
|
return "</>";
|
|
313
287
|
}
|
|
314
288
|
}
|
|
@@ -321,21 +295,21 @@ function getAttributeName(context, node) {
|
|
|
321
295
|
// src/jsx/jsx-attribute.ts
|
|
322
296
|
function getAttribute(context, name, attributes, initialScope) {
|
|
323
297
|
return attributes.findLast((attr) => {
|
|
324
|
-
if (attr.type ===
|
|
298
|
+
if (attr.type === AST_NODE_TYPES.JSXAttribute) {
|
|
325
299
|
return getAttributeName(context, attr) === name;
|
|
326
300
|
}
|
|
327
301
|
if (initialScope == null) return false;
|
|
328
302
|
switch (attr.argument.type) {
|
|
329
|
-
case
|
|
330
|
-
const variable =
|
|
331
|
-
const variableNode =
|
|
332
|
-
if (variableNode?.type ===
|
|
333
|
-
return
|
|
303
|
+
case AST_NODE_TYPES.Identifier: {
|
|
304
|
+
const variable = VAR3.findVariable(attr.argument.name, initialScope);
|
|
305
|
+
const variableNode = VAR3.getVariableInitNode(variable, 0);
|
|
306
|
+
if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
|
|
307
|
+
return VAR3.findPropertyInProperties(name, variableNode.properties, initialScope) != null;
|
|
334
308
|
}
|
|
335
309
|
return false;
|
|
336
310
|
}
|
|
337
|
-
case
|
|
338
|
-
return
|
|
311
|
+
case AST_NODE_TYPES.ObjectExpression:
|
|
312
|
+
return VAR3.findPropertyInProperties(name, attr.argument.properties, initialScope) != null;
|
|
339
313
|
}
|
|
340
314
|
return false;
|
|
341
315
|
});
|
|
@@ -343,8 +317,8 @@ function getAttribute(context, name, attributes, initialScope) {
|
|
|
343
317
|
function getAttributeValue(context, node, name) {
|
|
344
318
|
const initialScope = context.sourceCode.getScope(node);
|
|
345
319
|
switch (node.type) {
|
|
346
|
-
case
|
|
347
|
-
if (node.value?.type ===
|
|
320
|
+
case AST_NODE_TYPES.JSXAttribute:
|
|
321
|
+
if (node.value?.type === AST_NODE_TYPES.Literal) {
|
|
348
322
|
return {
|
|
349
323
|
kind: "some",
|
|
350
324
|
node: node.value,
|
|
@@ -352,16 +326,16 @@ function getAttributeValue(context, node, name) {
|
|
|
352
326
|
value: node.value.value
|
|
353
327
|
};
|
|
354
328
|
}
|
|
355
|
-
if (node.value?.type ===
|
|
356
|
-
return
|
|
329
|
+
if (node.value?.type === AST_NODE_TYPES.JSXExpressionContainer) {
|
|
330
|
+
return VAR3.toStaticValue({
|
|
357
331
|
kind: "lazy",
|
|
358
332
|
node: node.value.expression,
|
|
359
333
|
initialScope
|
|
360
334
|
});
|
|
361
335
|
}
|
|
362
336
|
return { kind: "none", node, initialScope };
|
|
363
|
-
case
|
|
364
|
-
const staticValue =
|
|
337
|
+
case AST_NODE_TYPES.JSXSpreadAttribute: {
|
|
338
|
+
const staticValue = VAR3.toStaticValue({
|
|
365
339
|
kind: "lazy",
|
|
366
340
|
node: node.argument,
|
|
367
341
|
initialScope
|
|
@@ -369,7 +343,7 @@ function getAttributeValue(context, node, name) {
|
|
|
369
343
|
if (staticValue.kind === "none") {
|
|
370
344
|
return staticValue;
|
|
371
345
|
}
|
|
372
|
-
return
|
|
346
|
+
return match(staticValue.value).with({ [name]: P.select(P.any) }, (value) => ({
|
|
373
347
|
kind: "some",
|
|
374
348
|
node: node.argument,
|
|
375
349
|
initialScope,
|
|
@@ -401,13 +375,13 @@ var DEFAULT_JSX_DETECTION_HINT = 0n | JSXDetectionHint.SkipUndefined | JSXDetect
|
|
|
401
375
|
// src/jsx/jsx-detection.ts
|
|
402
376
|
function isJsxText(node) {
|
|
403
377
|
if (node == null) return false;
|
|
404
|
-
return node.type ===
|
|
378
|
+
return node.type === AST_NODE_TYPES.JSXText || node.type === AST_NODE_TYPES.Literal;
|
|
405
379
|
}
|
|
406
380
|
function isJsxLike(code, node, hint = DEFAULT_JSX_DETECTION_HINT) {
|
|
407
381
|
if (node == null) return false;
|
|
408
|
-
if (
|
|
382
|
+
if (AST14.isJSX(node)) return true;
|
|
409
383
|
switch (node.type) {
|
|
410
|
-
case
|
|
384
|
+
case AST_NODE_TYPES.Literal: {
|
|
411
385
|
switch (typeof node.value) {
|
|
412
386
|
case "boolean":
|
|
413
387
|
return !(hint & JSXDetectionHint.SkipBooleanLiteral);
|
|
@@ -423,22 +397,22 @@ function isJsxLike(code, node, hint = DEFAULT_JSX_DETECTION_HINT) {
|
|
|
423
397
|
}
|
|
424
398
|
return false;
|
|
425
399
|
}
|
|
426
|
-
case
|
|
400
|
+
case AST_NODE_TYPES.TemplateLiteral: {
|
|
427
401
|
return !(hint & JSXDetectionHint.SkipStringLiteral);
|
|
428
402
|
}
|
|
429
|
-
case
|
|
403
|
+
case AST_NODE_TYPES.ArrayExpression: {
|
|
430
404
|
if (hint & JSXDetectionHint.StrictArray) {
|
|
431
405
|
return node.elements.every((n) => isJsxLike(code, n, hint));
|
|
432
406
|
}
|
|
433
407
|
return node.elements.some((n) => isJsxLike(code, n, hint));
|
|
434
408
|
}
|
|
435
|
-
case
|
|
409
|
+
case AST_NODE_TYPES.LogicalExpression: {
|
|
436
410
|
if (hint & JSXDetectionHint.StrictLogical) {
|
|
437
411
|
return isJsxLike(code, node.left, hint) && isJsxLike(code, node.right, hint);
|
|
438
412
|
}
|
|
439
413
|
return isJsxLike(code, node.left, hint) || isJsxLike(code, node.right, hint);
|
|
440
414
|
}
|
|
441
|
-
case
|
|
415
|
+
case AST_NODE_TYPES.ConditionalExpression: {
|
|
442
416
|
let leftHasJSX2 = function(node2) {
|
|
443
417
|
if (Array.isArray(node2.consequent)) {
|
|
444
418
|
if (node2.consequent.length === 0) {
|
|
@@ -458,39 +432,39 @@ function isJsxLike(code, node, hint = DEFAULT_JSX_DETECTION_HINT) {
|
|
|
458
432
|
}
|
|
459
433
|
return leftHasJSX2(node) || rightHasJSX2(node);
|
|
460
434
|
}
|
|
461
|
-
case
|
|
435
|
+
case AST_NODE_TYPES.SequenceExpression: {
|
|
462
436
|
const exp = node.expressions.at(-1);
|
|
463
437
|
return isJsxLike(code, exp, hint);
|
|
464
438
|
}
|
|
465
|
-
case
|
|
439
|
+
case AST_NODE_TYPES.CallExpression: {
|
|
466
440
|
if (hint & JSXDetectionHint.SkipCreateElement) {
|
|
467
441
|
return false;
|
|
468
442
|
}
|
|
469
443
|
switch (node.callee.type) {
|
|
470
|
-
case
|
|
444
|
+
case AST_NODE_TYPES.Identifier:
|
|
471
445
|
return node.callee.name === "createElement";
|
|
472
|
-
case
|
|
473
|
-
return node.callee.property.type ===
|
|
446
|
+
case AST_NODE_TYPES.MemberExpression:
|
|
447
|
+
return node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "createElement";
|
|
474
448
|
}
|
|
475
449
|
return false;
|
|
476
450
|
}
|
|
477
|
-
case
|
|
451
|
+
case AST_NODE_TYPES.Identifier: {
|
|
478
452
|
const { name } = node;
|
|
479
453
|
if (name === "undefined") {
|
|
480
454
|
return !(hint & JSXDetectionHint.SkipUndefined);
|
|
481
455
|
}
|
|
482
|
-
if (
|
|
456
|
+
if (AST14.isJSXTagNameExpression(node)) {
|
|
483
457
|
return true;
|
|
484
458
|
}
|
|
485
|
-
const variable =
|
|
486
|
-
const variableNode = variable &&
|
|
459
|
+
const variable = VAR3.findVariable(name, code.getScope(node));
|
|
460
|
+
const variableNode = variable && VAR3.getVariableInitNode(variable, 0);
|
|
487
461
|
return !!variableNode && isJsxLike(code, variableNode, hint);
|
|
488
462
|
}
|
|
489
463
|
}
|
|
490
464
|
return false;
|
|
491
465
|
}
|
|
492
466
|
function getElementType(context, node) {
|
|
493
|
-
if (node.type ===
|
|
467
|
+
if (node.type === AST_NODE_TYPES.JSXFragment) {
|
|
494
468
|
return "";
|
|
495
469
|
}
|
|
496
470
|
return stringifyJsx(node.openingElement.name);
|
|
@@ -506,22 +480,22 @@ function hasAnyAttribute(context, names, attributes, initialScope) {
|
|
|
506
480
|
function hasEveryAttribute(context, names, attributes, initialScope) {
|
|
507
481
|
return names.every((n) => hasAttribute(context, n, attributes, initialScope));
|
|
508
482
|
}
|
|
509
|
-
function findParentAttribute(node, test =
|
|
483
|
+
function findParentAttribute(node, test = constTrue) {
|
|
510
484
|
const guard = (node2) => {
|
|
511
|
-
return node2.type ===
|
|
485
|
+
return node2.type === AST_NODE_TYPES.JSXAttribute && test(node2);
|
|
512
486
|
};
|
|
513
|
-
return
|
|
487
|
+
return AST14.findParentNode(node, guard);
|
|
514
488
|
}
|
|
515
489
|
function isHostElement(context, node) {
|
|
516
|
-
return node.type ===
|
|
490
|
+
return node.type === AST_NODE_TYPES.JSXElement && node.openingElement.name.type === AST_NODE_TYPES.JSXIdentifier && /^[a-z]/u.test(node.openingElement.name.name);
|
|
517
491
|
}
|
|
518
492
|
function isKeyedElement(context, node, initialScope) {
|
|
519
|
-
return node.type ===
|
|
493
|
+
return node.type === AST_NODE_TYPES.JSXElement && hasAttribute(context, "key", node.openingElement.attributes, initialScope);
|
|
520
494
|
}
|
|
521
495
|
function isFragmentElement(context, node, allowJSXFragment = false) {
|
|
522
496
|
if (node == null) return false;
|
|
523
|
-
if (node.type !==
|
|
524
|
-
if (node.type ===
|
|
497
|
+
if (node.type !== AST_NODE_TYPES.JSXElement && node.type !== AST_NODE_TYPES.JSXFragment) return false;
|
|
498
|
+
if (node.type === AST_NODE_TYPES.JSXFragment) return allowJSXFragment;
|
|
525
499
|
return getElementType(context, node).split(".").at(-1) === "Fragment";
|
|
526
500
|
}
|
|
527
501
|
|
|
@@ -561,9 +535,9 @@ function isClassComponent(node) {
|
|
|
561
535
|
if ("superClass" in node && node.superClass != null) {
|
|
562
536
|
const re = /^(?:Pure)?Component$/u;
|
|
563
537
|
switch (true) {
|
|
564
|
-
case node.superClass.type ===
|
|
538
|
+
case node.superClass.type === AST_NODE_TYPES.Identifier:
|
|
565
539
|
return re.test(node.superClass.name);
|
|
566
|
-
case (node.superClass.type ===
|
|
540
|
+
case (node.superClass.type === AST_NODE_TYPES.MemberExpression && node.superClass.property.type === AST_NODE_TYPES.Identifier):
|
|
567
541
|
return re.test(node.superClass.property.name);
|
|
568
542
|
}
|
|
569
543
|
}
|
|
@@ -573,33 +547,33 @@ function isPureComponent(node) {
|
|
|
573
547
|
if ("superClass" in node && node.superClass != null) {
|
|
574
548
|
const re = /^PureComponent$/u;
|
|
575
549
|
switch (true) {
|
|
576
|
-
case node.superClass.type ===
|
|
550
|
+
case node.superClass.type === AST_NODE_TYPES.Identifier:
|
|
577
551
|
return re.test(node.superClass.name);
|
|
578
|
-
case (node.superClass.type ===
|
|
552
|
+
case (node.superClass.type === AST_NODE_TYPES.MemberExpression && node.superClass.property.type === AST_NODE_TYPES.Identifier):
|
|
579
553
|
return re.test(node.superClass.property.name);
|
|
580
554
|
}
|
|
581
555
|
}
|
|
582
556
|
return false;
|
|
583
557
|
}
|
|
584
558
|
function isRenderMethodLike(node) {
|
|
585
|
-
return
|
|
559
|
+
return AST14.isMethodOrProperty(node) && node.key.type === AST_NODE_TYPES.Identifier && node.key.name.startsWith("render") && node.parent.parent.type === AST_NODE_TYPES.ClassDeclaration;
|
|
586
560
|
}
|
|
587
561
|
|
|
588
562
|
// src/component/component-definition.ts
|
|
589
|
-
var isFunctionOfClassMethod =
|
|
590
|
-
type:
|
|
591
|
-
parent:
|
|
563
|
+
var isFunctionOfClassMethod = isMatching({
|
|
564
|
+
type: P.union(AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression),
|
|
565
|
+
parent: AST_NODE_TYPES.MethodDefinition
|
|
592
566
|
});
|
|
593
|
-
var isFunctionOfClassProperty =
|
|
594
|
-
type:
|
|
595
|
-
parent:
|
|
567
|
+
var isFunctionOfClassProperty = isMatching({
|
|
568
|
+
type: P.union(AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression),
|
|
569
|
+
parent: AST_NODE_TYPES.Property
|
|
596
570
|
});
|
|
597
|
-
var isFunctionOfObjectMethod =
|
|
598
|
-
type:
|
|
571
|
+
var isFunctionOfObjectMethod = isMatching({
|
|
572
|
+
type: P.union(AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression),
|
|
599
573
|
parent: {
|
|
600
|
-
type:
|
|
574
|
+
type: AST_NODE_TYPES.Property,
|
|
601
575
|
parent: {
|
|
602
|
-
type:
|
|
576
|
+
type: AST_NODE_TYPES.ObjectExpression
|
|
603
577
|
}
|
|
604
578
|
}
|
|
605
579
|
});
|
|
@@ -622,44 +596,44 @@ function isValidComponentDefinition(context, node, hint) {
|
|
|
622
596
|
if (hint & ComponentDetectionHint.SkipClassProperty && isFunctionOfClassProperty(node.parent)) {
|
|
623
597
|
return false;
|
|
624
598
|
}
|
|
625
|
-
if (hint & ComponentDetectionHint.SkipArrayMapArgument &&
|
|
599
|
+
if (hint & ComponentDetectionHint.SkipArrayMapArgument && AST14.isArrayMapCall(node.parent)) {
|
|
626
600
|
return false;
|
|
627
601
|
}
|
|
628
|
-
const significantParent =
|
|
602
|
+
const significantParent = AST14.findParentNode(
|
|
629
603
|
node,
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
604
|
+
AST14.isOneOf([
|
|
605
|
+
AST_NODE_TYPES.JSXExpressionContainer,
|
|
606
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
607
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
608
|
+
AST_NODE_TYPES.Property,
|
|
609
|
+
AST_NODE_TYPES.ClassBody
|
|
636
610
|
])
|
|
637
611
|
);
|
|
638
|
-
return significantParent == null || significantParent.type !==
|
|
612
|
+
return significantParent == null || significantParent.type !== AST_NODE_TYPES.JSXExpressionContainer;
|
|
639
613
|
}
|
|
640
614
|
function isComponentWrapperCall(context, node) {
|
|
641
|
-
if (node.type !==
|
|
615
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
642
616
|
return isMemoCall(context, node) || isForwardRefCall(context, node);
|
|
643
617
|
}
|
|
644
618
|
function isComponentWrapperCallLoose(context, node) {
|
|
645
|
-
if (node.type !==
|
|
619
|
+
if (node.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
646
620
|
return isComponentWrapperCall(context, node) || isUseCallbackCall(context, node);
|
|
647
621
|
}
|
|
648
622
|
|
|
649
623
|
// src/component/component-id.ts
|
|
650
624
|
function getFunctionComponentId(context, node) {
|
|
651
|
-
const functionId =
|
|
625
|
+
const functionId = AST14.getFunctionId(node);
|
|
652
626
|
if (functionId != null) {
|
|
653
627
|
return functionId;
|
|
654
628
|
}
|
|
655
629
|
const { parent } = node;
|
|
656
|
-
if (parent.type ===
|
|
630
|
+
if (parent.type === AST_NODE_TYPES.CallExpression && isComponentWrapperCallLoose(context, parent) && parent.parent.type === AST_NODE_TYPES.VariableDeclarator && parent.parent.id.type === AST_NODE_TYPES.Identifier) {
|
|
657
631
|
return parent.parent.id;
|
|
658
632
|
}
|
|
659
|
-
if (parent.type ===
|
|
633
|
+
if (parent.type === AST_NODE_TYPES.CallExpression && isComponentWrapperCallLoose(context, parent) && parent.parent.type === AST_NODE_TYPES.CallExpression && isComponentWrapperCallLoose(context, parent.parent) && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator && parent.parent.parent.id.type === AST_NODE_TYPES.Identifier) {
|
|
660
634
|
return parent.parent.parent.id;
|
|
661
635
|
}
|
|
662
|
-
return
|
|
636
|
+
return _;
|
|
663
637
|
}
|
|
664
638
|
|
|
665
639
|
// src/component/component-flag.ts
|
|
@@ -675,22 +649,22 @@ var ComponentFlag = {
|
|
|
675
649
|
// src/component/component-init-path.ts
|
|
676
650
|
function getComponentFlagFromInitPath(initPath) {
|
|
677
651
|
let flag = ComponentFlag.None;
|
|
678
|
-
if (initPath != null &&
|
|
652
|
+
if (initPath != null && AST14.hasCallInFunctionInitPath("memo", initPath)) {
|
|
679
653
|
flag |= ComponentFlag.Memo;
|
|
680
654
|
}
|
|
681
|
-
if (initPath != null &&
|
|
655
|
+
if (initPath != null && AST14.hasCallInFunctionInitPath("forwardRef", initPath)) {
|
|
682
656
|
flag |= ComponentFlag.ForwardRef;
|
|
683
657
|
}
|
|
684
658
|
return flag;
|
|
685
659
|
}
|
|
686
660
|
function isComponentName(name) {
|
|
687
|
-
return
|
|
661
|
+
return RegExp.COMPONENT_NAME.test(name);
|
|
688
662
|
}
|
|
689
663
|
function isComponentNameLoose(name) {
|
|
690
|
-
return
|
|
664
|
+
return RegExp.COMPONENT_NAME_LOOSE.test(name);
|
|
691
665
|
}
|
|
692
666
|
function getComponentNameFromId(id) {
|
|
693
|
-
if (id == null) return
|
|
667
|
+
if (id == null) return _;
|
|
694
668
|
return Array.isArray(id) ? id.map((n) => n.name).join(".") : id.name;
|
|
695
669
|
}
|
|
696
670
|
function hasNoneOrLooseComponentName(context, fn) {
|
|
@@ -711,14 +685,14 @@ function useComponentCollector(context, options = {}) {
|
|
|
711
685
|
const functionEntries = [];
|
|
712
686
|
const getCurrentEntry = () => functionEntries.at(-1);
|
|
713
687
|
const onFunctionEnter = (node) => {
|
|
714
|
-
const key =
|
|
688
|
+
const key = getId();
|
|
715
689
|
functionEntries.push({ key, node, hookCalls: [], isComponent: false });
|
|
716
690
|
};
|
|
717
691
|
const onFunctionExit = () => {
|
|
718
692
|
const entry = functionEntries.at(-1);
|
|
719
693
|
if (entry == null) return;
|
|
720
694
|
if (!entry.isComponent) return functionEntries.pop();
|
|
721
|
-
const shouldDrop =
|
|
695
|
+
const shouldDrop = AST14.getNestedReturnStatements(entry.node.body).slice().reverse().some((r) => {
|
|
722
696
|
return context.sourceCode.getScope(r).block === entry.node && r.argument != null && !isJsxLike(context.sourceCode, r.argument, hint);
|
|
723
697
|
});
|
|
724
698
|
if (shouldDrop) {
|
|
@@ -745,17 +719,17 @@ function useComponentCollector(context, options = {}) {
|
|
|
745
719
|
const { body } = entry.node;
|
|
746
720
|
const isComponent = hasNoneOrLooseComponentName(context, entry.node) && isJsxLike(context.sourceCode, body, hint) && isValidComponentDefinition(context, entry.node, hint);
|
|
747
721
|
if (!isComponent) return;
|
|
748
|
-
const initPath =
|
|
722
|
+
const initPath = AST14.getFunctionInitPath(entry.node);
|
|
749
723
|
const id = getFunctionComponentId(context, entry.node);
|
|
750
724
|
const name = getComponentNameFromId(id);
|
|
751
|
-
const key =
|
|
725
|
+
const key = getId();
|
|
752
726
|
components.set(key, {
|
|
753
727
|
id,
|
|
754
728
|
key,
|
|
755
729
|
kind: "function",
|
|
756
730
|
name,
|
|
757
731
|
node: entry.node,
|
|
758
|
-
displayName:
|
|
732
|
+
displayName: _,
|
|
759
733
|
flag: getComponentFlagFromInitPath(initPath),
|
|
760
734
|
hint,
|
|
761
735
|
hookCalls: entry.hookCalls,
|
|
@@ -763,10 +737,10 @@ function useComponentCollector(context, options = {}) {
|
|
|
763
737
|
});
|
|
764
738
|
},
|
|
765
739
|
...collectDisplayName ? {
|
|
766
|
-
[
|
|
740
|
+
[Selector.DISPLAY_NAME_ASSIGNMENT_EXPRESSION](node) {
|
|
767
741
|
const { left, right } = node;
|
|
768
|
-
if (left.type !==
|
|
769
|
-
const componentName = left.object.type ===
|
|
742
|
+
if (left.type !== AST_NODE_TYPES.MemberExpression) return;
|
|
743
|
+
const componentName = left.object.type === AST_NODE_TYPES.Identifier ? left.object.name : _;
|
|
770
744
|
const component = [...components.values()].findLast(({ name }) => name != null && name === componentName);
|
|
771
745
|
if (component == null) return;
|
|
772
746
|
component.displayName = right;
|
|
@@ -786,7 +760,7 @@ function useComponentCollector(context, options = {}) {
|
|
|
786
760
|
const isComponent = hasNoneOrLooseComponentName(context, entry.node) && isJsxLike(context.sourceCode, node.argument, hint) && isValidComponentDefinition(context, entry.node, hint);
|
|
787
761
|
if (!isComponent) return;
|
|
788
762
|
entry.isComponent = true;
|
|
789
|
-
const initPath =
|
|
763
|
+
const initPath = AST14.getFunctionInitPath(entry.node);
|
|
790
764
|
const id = getFunctionComponentId(context, entry.node);
|
|
791
765
|
const name = getComponentNameFromId(id);
|
|
792
766
|
components.set(entry.key, {
|
|
@@ -795,7 +769,7 @@ function useComponentCollector(context, options = {}) {
|
|
|
795
769
|
kind: "function",
|
|
796
770
|
name,
|
|
797
771
|
node: entry.node,
|
|
798
|
-
displayName:
|
|
772
|
+
displayName: _,
|
|
799
773
|
flag: getComponentFlagFromInitPath(initPath),
|
|
800
774
|
hint,
|
|
801
775
|
hookCalls: entry.hookCalls,
|
|
@@ -817,8 +791,8 @@ function useComponentCollectorLegacy() {
|
|
|
817
791
|
if (!isClassComponent(node)) {
|
|
818
792
|
return;
|
|
819
793
|
}
|
|
820
|
-
const id =
|
|
821
|
-
const key =
|
|
794
|
+
const id = AST14.getClassId(node);
|
|
795
|
+
const key = getId();
|
|
822
796
|
const flag = isPureComponent(node) ? ComponentFlag.PureComponent : ComponentFlag.None;
|
|
823
797
|
components.set(
|
|
824
798
|
key,
|
|
@@ -829,7 +803,7 @@ function useComponentCollectorLegacy() {
|
|
|
829
803
|
name: id?.name,
|
|
830
804
|
node,
|
|
831
805
|
// TODO: Get displayName of class component
|
|
832
|
-
displayName:
|
|
806
|
+
displayName: _,
|
|
833
807
|
flag,
|
|
834
808
|
hint: 0n,
|
|
835
809
|
// TODO: Get methods of class component
|
|
@@ -844,63 +818,63 @@ function useComponentCollectorLegacy() {
|
|
|
844
818
|
return { ctx, listeners };
|
|
845
819
|
}
|
|
846
820
|
function isComponentDidCatch(node) {
|
|
847
|
-
return
|
|
821
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentDidCatch";
|
|
848
822
|
}
|
|
849
823
|
function isComponentDidMount(node) {
|
|
850
|
-
return
|
|
824
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentDidMount";
|
|
851
825
|
}
|
|
852
826
|
function isComponentDidUpdate(node) {
|
|
853
|
-
return
|
|
827
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentDidUpdate";
|
|
854
828
|
}
|
|
855
829
|
function isComponentWillMount(node) {
|
|
856
|
-
return
|
|
830
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillMount";
|
|
857
831
|
}
|
|
858
832
|
function isComponentWillReceiveProps(node) {
|
|
859
|
-
return
|
|
833
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillReceiveProps";
|
|
860
834
|
}
|
|
861
835
|
function isComponentWillUnmount(node) {
|
|
862
|
-
return
|
|
836
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillUnmount";
|
|
863
837
|
}
|
|
864
838
|
function isComponentWillUpdate(node) {
|
|
865
|
-
return
|
|
839
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "componentWillUpdate";
|
|
866
840
|
}
|
|
867
841
|
function isGetChildContext(node) {
|
|
868
|
-
return
|
|
842
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getChildContext";
|
|
869
843
|
}
|
|
870
844
|
function isGetDefaultProps(node) {
|
|
871
|
-
return
|
|
845
|
+
return AST14.isMethodOrProperty(node) && node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getDefaultProps";
|
|
872
846
|
}
|
|
873
847
|
function isGetInitialState(node) {
|
|
874
|
-
return
|
|
848
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getInitialState";
|
|
875
849
|
}
|
|
876
850
|
function isGetSnapshotBeforeUpdate(node) {
|
|
877
|
-
return
|
|
851
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getSnapshotBeforeUpdate";
|
|
878
852
|
}
|
|
879
853
|
function isShouldComponentUpdate(node) {
|
|
880
|
-
return
|
|
854
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "shouldComponentUpdate";
|
|
881
855
|
}
|
|
882
856
|
function isUnsafeComponentWillMount(node) {
|
|
883
|
-
return
|
|
857
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "UNSAFE_componentWillMount";
|
|
884
858
|
}
|
|
885
859
|
function isUnsafeComponentWillReceiveProps(node) {
|
|
886
|
-
return
|
|
860
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "UNSAFE_componentWillReceiveProps";
|
|
887
861
|
}
|
|
888
862
|
function isUnsafeComponentWillUpdate(node) {
|
|
889
|
-
return
|
|
863
|
+
return AST14.isMethodOrProperty(node) && !node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "UNSAFE_componentWillUpdate";
|
|
890
864
|
}
|
|
891
865
|
function isGetDerivedStateFromProps(node) {
|
|
892
|
-
return
|
|
866
|
+
return AST14.isMethodOrProperty(node) && node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getDerivedStateFromProps";
|
|
893
867
|
}
|
|
894
868
|
function isGetDerivedStateFromError(node) {
|
|
895
|
-
return
|
|
869
|
+
return AST14.isMethodOrProperty(node) && node.static && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "getDerivedStateFromError";
|
|
896
870
|
}
|
|
897
|
-
var ComponentPhaseRelevance =
|
|
871
|
+
var ComponentPhaseRelevance = birecord({
|
|
898
872
|
mount: "unmount",
|
|
899
873
|
setup: "cleanup"
|
|
900
874
|
});
|
|
901
|
-
var isInversePhase =
|
|
875
|
+
var isInversePhase = dual(2, (a, b) => ComponentPhaseRelevance.get(a) === b);
|
|
902
876
|
function isRenderLike(node) {
|
|
903
|
-
return
|
|
877
|
+
return AST14.isMethodOrProperty(node) && node.key.type === AST_NODE_TYPES.Identifier && node.key.name === "render" && node.parent.parent.type === AST_NODE_TYPES.ClassDeclaration;
|
|
904
878
|
}
|
|
905
879
|
function isFunctionOfRender(node) {
|
|
906
880
|
if (!isRenderLike(node.parent)) {
|
|
@@ -910,8 +884,8 @@ function isFunctionOfRender(node) {
|
|
|
910
884
|
}
|
|
911
885
|
function isRenderFunctionLoose(context, node) {
|
|
912
886
|
const { body, parent } = node;
|
|
913
|
-
if (
|
|
914
|
-
return parent.type ===
|
|
887
|
+
if (AST14.getFunctionId(node)?.name.startsWith("render")) {
|
|
888
|
+
return parent.type === AST_NODE_TYPES.JSXExpressionContainer && parent.parent.type === AST_NODE_TYPES.JSXAttribute && parent.parent.name.type === AST_NODE_TYPES.JSXIdentifier && parent.parent.name.name.startsWith("render");
|
|
915
889
|
}
|
|
916
890
|
return isJsxLike(
|
|
917
891
|
context.sourceCode,
|
|
@@ -920,14 +894,14 @@ function isRenderFunctionLoose(context, node) {
|
|
|
920
894
|
);
|
|
921
895
|
}
|
|
922
896
|
function isRenderPropLoose(context, node) {
|
|
923
|
-
if (node.name.type !==
|
|
897
|
+
if (node.name.type !== AST_NODE_TYPES.JSXIdentifier) {
|
|
924
898
|
return false;
|
|
925
899
|
}
|
|
926
|
-
return node.name.name.startsWith("render") && node.value?.type ===
|
|
900
|
+
return node.name.name.startsWith("render") && node.value?.type === AST_NODE_TYPES.JSXExpressionContainer && AST14.isFunction(node.value.expression) && isRenderFunctionLoose(context, node.value.expression);
|
|
927
901
|
}
|
|
928
902
|
function isDirectValueOfRenderPropertyLoose(node) {
|
|
929
903
|
const matching = (node2) => {
|
|
930
|
-
return node2.type ===
|
|
904
|
+
return node2.type === AST_NODE_TYPES.Property && node2.key.type === AST_NODE_TYPES.Identifier && node2.key.name.startsWith("render");
|
|
931
905
|
};
|
|
932
906
|
return matching(node) || node.parent != null && matching(node.parent);
|
|
933
907
|
}
|
|
@@ -935,138 +909,19 @@ function isDeclaredInRenderPropLoose(node) {
|
|
|
935
909
|
if (isDirectValueOfRenderPropertyLoose(node)) {
|
|
936
910
|
return true;
|
|
937
911
|
}
|
|
938
|
-
const parent =
|
|
939
|
-
if (parent?.type !==
|
|
912
|
+
const parent = AST14.findParentNode(node, AST14.is(AST_NODE_TYPES.JSXExpressionContainer))?.parent;
|
|
913
|
+
if (parent?.type !== AST_NODE_TYPES.JSXAttribute) {
|
|
940
914
|
return false;
|
|
941
915
|
}
|
|
942
|
-
return parent.name.type ===
|
|
916
|
+
return parent.name.type === AST_NODE_TYPES.JSXIdentifier && parent.name.name.startsWith("render");
|
|
943
917
|
}
|
|
944
918
|
function isThisSetState(node) {
|
|
945
919
|
const { callee } = node;
|
|
946
|
-
return callee.type ===
|
|
920
|
+
return callee.type === AST_NODE_TYPES.MemberExpression && AST14.isThisExpression(callee.object) && callee.property.type === AST_NODE_TYPES.Identifier && callee.property.name === "setState";
|
|
947
921
|
}
|
|
948
922
|
function isAssignmentToThisState(node) {
|
|
949
923
|
const { left } = node;
|
|
950
|
-
return left.type ===
|
|
924
|
+
return left.type === AST_NODE_TYPES.MemberExpression && AST14.isThisExpression(left.object) && AST14.getPropertyName(left.property) === "state";
|
|
951
925
|
}
|
|
952
926
|
|
|
953
|
-
|
|
954
|
-
exports.ComponentFlag = ComponentFlag;
|
|
955
|
-
exports.ComponentPhaseRelevance = ComponentPhaseRelevance;
|
|
956
|
-
exports.DEFAULT_COMPONENT_DETECTION_HINT = DEFAULT_COMPONENT_DETECTION_HINT;
|
|
957
|
-
exports.DEFAULT_JSX_DETECTION_HINT = DEFAULT_JSX_DETECTION_HINT;
|
|
958
|
-
exports.JSXDetectionHint = JSXDetectionHint;
|
|
959
|
-
exports.findParentAttribute = findParentAttribute;
|
|
960
|
-
exports.getAttribute = getAttribute;
|
|
961
|
-
exports.getAttributeName = getAttributeName;
|
|
962
|
-
exports.getAttributeValue = getAttributeValue;
|
|
963
|
-
exports.getComponentFlagFromInitPath = getComponentFlagFromInitPath;
|
|
964
|
-
exports.getComponentNameFromId = getComponentNameFromId;
|
|
965
|
-
exports.getElementType = getElementType;
|
|
966
|
-
exports.getFunctionComponentId = getFunctionComponentId;
|
|
967
|
-
exports.getInstanceId = getInstanceId;
|
|
968
|
-
exports.hasAnyAttribute = hasAnyAttribute;
|
|
969
|
-
exports.hasAttribute = hasAttribute;
|
|
970
|
-
exports.hasEveryAttribute = hasEveryAttribute;
|
|
971
|
-
exports.hasNoneOrLooseComponentName = hasNoneOrLooseComponentName;
|
|
972
|
-
exports.isAssignmentToThisState = isAssignmentToThisState;
|
|
973
|
-
exports.isCaptureOwnerStack = isCaptureOwnerStack;
|
|
974
|
-
exports.isCaptureOwnerStackCall = isCaptureOwnerStackCall;
|
|
975
|
-
exports.isChildrenCount = isChildrenCount;
|
|
976
|
-
exports.isChildrenCountCall = isChildrenCountCall;
|
|
977
|
-
exports.isChildrenForEach = isChildrenForEach;
|
|
978
|
-
exports.isChildrenForEachCall = isChildrenForEachCall;
|
|
979
|
-
exports.isChildrenMap = isChildrenMap;
|
|
980
|
-
exports.isChildrenMapCall = isChildrenMapCall;
|
|
981
|
-
exports.isChildrenOfCreateElement = isChildrenOfCreateElement;
|
|
982
|
-
exports.isChildrenOnly = isChildrenOnly;
|
|
983
|
-
exports.isChildrenOnlyCall = isChildrenOnlyCall;
|
|
984
|
-
exports.isChildrenToArray = isChildrenToArray;
|
|
985
|
-
exports.isChildrenToArrayCall = isChildrenToArrayCall;
|
|
986
|
-
exports.isClassComponent = isClassComponent;
|
|
987
|
-
exports.isCloneElement = isCloneElement;
|
|
988
|
-
exports.isCloneElementCall = isCloneElementCall;
|
|
989
|
-
exports.isComponentDidCatch = isComponentDidCatch;
|
|
990
|
-
exports.isComponentDidMount = isComponentDidMount;
|
|
991
|
-
exports.isComponentDidUpdate = isComponentDidUpdate;
|
|
992
|
-
exports.isComponentName = isComponentName;
|
|
993
|
-
exports.isComponentNameLoose = isComponentNameLoose;
|
|
994
|
-
exports.isComponentWillMount = isComponentWillMount;
|
|
995
|
-
exports.isComponentWillReceiveProps = isComponentWillReceiveProps;
|
|
996
|
-
exports.isComponentWillUnmount = isComponentWillUnmount;
|
|
997
|
-
exports.isComponentWillUpdate = isComponentWillUpdate;
|
|
998
|
-
exports.isComponentWrapperCall = isComponentWrapperCall;
|
|
999
|
-
exports.isComponentWrapperCallLoose = isComponentWrapperCallLoose;
|
|
1000
|
-
exports.isCreateContext = isCreateContext;
|
|
1001
|
-
exports.isCreateContextCall = isCreateContextCall;
|
|
1002
|
-
exports.isCreateElement = isCreateElement;
|
|
1003
|
-
exports.isCreateElementCall = isCreateElementCall;
|
|
1004
|
-
exports.isCreateRef = isCreateRef;
|
|
1005
|
-
exports.isCreateRefCall = isCreateRefCall;
|
|
1006
|
-
exports.isDeclaredInRenderPropLoose = isDeclaredInRenderPropLoose;
|
|
1007
|
-
exports.isDirectValueOfRenderPropertyLoose = isDirectValueOfRenderPropertyLoose;
|
|
1008
|
-
exports.isForwardRef = isForwardRef;
|
|
1009
|
-
exports.isForwardRefCall = isForwardRefCall;
|
|
1010
|
-
exports.isFragmentElement = isFragmentElement;
|
|
1011
|
-
exports.isFunctionOfRender = isFunctionOfRender;
|
|
1012
|
-
exports.isFunctionOfRenderMethod = isFunctionOfRenderMethod;
|
|
1013
|
-
exports.isGetChildContext = isGetChildContext;
|
|
1014
|
-
exports.isGetDefaultProps = isGetDefaultProps;
|
|
1015
|
-
exports.isGetDerivedStateFromError = isGetDerivedStateFromError;
|
|
1016
|
-
exports.isGetDerivedStateFromProps = isGetDerivedStateFromProps;
|
|
1017
|
-
exports.isGetInitialState = isGetInitialState;
|
|
1018
|
-
exports.isGetSnapshotBeforeUpdate = isGetSnapshotBeforeUpdate;
|
|
1019
|
-
exports.isHostElement = isHostElement;
|
|
1020
|
-
exports.isInitializedFromReact = isInitializedFromReact;
|
|
1021
|
-
exports.isInstanceIdEqual = isInstanceIdEqual;
|
|
1022
|
-
exports.isInversePhase = isInversePhase;
|
|
1023
|
-
exports.isJsxLike = isJsxLike;
|
|
1024
|
-
exports.isJsxText = isJsxText;
|
|
1025
|
-
exports.isKeyedElement = isKeyedElement;
|
|
1026
|
-
exports.isLazy = isLazy;
|
|
1027
|
-
exports.isLazyCall = isLazyCall;
|
|
1028
|
-
exports.isMemo = isMemo;
|
|
1029
|
-
exports.isMemoCall = isMemoCall;
|
|
1030
|
-
exports.isPureComponent = isPureComponent;
|
|
1031
|
-
exports.isReactAPI = isReactAPI;
|
|
1032
|
-
exports.isReactAPICall = isReactAPICall;
|
|
1033
|
-
exports.isReactHook = isReactHook;
|
|
1034
|
-
exports.isReactHookCall = isReactHookCall;
|
|
1035
|
-
exports.isReactHookCallWithName = isReactHookCallWithName;
|
|
1036
|
-
exports.isReactHookCallWithNameAlias = isReactHookCallWithNameAlias;
|
|
1037
|
-
exports.isReactHookCallWithNameLoose = isReactHookCallWithNameLoose;
|
|
1038
|
-
exports.isReactHookName = isReactHookName;
|
|
1039
|
-
exports.isRenderFunctionLoose = isRenderFunctionLoose;
|
|
1040
|
-
exports.isRenderLike = isRenderLike;
|
|
1041
|
-
exports.isRenderMethodLike = isRenderMethodLike;
|
|
1042
|
-
exports.isRenderPropLoose = isRenderPropLoose;
|
|
1043
|
-
exports.isShouldComponentUpdate = isShouldComponentUpdate;
|
|
1044
|
-
exports.isThisSetState = isThisSetState;
|
|
1045
|
-
exports.isUnsafeComponentWillMount = isUnsafeComponentWillMount;
|
|
1046
|
-
exports.isUnsafeComponentWillReceiveProps = isUnsafeComponentWillReceiveProps;
|
|
1047
|
-
exports.isUnsafeComponentWillUpdate = isUnsafeComponentWillUpdate;
|
|
1048
|
-
exports.isUseActionStateCall = isUseActionStateCall;
|
|
1049
|
-
exports.isUseCall = isUseCall;
|
|
1050
|
-
exports.isUseCallbackCall = isUseCallbackCall;
|
|
1051
|
-
exports.isUseContextCall = isUseContextCall;
|
|
1052
|
-
exports.isUseDebugValueCall = isUseDebugValueCall;
|
|
1053
|
-
exports.isUseDeferredValueCall = isUseDeferredValueCall;
|
|
1054
|
-
exports.isUseEffectCall = isUseEffectCall;
|
|
1055
|
-
exports.isUseEffectCallLoose = isUseEffectCallLoose;
|
|
1056
|
-
exports.isUseFormStatusCall = isUseFormStatusCall;
|
|
1057
|
-
exports.isUseIdCall = isUseIdCall;
|
|
1058
|
-
exports.isUseImperativeHandleCall = isUseImperativeHandleCall;
|
|
1059
|
-
exports.isUseInsertionEffectCall = isUseInsertionEffectCall;
|
|
1060
|
-
exports.isUseLayoutEffectCall = isUseLayoutEffectCall;
|
|
1061
|
-
exports.isUseMemoCall = isUseMemoCall;
|
|
1062
|
-
exports.isUseOptimisticCall = isUseOptimisticCall;
|
|
1063
|
-
exports.isUseReducerCall = isUseReducerCall;
|
|
1064
|
-
exports.isUseRefCall = isUseRefCall;
|
|
1065
|
-
exports.isUseStateCall = isUseStateCall;
|
|
1066
|
-
exports.isUseSyncExternalStoreCall = isUseSyncExternalStoreCall;
|
|
1067
|
-
exports.isUseTransitionCall = isUseTransitionCall;
|
|
1068
|
-
exports.isValidComponentDefinition = isValidComponentDefinition;
|
|
1069
|
-
exports.stringifyJsx = stringifyJsx;
|
|
1070
|
-
exports.useComponentCollector = useComponentCollector;
|
|
1071
|
-
exports.useComponentCollectorLegacy = useComponentCollectorLegacy;
|
|
1072
|
-
exports.useHookCollector = useHookCollector;
|
|
927
|
+
export { ComponentDetectionHint, ComponentFlag, ComponentPhaseRelevance, DEFAULT_COMPONENT_DETECTION_HINT, DEFAULT_JSX_DETECTION_HINT, JSXDetectionHint, findParentAttribute, getAttribute, getAttributeName, getAttributeValue, getComponentFlagFromInitPath, getComponentNameFromId, getElementType, getFunctionComponentId, getInstanceId, hasAnyAttribute, hasAttribute, hasEveryAttribute, hasNoneOrLooseComponentName, isAssignmentToThisState, isCaptureOwnerStack, isCaptureOwnerStackCall, isChildrenCount, isChildrenCountCall, isChildrenForEach, isChildrenForEachCall, isChildrenMap, isChildrenMapCall, isChildrenOfCreateElement, isChildrenOnly, isChildrenOnlyCall, isChildrenToArray, isChildrenToArrayCall, isClassComponent, isCloneElement, isCloneElementCall, isComponentDidCatch, isComponentDidMount, isComponentDidUpdate, isComponentName, isComponentNameLoose, isComponentWillMount, isComponentWillReceiveProps, isComponentWillUnmount, isComponentWillUpdate, isComponentWrapperCall, isComponentWrapperCallLoose, isCreateContext, isCreateContextCall, isCreateElement, isCreateElementCall, isCreateRef, isCreateRefCall, isDeclaredInRenderPropLoose, isDirectValueOfRenderPropertyLoose, isForwardRef, isForwardRefCall, isFragmentElement, isFunctionOfRender, isFunctionOfRenderMethod, isGetChildContext, isGetDefaultProps, isGetDerivedStateFromError, isGetDerivedStateFromProps, isGetInitialState, isGetSnapshotBeforeUpdate, isHostElement, isInitializedFromReact, isInstanceIdEqual, isInversePhase, isJsxLike, isJsxText, isKeyedElement, isLazy, isLazyCall, isMemo, isMemoCall, isPureComponent, isReactAPI, isReactAPICall, isReactHook, isReactHookCall, isReactHookCallWithName, isReactHookCallWithNameAlias, isReactHookCallWithNameLoose, isReactHookName, isRenderFunctionLoose, isRenderLike, isRenderMethodLike, isRenderPropLoose, isShouldComponentUpdate, isThisSetState, isUnsafeComponentWillMount, isUnsafeComponentWillReceiveProps, isUnsafeComponentWillUpdate, isUseActionStateCall, isUseCall, isUseCallbackCall, isUseContextCall, isUseDebugValueCall, isUseDeferredValueCall, isUseEffectCall, isUseEffectCallLoose, isUseFormStatusCall, isUseIdCall, isUseImperativeHandleCall, isUseInsertionEffectCall, isUseLayoutEffectCall, isUseMemoCall, isUseOptimisticCall, isUseReducerCall, isUseRefCall, isUseStateCall, isUseSyncExternalStoreCall, isUseTransitionCall, isValidComponentDefinition, stringifyJsx, useComponentCollector, useComponentCollectorLegacy, useHookCollector };
|