@eslint-react/core 5.30.0-beta.1 → 5.30.0-beta.11
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 +11 -3
- package/dist/index.js +23 -10
- package/package.json +8 -9
package/dist/index.d.ts
CHANGED
|
@@ -376,8 +376,10 @@ export declare function getFunctionId(node: TSESTree.Expression | TSESTreeFuncti
|
|
|
376
376
|
* @remarks
|
|
377
377
|
* Higher-order component wrappers (ex: `memo`, `forwardRef`) are only
|
|
378
378
|
* recognized when the call expression initializes a variable declarator
|
|
379
|
-
* directly
|
|
380
|
-
*
|
|
379
|
+
* directly and the function occupies a component position: the first argument
|
|
380
|
+
* for `memo`/`forwardRef`, or any argument for other calls (never the callee).
|
|
381
|
+
* HOCs inside object properties (ex: `{ Nav: memo(() => {}) }`) are not
|
|
382
|
+
* supported and yield `null`.
|
|
381
383
|
*
|
|
382
384
|
* @param node The function node to analyze.
|
|
383
385
|
* @returns The function initialization path or `null` if not identifiable.
|
|
@@ -581,6 +583,9 @@ export declare function isFunctionComponentWrapperCall(context: RuleContext, nod
|
|
|
581
583
|
export declare function isFunctionComponentWrapperCallback(context: RuleContext, node: TSESTreeFunction): boolean;
|
|
582
584
|
/**
|
|
583
585
|
* Get function component identifier from `const Component = memo(() => {});`.
|
|
586
|
+
* Only the first argument of a component wrapper is treated as the wrapped
|
|
587
|
+
* component; later arguments (ex: memo's propsAreEqual comparator) do not
|
|
588
|
+
* inherit the surrounding binding.
|
|
584
589
|
* @param context The rule context.
|
|
585
590
|
* @param node The AST node to get the function component identifier from.
|
|
586
591
|
* @internal
|
|
@@ -620,7 +625,10 @@ export declare const FunctionComponentDetectionHint: {
|
|
|
620
625
|
readonly DoNotIncludeFunctionDefinedAsObjectMethod: bigint;
|
|
621
626
|
/** Exclude functions defined as array expression elements from component detection. */
|
|
622
627
|
readonly DoNotIncludeFunctionDefinedAsArrayExpressionElement: bigint;
|
|
623
|
-
/**
|
|
628
|
+
/**
|
|
629
|
+
* Exclude functions defined as array pattern elements or as their default values from
|
|
630
|
+
* component detection (ex: `const [Nav = () => {}] = arr`).
|
|
631
|
+
*/
|
|
624
632
|
readonly DoNotIncludeFunctionDefinedAsArrayPatternElement: bigint;
|
|
625
633
|
/** Exclude functions defined as array flatMap callbacks (named or anonymous) from component detection. */
|
|
626
634
|
readonly DoNotIncludeFunctionDefinedAsArrayFlatMapCallback: bigint;
|
package/dist/index.js
CHANGED
|
@@ -311,6 +311,13 @@ function getFunctionId(node) {
|
|
|
311
311
|
}
|
|
312
312
|
return null;
|
|
313
313
|
}
|
|
314
|
+
function isComponentWrapperCall(node) {
|
|
315
|
+
const name = Extract.getCalleeName(node);
|
|
316
|
+
return name === "memo" || name === "forwardRef";
|
|
317
|
+
}
|
|
318
|
+
function isComponentArgumentPosition(call, child) {
|
|
319
|
+
return isComponentWrapperCall(call) ? call.arguments[0] === child : call.arguments.includes(child);
|
|
320
|
+
}
|
|
314
321
|
function getFunctionInitPath(node) {
|
|
315
322
|
if (node.type === AST_NODE_TYPES.FunctionDeclaration) return [node];
|
|
316
323
|
let parent = node.parent;
|
|
@@ -325,13 +332,13 @@ function getFunctionInitPath(node) {
|
|
|
325
332
|
parent,
|
|
326
333
|
node
|
|
327
334
|
];
|
|
328
|
-
case parent.type === AST_NODE_TYPES.CallExpression && parent
|
|
335
|
+
case parent.type === AST_NODE_TYPES.CallExpression && isComponentArgumentPosition(parent, child) && parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return [
|
|
329
336
|
parent.parent.parent,
|
|
330
337
|
parent.parent,
|
|
331
338
|
parent,
|
|
332
339
|
node
|
|
333
340
|
];
|
|
334
|
-
case parent.type === AST_NODE_TYPES.CallExpression && parent
|
|
341
|
+
case parent.type === AST_NODE_TYPES.CallExpression && isComponentArgumentPosition(parent, child) && parent.parent.type === AST_NODE_TYPES.CallExpression && isComponentArgumentPosition(parent.parent, parent) && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return [
|
|
335
342
|
parent.parent.parent.parent,
|
|
336
343
|
parent.parent.parent,
|
|
337
344
|
parent.parent,
|
|
@@ -422,7 +429,8 @@ function isJSXLike(context, node, hint = DEFAULT_JSX_DETECTION_HINT) {
|
|
|
422
429
|
case AST_NODE_TYPES.TSAsExpression:
|
|
423
430
|
case AST_NODE_TYPES.TSSatisfiesExpression:
|
|
424
431
|
case AST_NODE_TYPES.TSTypeAssertion:
|
|
425
|
-
case AST_NODE_TYPES.TSNonNullExpression:
|
|
432
|
+
case AST_NODE_TYPES.TSNonNullExpression:
|
|
433
|
+
case AST_NODE_TYPES.ChainExpression: return visit(node.expression);
|
|
426
434
|
case AST_NODE_TYPES.AwaitExpression: return visit(node.argument);
|
|
427
435
|
case AST_NODE_TYPES.Literal: return match(node.value).with(P.nullish, () => !(hint & JSXDetectionHint.DoNotIncludeJSXWithNullValue)).with(P.boolean, () => !(hint & JSXDetectionHint.DoNotIncludeJSXWithBooleanValue)).with(P.string, () => !(hint & JSXDetectionHint.DoNotIncludeJSXWithStringValue)).with(P.number, () => !(hint & JSXDetectionHint.DoNotIncludeJSXWithNumberValue)).with(P.bigint, () => !(hint & JSXDetectionHint.DoNotIncludeJSXWithBigIntValue)).with(P.instanceOf(RegExp), () => false).exhaustive();
|
|
428
436
|
case AST_NODE_TYPES.TemplateLiteral: return !(hint & JSXDetectionHint.DoNotIncludeJSXWithStringValue);
|
|
@@ -484,10 +492,14 @@ function getFunctionComponentId(context, node) {
|
|
|
484
492
|
const functionId = getFunctionId(node);
|
|
485
493
|
if (functionId != null) return functionId;
|
|
486
494
|
let parent = node.parent;
|
|
487
|
-
|
|
495
|
+
let child = node;
|
|
496
|
+
while (Check.isTypeExpression(parent)) {
|
|
497
|
+
child = parent;
|
|
498
|
+
parent = parent.parent;
|
|
499
|
+
}
|
|
488
500
|
switch (true) {
|
|
489
|
-
case parent.type === AST_NODE_TYPES.CallExpression && isFunctionComponentWrapperCall(context, parent) && parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return parent.parent.id;
|
|
490
|
-
case parent.type === AST_NODE_TYPES.CallExpression && isFunctionComponentWrapperCall(context, parent) && parent.parent.type === AST_NODE_TYPES.CallExpression && isFunctionComponentWrapperCall(context, parent.parent) && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return parent.parent.parent.id;
|
|
501
|
+
case parent.type === AST_NODE_TYPES.CallExpression && parent.arguments[0] === child && isFunctionComponentWrapperCall(context, parent) && parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return parent.parent.id;
|
|
502
|
+
case parent.type === AST_NODE_TYPES.CallExpression && parent.arguments[0] === child && isFunctionComponentWrapperCall(context, parent) && parent.parent.type === AST_NODE_TYPES.CallExpression && parent.parent.arguments[0] === parent && isFunctionComponentWrapperCall(context, parent.parent) && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return parent.parent.parent.id;
|
|
491
503
|
default: return null;
|
|
492
504
|
}
|
|
493
505
|
}
|
|
@@ -522,6 +534,7 @@ function isFunctionComponentDefinition(context, node, hint) {
|
|
|
522
534
|
while (Check.isTypeExpression(parent)) parent = parent.parent;
|
|
523
535
|
if (isRenderMethodCallback(node)) return false;
|
|
524
536
|
if (isCreateElementChildrenArgument(context, node)) return false;
|
|
537
|
+
if (Check.isCallExpression(parent) && isFunctionComponentWrapperCall(context, parent) && !isFunctionComponentWrapperCallback(context, node)) return false;
|
|
525
538
|
const [parentCallee, parentCalleeName] = parent.type === AST_NODE_TYPES.CallExpression ? [Extract.unwrap(parent.callee), Extract.getCalleeName(parent)] : [null, null];
|
|
526
539
|
switch (true) {
|
|
527
540
|
case Check.isOneOf([AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression])(node) && parent.type === AST_NODE_TYPES.Property && parent.parent.type === AST_NODE_TYPES.ObjectExpression:
|
|
@@ -533,7 +546,7 @@ function isFunctionComponentDefinition(context, node, hint) {
|
|
|
533
546
|
case Check.isOneOf([AST_NODE_TYPES.ArrowFunctionExpression, AST_NODE_TYPES.FunctionExpression])(node) && parent.type === AST_NODE_TYPES.PropertyDefinition:
|
|
534
547
|
if (hint & FunctionComponentDetectionHint.DoNotIncludeFunctionDefinedAsClassProperty) return false;
|
|
535
548
|
break;
|
|
536
|
-
case parent.type === AST_NODE_TYPES.ArrayPattern:
|
|
549
|
+
case parent.type === AST_NODE_TYPES.ArrayPattern || Check.isArrayPatternElementDefault(parent):
|
|
537
550
|
if (hint & FunctionComponentDetectionHint.DoNotIncludeFunctionDefinedAsArrayPatternElement) return false;
|
|
538
551
|
break;
|
|
539
552
|
case parent.type === AST_NODE_TYPES.ArrayExpression:
|
|
@@ -548,11 +561,11 @@ function isFunctionComponentDefinition(context, node, hint) {
|
|
|
548
561
|
case parent.type === AST_NODE_TYPES.CallExpression && getFunctionId(node) == null && !isFunctionComponentWrapperCall(context, parent) && !isCreateElementCall(context, parent): if (hint & FunctionComponentDetectionHint.DoNotIncludeFunctionDefinedAsArbitraryCallExpressionCallback) return false;
|
|
549
562
|
}
|
|
550
563
|
const significantParent = Traverse.findParent(node, Check.isOneOf([
|
|
551
|
-
AST_NODE_TYPES.JSXExpressionContainer,
|
|
552
564
|
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
553
|
-
AST_NODE_TYPES.FunctionExpression,
|
|
554
565
|
AST_NODE_TYPES.Property,
|
|
555
|
-
AST_NODE_TYPES.
|
|
566
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
567
|
+
AST_NODE_TYPES.ClassBody,
|
|
568
|
+
AST_NODE_TYPES.JSXExpressionContainer
|
|
556
569
|
]));
|
|
557
570
|
if (significantParent == null) return true;
|
|
558
571
|
return significantParent.type !== AST_NODE_TYPES.JSXExpressionContainer;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/core",
|
|
3
|
-
"version": "5.30.0-beta.
|
|
3
|
+
"version": "5.30.0-beta.11",
|
|
4
4
|
"description": "ESLint React's ESLint utility module for static analysis of React core APIs and patterns.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eslint-react/ast": "5.30.0-beta.
|
|
33
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
34
|
-
"@eslint-react/jsx": "5.30.0-beta.
|
|
35
|
-
"@eslint-react/shared": "5.30.0-beta.
|
|
36
|
-
"@eslint-react/var": "5.30.0-beta.
|
|
37
|
-
"@typescript-eslint/types": "^8.72.
|
|
38
|
-
"@typescript-eslint/utils": "^8.72.
|
|
32
|
+
"@eslint-react/ast": "5.30.0-beta.11",
|
|
33
|
+
"@eslint-react/eslint": "5.30.0-beta.11",
|
|
34
|
+
"@eslint-react/jsx": "5.30.0-beta.11",
|
|
35
|
+
"@eslint-react/shared": "5.30.0-beta.11",
|
|
36
|
+
"@eslint-react/var": "5.30.0-beta.11",
|
|
37
|
+
"@typescript-eslint/types": "^8.72.1",
|
|
38
|
+
"@typescript-eslint/utils": "^8.72.1",
|
|
39
39
|
"ts-pattern": "^5.9.2"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
@@ -44,7 +44,6 @@
|
|
|
44
44
|
"@local/testkit": "0.0.0",
|
|
45
45
|
"@tsconfig/node20": "^20.1.10",
|
|
46
46
|
"@types/node": "^20.19.43",
|
|
47
|
-
"@typescript-eslint/typescript-estree": "^8.72.0",
|
|
48
47
|
"dedent": "^1.7.2",
|
|
49
48
|
"eslint": "^10.13.0",
|
|
50
49
|
"tsdown": "^0.23.2",
|