@eslint-react/core 5.30.0-beta.4 → 5.30.0-beta.5

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 CHANGED
@@ -369,15 +369,17 @@ export type FunctionSemanticNode = ClientFunctionSemanticNode | ServerFunctionSe
369
369
  * @param node The function node to analyze.
370
370
  * @returns The identifier node if found, `null` otherwise.
371
371
  */
372
- export declare function getFunctionId(node: TSESTree.Expression | TSESTreeFunction): TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpressionWithBlockBody | TSESTree.ArrowFunctionExpressionWithExpressionBody | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.TemplateLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldNoStarExpression | TSESTree.YieldStarExpression | TSESTree.PrivateIdentifier | null;
372
+ export declare function getFunctionId(node: TSESTree.Expression | TSESTreeFunction): TSESTree.ClassExpression | TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpressionWithBlockBody | TSESTree.ArrowFunctionExpressionWithExpressionBody | TSESTree.CallExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.TemplateLiteral | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSNonNullExpression | TSESTree.TSTypeAssertion | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.ChainExpression | TSESTree.ConditionalExpression | TSESTree.ImportExpression | TSESTree.LogicalExpression | TSESTree.NewExpression | TSESTree.PrivateIdentifier | TSESTree.TSInstantiationExpression | TSESTree.TSSatisfiesExpression | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldNoStarExpression | TSESTree.YieldStarExpression | null;
373
373
  /**
374
374
  * Get the initialization path of a function node in the AST.
375
375
  *
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. HOCs inside object properties (ex: `{ Nav: memo(() => {}) }`)
380
- * are not supported and yield `null`.
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
- /** Exclude functions defined as array pattern elements from component detection. */
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.arguments.includes(child) && parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return [
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.arguments.includes(child) && parent.parent.type === AST_NODE_TYPES.CallExpression && parent.parent.arguments.includes(parent) && parent.parent.parent.type === AST_NODE_TYPES.VariableDeclarator: return [
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: return visit(node.expression);
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
- while (Check.isTypeExpression(parent)) parent = parent.parent;
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.ClassBody
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.4",
3
+ "version": "5.30.0-beta.5",
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.4",
33
- "@eslint-react/eslint": "5.30.0-beta.4",
34
- "@eslint-react/jsx": "5.30.0-beta.4",
35
- "@eslint-react/shared": "5.30.0-beta.4",
36
- "@eslint-react/var": "5.30.0-beta.4",
37
- "@typescript-eslint/types": "^8.72.0",
38
- "@typescript-eslint/utils": "^8.72.0",
32
+ "@eslint-react/ast": "5.30.0-beta.5",
33
+ "@eslint-react/eslint": "5.30.0-beta.5",
34
+ "@eslint-react/jsx": "5.30.0-beta.5",
35
+ "@eslint-react/shared": "5.30.0-beta.5",
36
+ "@eslint-react/var": "5.30.0-beta.5",
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": {