@eslint-react/core 1.24.0-beta.1 → 1.24.0-beta.16
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.mts +152 -140
- package/dist/index.d.ts +152 -140
- package/dist/index.js +160 -158
- package/dist/index.mjs +144 -148
- package/package.json +11 -11
package/dist/index.js
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
var AST8 = require('@eslint-react/ast');
|
|
4
4
|
var eff = require('@eslint-react/eff');
|
|
5
|
-
var
|
|
5
|
+
var JSX3 = require('@eslint-react/jsx');
|
|
6
|
+
var shared = require('@eslint-react/shared');
|
|
6
7
|
var types = require('@typescript-eslint/types');
|
|
7
8
|
var VAR = require('@eslint-react/var');
|
|
8
|
-
var
|
|
9
|
+
var birecord = require('birecord');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
12
|
|
|
10
13
|
function _interopNamespace(e) {
|
|
11
14
|
if (e && e.__esModule) return e;
|
|
@@ -26,34 +29,12 @@ function _interopNamespace(e) {
|
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
var AST8__namespace = /*#__PURE__*/_interopNamespace(AST8);
|
|
29
|
-
var
|
|
32
|
+
var JSX3__namespace = /*#__PURE__*/_interopNamespace(JSX3);
|
|
30
33
|
var VAR__namespace = /*#__PURE__*/_interopNamespace(VAR);
|
|
34
|
+
var birecord__default = /*#__PURE__*/_interopDefault(birecord);
|
|
31
35
|
|
|
32
36
|
// src/component/component-collector.ts
|
|
33
|
-
function
|
|
34
|
-
const name = JSX4__namespace.getElementName(node);
|
|
35
|
-
if (name === name.toLowerCase()) return [name, name];
|
|
36
|
-
const component = additionalComponents.findLast((c) => c.name === name || c.re.test(name));
|
|
37
|
-
if (component != null) return [name, component.as];
|
|
38
|
-
if (polymorphicPropName == null) return [name, name];
|
|
39
|
-
const initialScope = context.sourceCode.getScope(node);
|
|
40
|
-
const polymorphicProp = JSX4__namespace.findPropInAttributes(
|
|
41
|
-
polymorphicPropName,
|
|
42
|
-
initialScope,
|
|
43
|
-
node.attributes
|
|
44
|
-
);
|
|
45
|
-
if (polymorphicProp == null) return [name, name];
|
|
46
|
-
const polymorphicName = VAR__namespace.toResolved(JSX4__namespace.getPropValue(polymorphicProp, initialScope)).value;
|
|
47
|
-
if (typeof polymorphicName === "string") {
|
|
48
|
-
return [name, polymorphicName];
|
|
49
|
-
}
|
|
50
|
-
return [name, name];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// src/utils/get-id.ts
|
|
54
|
-
var id = 0n;
|
|
55
|
-
var getId = () => (id++).toString();
|
|
56
|
-
function isInitializedFromReact(name, initialScope, importSource = "react") {
|
|
37
|
+
function isInitializedFromReact(name, importSource, initialScope) {
|
|
57
38
|
return name.toLowerCase() === "react" || VAR__namespace.isInitializedFromSource(
|
|
58
39
|
name,
|
|
59
40
|
importSource,
|
|
@@ -71,12 +52,13 @@ function isFromReact(name) {
|
|
|
71
52
|
}
|
|
72
53
|
return node.name === name;
|
|
73
54
|
}
|
|
55
|
+
const importSource = settings.importSource ?? shared.DEFAULT_ESLINT_REACT_SETTINGS.importSource;
|
|
74
56
|
const initialScope = context.sourceCode.getScope(node);
|
|
75
57
|
if (node.type === types.AST_NODE_TYPES.MemberExpression) {
|
|
76
|
-
return node.object.type === types.AST_NODE_TYPES.Identifier && node.property.type === types.AST_NODE_TYPES.Identifier && node.property.name === name && isInitializedFromReact(node.object.name,
|
|
58
|
+
return node.object.type === types.AST_NODE_TYPES.Identifier && node.property.type === types.AST_NODE_TYPES.Identifier && node.property.name === name && isInitializedFromReact(node.object.name, importSource, initialScope);
|
|
77
59
|
}
|
|
78
60
|
if (node.name === name) {
|
|
79
|
-
return isInitializedFromReact(name,
|
|
61
|
+
return isInitializedFromReact(name, importSource, initialScope);
|
|
80
62
|
}
|
|
81
63
|
return false;
|
|
82
64
|
};
|
|
@@ -96,14 +78,15 @@ function isFromReactMember(memberName, name) {
|
|
|
96
78
|
}
|
|
97
79
|
return false;
|
|
98
80
|
}
|
|
81
|
+
const importSource = settings.importSource ?? shared.DEFAULT_ESLINT_REACT_SETTINGS.importSource;
|
|
99
82
|
const initialScope = context.sourceCode.getScope(node);
|
|
100
83
|
if (node.property.type !== types.AST_NODE_TYPES.Identifier || node.property.name !== name) {
|
|
101
84
|
return false;
|
|
102
85
|
}
|
|
103
86
|
if (node.object.type === types.AST_NODE_TYPES.Identifier && node.object.name === memberName) {
|
|
104
|
-
return isInitializedFromReact(node.object.name,
|
|
87
|
+
return isInitializedFromReact(node.object.name, importSource, initialScope);
|
|
105
88
|
}
|
|
106
|
-
if (node.object.type === types.AST_NODE_TYPES.MemberExpression && node.object.object.type === types.AST_NODE_TYPES.Identifier && isInitializedFromReact(node.object.object.name,
|
|
89
|
+
if (node.object.type === types.AST_NODE_TYPES.MemberExpression && node.object.object.type === types.AST_NODE_TYPES.Identifier && isInitializedFromReact(node.object.object.name, importSource, initialScope) && node.object.property.type === types.AST_NODE_TYPES.Identifier) {
|
|
107
90
|
return node.object.property.name === memberName;
|
|
108
91
|
}
|
|
109
92
|
return false;
|
|
@@ -164,9 +147,9 @@ var isMemoCall = isReactAPICallWithName("memo");
|
|
|
164
147
|
|
|
165
148
|
// src/element/hierarchy.ts
|
|
166
149
|
function isInsideCreateElementProps(node, context) {
|
|
167
|
-
const call = AST8__namespace.
|
|
150
|
+
const call = AST8__namespace.findParentNode(node, isCreateElementCall(context));
|
|
168
151
|
if (call == null) return false;
|
|
169
|
-
const prop = AST8__namespace.
|
|
152
|
+
const prop = AST8__namespace.findParentNode(node, AST8__namespace.is(types.AST_NODE_TYPES.ObjectExpression));
|
|
170
153
|
if (prop == null) return false;
|
|
171
154
|
return prop === call.arguments[1];
|
|
172
155
|
}
|
|
@@ -186,8 +169,8 @@ function isReactHookName(name) {
|
|
|
186
169
|
// src/hook/is.ts
|
|
187
170
|
function isReactHook(node) {
|
|
188
171
|
if (node == null) return eff._;
|
|
189
|
-
const
|
|
190
|
-
return
|
|
172
|
+
const id = AST8__namespace.getFunctionIdentifier(node);
|
|
173
|
+
return id?.name != null && isReactHookName(id.name);
|
|
191
174
|
}
|
|
192
175
|
function isReactHookCall(node) {
|
|
193
176
|
if (node == null) return false;
|
|
@@ -205,13 +188,14 @@ function isReactHookCall(node) {
|
|
|
205
188
|
function isReactHookCallWithName(node, context) {
|
|
206
189
|
if (node == null) return eff.returnFalse;
|
|
207
190
|
const settings = shared.unsafeDecodeSettings(context.settings);
|
|
191
|
+
const importSource = settings.importSource ?? "react";
|
|
192
|
+
const initialScope = context.sourceCode.getScope(node);
|
|
208
193
|
return (name) => {
|
|
209
|
-
const initialScope = context.sourceCode.getScope(node);
|
|
210
194
|
switch (true) {
|
|
211
195
|
case (node.callee.type === types.AST_NODE_TYPES.Identifier && node.callee.name === name):
|
|
212
|
-
return !settings.strictImportCheck || isInitializedFromReact(name,
|
|
196
|
+
return !settings.strictImportCheck || isInitializedFromReact(name, importSource, initialScope);
|
|
213
197
|
case (node.callee.type === types.AST_NODE_TYPES.MemberExpression && node.callee.property.type === types.AST_NODE_TYPES.Identifier && node.callee.property.name === name && "name" in node.callee.object):
|
|
214
|
-
return !settings.strictImportCheck || isInitializedFromReact(node.callee.object.name,
|
|
198
|
+
return !settings.strictImportCheck || isInitializedFromReact(node.callee.object.name, importSource, initialScope);
|
|
215
199
|
default:
|
|
216
200
|
return false;
|
|
217
201
|
}
|
|
@@ -232,13 +216,14 @@ function isReactHookCallWithNameLoose(node) {
|
|
|
232
216
|
}
|
|
233
217
|
function isReactHookCallWithNameAlias(name, context, alias) {
|
|
234
218
|
const settings = shared.unsafeDecodeSettings(context);
|
|
219
|
+
const importSource = settings.importSource ?? "react";
|
|
235
220
|
return (node) => {
|
|
236
221
|
const initialScope = context.sourceCode.getScope(node);
|
|
237
222
|
switch (true) {
|
|
238
223
|
case (node.callee.type === types.AST_NODE_TYPES.Identifier && node.callee.name === name):
|
|
239
|
-
return !settings.strictImportCheck || isInitializedFromReact(name,
|
|
224
|
+
return !settings.strictImportCheck || isInitializedFromReact(name, importSource, initialScope);
|
|
240
225
|
case (node.callee.type === types.AST_NODE_TYPES.MemberExpression && node.callee.property.type === types.AST_NODE_TYPES.Identifier && node.callee.property.name === name && "name" in node.callee.object):
|
|
241
|
-
return !settings.strictImportCheck || isInitializedFromReact(node.callee.object.name,
|
|
226
|
+
return !settings.strictImportCheck || isInitializedFromReact(node.callee.object.name, importSource, initialScope);
|
|
242
227
|
default:
|
|
243
228
|
return alias.some(isReactHookCallWithNameLoose(node));
|
|
244
229
|
}
|
|
@@ -276,20 +261,20 @@ var isUseTransitionCall = eff.flip(isReactHookCallWithName)("useTransition");
|
|
|
276
261
|
|
|
277
262
|
// src/hook/hierarchy.ts
|
|
278
263
|
function isInsideReactHook(node) {
|
|
279
|
-
return isReactHook(AST8__namespace.
|
|
264
|
+
return isReactHook(AST8__namespace.findParentNode(node, AST8__namespace.isFunction));
|
|
280
265
|
}
|
|
281
266
|
function useHookCollector() {
|
|
282
267
|
const hooks = /* @__PURE__ */ new Map();
|
|
283
268
|
const fEntries = [];
|
|
284
269
|
const onFunctionEnter = (node) => {
|
|
285
|
-
const
|
|
286
|
-
const name =
|
|
270
|
+
const id = AST8__namespace.getFunctionIdentifier(node);
|
|
271
|
+
const name = id?.name;
|
|
287
272
|
if (name != null && isReactHookName(name)) {
|
|
288
|
-
const key = getId();
|
|
273
|
+
const key = shared.getId();
|
|
289
274
|
fEntries.push({ key, node });
|
|
290
275
|
hooks.set(key, {
|
|
291
|
-
|
|
292
|
-
|
|
276
|
+
id,
|
|
277
|
+
key,
|
|
293
278
|
kind: "function",
|
|
294
279
|
name,
|
|
295
280
|
node,
|
|
@@ -328,19 +313,13 @@ function useHookCollector() {
|
|
|
328
313
|
if (hook == null) {
|
|
329
314
|
return;
|
|
330
315
|
}
|
|
331
|
-
|
|
332
|
-
...hook,
|
|
333
|
-
hookCalls: [
|
|
334
|
-
...hook.hookCalls,
|
|
335
|
-
node
|
|
336
|
-
]
|
|
337
|
-
});
|
|
316
|
+
hook.hookCalls.push(node);
|
|
338
317
|
}
|
|
339
318
|
};
|
|
340
319
|
return { ctx, listeners };
|
|
341
320
|
}
|
|
342
321
|
var ERComponentHint = {
|
|
343
|
-
...
|
|
322
|
+
...JSX3__namespace.JSXValueHint,
|
|
344
323
|
// 1n << 0n - 1n << 63n are reserved for JSXValueHint
|
|
345
324
|
// Skip function component created by React.memo
|
|
346
325
|
SkipMemo: 1n << 64n,
|
|
@@ -356,6 +335,17 @@ var ERComponentHint = {
|
|
|
356
335
|
SkipClassProperty: 1n << 69n
|
|
357
336
|
};
|
|
358
337
|
var DEFAULT_COMPONENT_HINT = 0n | ERComponentHint.SkipBooleanLiteral | ERComponentHint.SkipMapCallback | ERComponentHint.SkipNumberLiteral | ERComponentHint.SkipStringLiteral | ERComponentHint.SkipUndefined | ERComponentHint.SkipEmptyArray | ERComponentHint.StrictArray | ERComponentHint.StrictConditional | ERComponentHint.StrictLogical;
|
|
338
|
+
var COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR = [
|
|
339
|
+
"AssignmentExpression",
|
|
340
|
+
"[type]",
|
|
341
|
+
"[operator='=']",
|
|
342
|
+
"[left.type='MemberExpression']",
|
|
343
|
+
"[left.property.name='displayName']"
|
|
344
|
+
].join("");
|
|
345
|
+
function isComponentDisplayNameAssignment(node) {
|
|
346
|
+
if (node == null) return false;
|
|
347
|
+
return node.type === types.AST_NODE_TYPES.AssignmentExpression && node.operator === "=" && node.left.type === types.AST_NODE_TYPES.MemberExpression && node.left.property.type === types.AST_NODE_TYPES.Identifier && node.left.property.name === "displayName";
|
|
348
|
+
}
|
|
359
349
|
|
|
360
350
|
// src/component/component-flag.ts
|
|
361
351
|
var ERClassComponentFlag = {
|
|
@@ -395,14 +385,6 @@ function getFunctionComponentIdentifier(node, context) {
|
|
|
395
385
|
}
|
|
396
386
|
return eff._;
|
|
397
387
|
}
|
|
398
|
-
var RE_COMPONENT_NAME = /^_?[A-Z]/u;
|
|
399
|
-
function getComponentNameFromIdentifier(node) {
|
|
400
|
-
if (node == null) return eff._;
|
|
401
|
-
return Array.isArray(node) ? node.map((n) => n.name).join(".") : node.name;
|
|
402
|
-
}
|
|
403
|
-
function isComponentName(name) {
|
|
404
|
-
return RE_COMPONENT_NAME.test(name);
|
|
405
|
-
}
|
|
406
388
|
function isClassComponent(node) {
|
|
407
389
|
if ("superClass" in node && node.superClass != null) {
|
|
408
390
|
const re = /^(?:Pure)?Component$/u;
|
|
@@ -428,9 +410,30 @@ function isPureComponent(node) {
|
|
|
428
410
|
return false;
|
|
429
411
|
}
|
|
430
412
|
|
|
431
|
-
// src/component/component-
|
|
413
|
+
// src/component/component-lifecycle.ts
|
|
414
|
+
function isComponentDidMount(node) {
|
|
415
|
+
return AST8__namespace.isMethodOrProperty(node) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "componentDidMount";
|
|
416
|
+
}
|
|
417
|
+
function isComponentWillUnmount(node) {
|
|
418
|
+
return AST8__namespace.isMethodOrProperty(node) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "componentWillUnmount";
|
|
419
|
+
}
|
|
420
|
+
function isComponentDidCatch(node) {
|
|
421
|
+
return AST8__namespace.isMethodOrProperty(node) && !node.static && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "componentDidCatch";
|
|
422
|
+
}
|
|
423
|
+
function isGetDerivedStateFromError(node) {
|
|
424
|
+
return AST8__namespace.isMethodOrProperty(node) && node.static && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "getDerivedStateFromError";
|
|
425
|
+
}
|
|
426
|
+
function isGetDerivedStateFromProps(node) {
|
|
427
|
+
return AST8__namespace.isMethodOrProperty(node) && node.static && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "getDerivedStateFromProps";
|
|
428
|
+
}
|
|
429
|
+
function isFunctionOfComponentDidMount(node) {
|
|
430
|
+
return AST8__namespace.isFunction(node) && isComponentDidMount(node.parent) && node.parent.value === node;
|
|
431
|
+
}
|
|
432
|
+
function isFunctionOfComponentWillUnmount(node) {
|
|
433
|
+
return AST8__namespace.isFunction(node) && isComponentWillUnmount(node.parent) && node.parent.value === node;
|
|
434
|
+
}
|
|
432
435
|
function isRenderMethodLike(node) {
|
|
433
|
-
return (node
|
|
436
|
+
return AST8__namespace.isMethodOrProperty(node) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "render" && node.parent.parent.type === types.AST_NODE_TYPES.ClassDeclaration;
|
|
434
437
|
}
|
|
435
438
|
function isFunctionOfRenderMethod(node) {
|
|
436
439
|
if (!isRenderMethodLike(node.parent)) {
|
|
@@ -441,12 +444,18 @@ function isFunctionOfRenderMethod(node) {
|
|
|
441
444
|
function isInsideRenderMethod(node) {
|
|
442
445
|
return AST8__namespace.findParentNode(node, (node2) => isRenderMethodLike(node2) && isClassComponent(node2.parent.parent)) != null;
|
|
443
446
|
}
|
|
444
|
-
|
|
445
|
-
|
|
447
|
+
var RE_COMPONENT_NAME = /^_?[A-Z]/u;
|
|
448
|
+
function getComponentNameFromIdentifier(node) {
|
|
449
|
+
if (node == null) return eff._;
|
|
450
|
+
return Array.isArray(node) ? node.map((n) => n.name).join(".") : node.name;
|
|
451
|
+
}
|
|
452
|
+
function isComponentName(name) {
|
|
453
|
+
return RE_COMPONENT_NAME.test(name);
|
|
454
|
+
}
|
|
446
455
|
function hasNoneOrValidComponentName(node, context) {
|
|
447
|
-
const
|
|
448
|
-
if (
|
|
449
|
-
const name = Array.isArray(
|
|
456
|
+
const id = getFunctionComponentIdentifier(node, context);
|
|
457
|
+
if (id == null) return true;
|
|
458
|
+
const name = Array.isArray(id) ? id.at(-1)?.name : id.name;
|
|
450
459
|
return name != null && isComponentName(name);
|
|
451
460
|
}
|
|
452
461
|
|
|
@@ -481,21 +490,22 @@ function hasValidHierarchy(node, context, hint) {
|
|
|
481
490
|
}
|
|
482
491
|
function getComponentFlag(initPath) {
|
|
483
492
|
let flag = ERFunctionComponentFlag.None;
|
|
484
|
-
if (initPath != null && AST8__namespace.hasCallInFunctionInitPath("memo"
|
|
493
|
+
if (initPath != null && AST8__namespace.hasCallInFunctionInitPath("memo", initPath)) {
|
|
485
494
|
flag |= ERFunctionComponentFlag.Memo;
|
|
486
495
|
}
|
|
487
|
-
if (initPath != null && AST8__namespace.hasCallInFunctionInitPath("forwardRef"
|
|
496
|
+
if (initPath != null && AST8__namespace.hasCallInFunctionInitPath("forwardRef", initPath)) {
|
|
488
497
|
flag |= ERFunctionComponentFlag.ForwardRef;
|
|
489
498
|
}
|
|
490
499
|
return flag;
|
|
491
500
|
}
|
|
492
|
-
function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
501
|
+
function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT, options = {}) {
|
|
502
|
+
const { collectDisplayName = false, collectHookCalls = false } = options;
|
|
493
503
|
const jsxCtx = { getScope: (node) => context.sourceCode.getScope(node) };
|
|
494
504
|
const components = /* @__PURE__ */ new Map();
|
|
495
505
|
const functionEntries = [];
|
|
496
506
|
const getCurrentEntry = () => functionEntries.at(-1);
|
|
497
507
|
const onFunctionEnter = (node) => {
|
|
498
|
-
const key = getId();
|
|
508
|
+
const key = shared.getId();
|
|
499
509
|
functionEntries.push({ key, node, hookCalls: [], isComponent: false });
|
|
500
510
|
};
|
|
501
511
|
const onFunctionExit = () => {
|
|
@@ -505,7 +515,7 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
505
515
|
return functionEntries.pop();
|
|
506
516
|
}
|
|
507
517
|
const shouldDrop = AST8__namespace.getNestedReturnStatements(entry.node.body).slice().reverse().some((r) => {
|
|
508
|
-
return context.sourceCode.getScope(r).block === entry.node && r.argument != null && !
|
|
518
|
+
return context.sourceCode.getScope(r).block === entry.node && r.argument != null && !JSX3__namespace.isJSXValue(r.argument, jsxCtx, hint);
|
|
509
519
|
});
|
|
510
520
|
if (shouldDrop) {
|
|
511
521
|
components.delete(entry.key);
|
|
@@ -531,17 +541,17 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
531
541
|
return;
|
|
532
542
|
}
|
|
533
543
|
const { body } = entry.node;
|
|
534
|
-
const isComponent = hasNoneOrValidComponentName(entry.node, context) &&
|
|
544
|
+
const isComponent = hasNoneOrValidComponentName(entry.node, context) && JSX3__namespace.isJSXValue(body, jsxCtx, hint) && hasValidHierarchy(entry.node, context, hint);
|
|
535
545
|
if (!isComponent) {
|
|
536
546
|
return;
|
|
537
547
|
}
|
|
538
548
|
const initPath = AST8__namespace.getFunctionInitPath(entry.node);
|
|
539
|
-
const
|
|
540
|
-
const name = getComponentNameFromIdentifier(
|
|
541
|
-
const key = getId();
|
|
549
|
+
const id = getFunctionComponentIdentifier(entry.node, context);
|
|
550
|
+
const name = getComponentNameFromIdentifier(id);
|
|
551
|
+
const key = shared.getId();
|
|
542
552
|
components.set(key, {
|
|
543
|
-
|
|
544
|
-
|
|
553
|
+
id,
|
|
554
|
+
key,
|
|
545
555
|
kind: "function",
|
|
546
556
|
name,
|
|
547
557
|
node: entry.node,
|
|
@@ -552,46 +562,47 @@ function useComponentCollector(context, hint = DEFAULT_COMPONENT_HINT) {
|
|
|
552
562
|
initPath
|
|
553
563
|
});
|
|
554
564
|
},
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
displayName
|
|
565
|
-
});
|
|
566
|
-
},
|
|
567
|
-
"CallExpression[type]:exit"(node) {
|
|
568
|
-
if (!isReactHookCall(node)) {
|
|
569
|
-
return;
|
|
565
|
+
...collectDisplayName ? {
|
|
566
|
+
[COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR](node) {
|
|
567
|
+
const { left, right } = node;
|
|
568
|
+
if (left.type !== types.AST_NODE_TYPES.MemberExpression) return;
|
|
569
|
+
const componentName = left.object.type === types.AST_NODE_TYPES.Identifier ? left.object.name : eff._;
|
|
570
|
+
const component = [...components.values()].findLast(({ name }) => name != null && name === componentName);
|
|
571
|
+
if (component == null) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
component.displayName = right;
|
|
570
575
|
}
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
576
|
+
} : {},
|
|
577
|
+
...collectHookCalls ? {
|
|
578
|
+
"CallExpression[type]:exit"(node) {
|
|
579
|
+
if (!isReactHookCall(node)) {
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
const entry = getCurrentEntry();
|
|
583
|
+
if (entry == null) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
entry.hookCalls.push(node);
|
|
574
587
|
}
|
|
575
|
-
|
|
576
|
-
functionEntries.push({ ...entry, hookCalls: [...entry.hookCalls, node] });
|
|
577
|
-
},
|
|
588
|
+
} : {},
|
|
578
589
|
"ReturnStatement[type]"(node) {
|
|
579
590
|
const entry = getCurrentEntry();
|
|
580
591
|
if (entry == null) {
|
|
581
592
|
return;
|
|
582
593
|
}
|
|
583
|
-
const isComponent = hasNoneOrValidComponentName(entry.node, context) &&
|
|
594
|
+
const isComponent = hasNoneOrValidComponentName(entry.node, context) && JSX3__namespace.isJSXValue(node.argument, jsxCtx, hint) && hasValidHierarchy(entry.node, context, hint);
|
|
584
595
|
if (!isComponent) {
|
|
585
596
|
return;
|
|
586
597
|
}
|
|
587
598
|
functionEntries.pop();
|
|
588
599
|
functionEntries.push({ ...entry, isComponent });
|
|
589
600
|
const initPath = AST8__namespace.getFunctionInitPath(entry.node);
|
|
590
|
-
const
|
|
591
|
-
const name = getComponentNameFromIdentifier(
|
|
601
|
+
const id = getFunctionComponentIdentifier(entry.node, context);
|
|
602
|
+
const name = getComponentNameFromIdentifier(id);
|
|
592
603
|
components.set(entry.key, {
|
|
593
|
-
|
|
594
|
-
|
|
604
|
+
id,
|
|
605
|
+
key: entry.key,
|
|
595
606
|
kind: "function",
|
|
596
607
|
name,
|
|
597
608
|
node: entry.node,
|
|
@@ -617,16 +628,16 @@ function useComponentCollectorLegacy() {
|
|
|
617
628
|
if (!isClassComponent(node)) {
|
|
618
629
|
return;
|
|
619
630
|
}
|
|
620
|
-
const
|
|
621
|
-
const key = getId();
|
|
631
|
+
const id = AST8__namespace.getClassIdentifier(node);
|
|
632
|
+
const key = shared.getId();
|
|
622
633
|
const flag = isPureComponent(node) ? ERClassComponentFlag.PureComponent : ERClassComponentFlag.None;
|
|
623
634
|
components.set(
|
|
624
635
|
key,
|
|
625
636
|
{
|
|
626
|
-
|
|
627
|
-
|
|
637
|
+
id,
|
|
638
|
+
key,
|
|
628
639
|
kind: "class",
|
|
629
|
-
name:
|
|
640
|
+
name: id?.name,
|
|
630
641
|
node,
|
|
631
642
|
// TODO: Get displayName of class component
|
|
632
643
|
displayName: eff._,
|
|
@@ -643,60 +654,22 @@ function useComponentCollectorLegacy() {
|
|
|
643
654
|
};
|
|
644
655
|
return { ctx, listeners };
|
|
645
656
|
}
|
|
646
|
-
|
|
647
|
-
if (node == null) return eff._;
|
|
648
|
-
return node.parent?.type === types.AST_NODE_TYPES.CallExpression && node.parent.callee !== node && node.parent.callee.type === types.AST_NODE_TYPES.Identifier && node.parent.arguments.at(0) === node && isUseEffectCallLoose(node.parent);
|
|
649
|
-
}
|
|
650
|
-
function isCleanupFunction(node) {
|
|
651
|
-
const nearReturn = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.ReturnStatement));
|
|
652
|
-
const nearFunction = AST8__namespace.findParentNodeGuard(node, AST8__namespace.isFunction);
|
|
653
|
-
const nearFunctionOfReturn = AST8__namespace.findParentNodeGuard(nearReturn, AST8__namespace.isFunction);
|
|
654
|
-
return nearFunction === nearFunctionOfReturn && isSetupFunction(nearFunction);
|
|
655
|
-
}
|
|
656
|
-
function isComponentDidMount(node) {
|
|
657
|
-
return AST8__namespace.isOneOf([types.AST_NODE_TYPES.MethodDefinition, types.AST_NODE_TYPES.PropertyDefinition])(node) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "componentDidMount";
|
|
658
|
-
}
|
|
659
|
-
function isComponentWillUnmount(node) {
|
|
660
|
-
return AST8__namespace.isOneOf([types.AST_NODE_TYPES.MethodDefinition, types.AST_NODE_TYPES.PropertyDefinition])(node) && node.key.type === types.AST_NODE_TYPES.Identifier && node.key.name === "componentWillUnmount";
|
|
661
|
-
}
|
|
662
|
-
function isComponentDidMountFunction(node) {
|
|
663
|
-
return AST8__namespace.isFunction(node) && isComponentDidMount(node.parent) && node.parent.value === node;
|
|
664
|
-
}
|
|
665
|
-
function isComponentWillUnmountFunction(node) {
|
|
666
|
-
return AST8__namespace.isFunction(node) && isComponentWillUnmount(node.parent) && node.parent.value === node;
|
|
667
|
-
}
|
|
668
|
-
var ERPhaseRelevance = eff.birecord({
|
|
657
|
+
var ERPhaseRelevance = birecord__default.default({
|
|
669
658
|
mount: "unmount",
|
|
670
659
|
setup: "cleanup"
|
|
671
660
|
});
|
|
672
661
|
var isInversePhase = eff.dual(2, (a, b) => ERPhaseRelevance.get(a) === b);
|
|
673
|
-
function isDirectValueOfRenderPropertyLoose(node) {
|
|
674
|
-
const matching = (node2) => {
|
|
675
|
-
return node2.type === types.AST_NODE_TYPES.Property && node2.key.type === types.AST_NODE_TYPES.Identifier && node2.key.name.startsWith("render");
|
|
676
|
-
};
|
|
677
|
-
return matching(node) || node.parent != null && matching(node.parent);
|
|
678
|
-
}
|
|
679
|
-
function isDeclaredInRenderPropLoose(node) {
|
|
680
|
-
if (isDirectValueOfRenderPropertyLoose(node)) {
|
|
681
|
-
return true;
|
|
682
|
-
}
|
|
683
|
-
const parent = AST8__namespace.findParentNodeGuard(node, AST8__namespace.is(types.AST_NODE_TYPES.JSXExpressionContainer))?.parent;
|
|
684
|
-
if (parent?.type !== types.AST_NODE_TYPES.JSXAttribute) {
|
|
685
|
-
return false;
|
|
686
|
-
}
|
|
687
|
-
return parent.name.type === types.AST_NODE_TYPES.JSXIdentifier && parent.name.name.startsWith("render");
|
|
688
|
-
}
|
|
689
662
|
function isRenderFunctionLoose(node, context) {
|
|
690
663
|
const { body, parent } = node;
|
|
691
664
|
if (AST8__namespace.getFunctionIdentifier(node)?.name.startsWith("render")) {
|
|
692
665
|
return parent.type === types.AST_NODE_TYPES.JSXExpressionContainer && parent.parent.type === types.AST_NODE_TYPES.JSXAttribute && parent.parent.name.type === types.AST_NODE_TYPES.JSXIdentifier && parent.parent.name.name.startsWith("render");
|
|
693
666
|
}
|
|
694
|
-
return
|
|
667
|
+
return JSX3__namespace.isJSXValue(
|
|
695
668
|
body,
|
|
696
669
|
{
|
|
697
670
|
getScope: (node2) => context.sourceCode.getScope(node2)
|
|
698
671
|
},
|
|
699
|
-
|
|
672
|
+
JSX3__namespace.JSXValueHint.SkipNullLiteral | JSX3__namespace.JSXValueHint.SkipUndefined | JSX3__namespace.JSXValueHint.StrictLogical | JSX3__namespace.JSXValueHint.StrictConditional
|
|
700
673
|
);
|
|
701
674
|
}
|
|
702
675
|
function isRenderPropLoose(node, context) {
|
|
@@ -705,11 +678,38 @@ function isRenderPropLoose(node, context) {
|
|
|
705
678
|
}
|
|
706
679
|
return node.name.name.startsWith("render") && node.value?.type === types.AST_NODE_TYPES.JSXExpressionContainer && AST8__namespace.isFunction(node.value.expression) && isRenderFunctionLoose(node.value.expression, context);
|
|
707
680
|
}
|
|
681
|
+
function isDirectValueOfRenderPropertyLoose(node) {
|
|
682
|
+
const matching = (node2) => {
|
|
683
|
+
return node2.type === types.AST_NODE_TYPES.Property && node2.key.type === types.AST_NODE_TYPES.Identifier && node2.key.name.startsWith("render");
|
|
684
|
+
};
|
|
685
|
+
return matching(node) || node.parent != null && matching(node.parent);
|
|
686
|
+
}
|
|
687
|
+
function isDeclaredInRenderPropLoose(node) {
|
|
688
|
+
if (isDirectValueOfRenderPropertyLoose(node)) {
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
const parent = AST8__namespace.findParentNode(node, AST8__namespace.is(types.AST_NODE_TYPES.JSXExpressionContainer))?.parent;
|
|
692
|
+
if (parent?.type !== types.AST_NODE_TYPES.JSXAttribute) {
|
|
693
|
+
return false;
|
|
694
|
+
}
|
|
695
|
+
return parent.name.type === types.AST_NODE_TYPES.JSXIdentifier && parent.name.name.startsWith("render");
|
|
696
|
+
}
|
|
708
697
|
function isThisSetState(node) {
|
|
709
698
|
const { callee } = node;
|
|
710
699
|
return callee.type === types.AST_NODE_TYPES.MemberExpression && AST8__namespace.isThisExpression(callee.object) && callee.property.type === types.AST_NODE_TYPES.Identifier && callee.property.name === "setState";
|
|
711
700
|
}
|
|
701
|
+
function isFunctionOfUseEffectSetup(node) {
|
|
702
|
+
if (node == null) return eff._;
|
|
703
|
+
return node.parent?.type === types.AST_NODE_TYPES.CallExpression && node.parent.callee !== node && node.parent.callee.type === types.AST_NODE_TYPES.Identifier && node.parent.arguments.at(0) === node && isUseEffectCallLoose(node.parent);
|
|
704
|
+
}
|
|
705
|
+
function isFunctionOfUseEffectCleanup(node) {
|
|
706
|
+
const nearReturn = AST8__namespace.findParentNode(node, AST8__namespace.is(types.AST_NODE_TYPES.ReturnStatement));
|
|
707
|
+
const nearFunction = AST8__namespace.findParentNode(node, AST8__namespace.isFunction);
|
|
708
|
+
const nearFunctionOfReturn = AST8__namespace.findParentNode(nearReturn, AST8__namespace.isFunction);
|
|
709
|
+
return nearFunction === nearFunctionOfReturn && isFunctionOfUseEffectSetup(nearFunction);
|
|
710
|
+
}
|
|
712
711
|
|
|
712
|
+
exports.COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR = COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR;
|
|
713
713
|
exports.DEFAULT_COMPONENT_HINT = DEFAULT_COMPONENT_HINT;
|
|
714
714
|
exports.ERClassComponentFlag = ERClassComponentFlag;
|
|
715
715
|
exports.ERComponentHint = ERComponentHint;
|
|
@@ -718,9 +718,7 @@ exports.ERPhaseRelevance = ERPhaseRelevance;
|
|
|
718
718
|
exports.RE_COMPONENT_NAME = RE_COMPONENT_NAME;
|
|
719
719
|
exports.RE_HOOK_NAME = RE_HOOK_NAME;
|
|
720
720
|
exports.getComponentNameFromIdentifier = getComponentNameFromIdentifier;
|
|
721
|
-
exports.getElementNameAndRepresentName = getElementNameAndRepresentName;
|
|
722
721
|
exports.getFunctionComponentIdentifier = getFunctionComponentIdentifier;
|
|
723
|
-
exports.getId = getId;
|
|
724
722
|
exports.hasNoneOrValidComponentName = hasNoneOrValidComponentName;
|
|
725
723
|
exports.isCallFromReact = isCallFromReact;
|
|
726
724
|
exports.isCallFromReactMember = isCallFromReactMember;
|
|
@@ -736,14 +734,13 @@ exports.isChildrenOnlyCall = isChildrenOnlyCall;
|
|
|
736
734
|
exports.isChildrenToArray = isChildrenToArray;
|
|
737
735
|
exports.isChildrenToArrayCall = isChildrenToArrayCall;
|
|
738
736
|
exports.isClassComponent = isClassComponent;
|
|
739
|
-
exports.isCleanupFunction = isCleanupFunction;
|
|
740
737
|
exports.isCloneElement = isCloneElement;
|
|
741
738
|
exports.isCloneElementCall = isCloneElementCall;
|
|
739
|
+
exports.isComponentDidCatch = isComponentDidCatch;
|
|
742
740
|
exports.isComponentDidMount = isComponentDidMount;
|
|
743
|
-
exports.
|
|
741
|
+
exports.isComponentDisplayNameAssignment = isComponentDisplayNameAssignment;
|
|
744
742
|
exports.isComponentName = isComponentName;
|
|
745
743
|
exports.isComponentWillUnmount = isComponentWillUnmount;
|
|
746
|
-
exports.isComponentWillUnmountFunction = isComponentWillUnmountFunction;
|
|
747
744
|
exports.isCreateContext = isCreateContext;
|
|
748
745
|
exports.isCreateContextCall = isCreateContextCall;
|
|
749
746
|
exports.isCreateElement = isCreateElement;
|
|
@@ -756,7 +753,13 @@ exports.isForwardRef = isForwardRef;
|
|
|
756
753
|
exports.isForwardRefCall = isForwardRefCall;
|
|
757
754
|
exports.isFromReact = isFromReact;
|
|
758
755
|
exports.isFromReactMember = isFromReactMember;
|
|
756
|
+
exports.isFunctionOfComponentDidMount = isFunctionOfComponentDidMount;
|
|
757
|
+
exports.isFunctionOfComponentWillUnmount = isFunctionOfComponentWillUnmount;
|
|
759
758
|
exports.isFunctionOfRenderMethod = isFunctionOfRenderMethod;
|
|
759
|
+
exports.isFunctionOfUseEffectCleanup = isFunctionOfUseEffectCleanup;
|
|
760
|
+
exports.isFunctionOfUseEffectSetup = isFunctionOfUseEffectSetup;
|
|
761
|
+
exports.isGetDerivedStateFromError = isGetDerivedStateFromError;
|
|
762
|
+
exports.isGetDerivedStateFromProps = isGetDerivedStateFromProps;
|
|
760
763
|
exports.isInitializedFromReact = isInitializedFromReact;
|
|
761
764
|
exports.isInsideCreateElementProps = isInsideCreateElementProps;
|
|
762
765
|
exports.isInsideReactHook = isInsideReactHook;
|
|
@@ -776,7 +779,6 @@ exports.isReactHookName = isReactHookName;
|
|
|
776
779
|
exports.isRenderFunctionLoose = isRenderFunctionLoose;
|
|
777
780
|
exports.isRenderMethodLike = isRenderMethodLike;
|
|
778
781
|
exports.isRenderPropLoose = isRenderPropLoose;
|
|
779
|
-
exports.isSetupFunction = isSetupFunction;
|
|
780
782
|
exports.isThisSetState = isThisSetState;
|
|
781
783
|
exports.isUseCallbackCall = isUseCallbackCall;
|
|
782
784
|
exports.isUseContextCall = isUseContextCall;
|