1- import { isOneOf , NodeType , type TSESTreeFunction } from "@eslint-react/ast" ;
1+ import { getFunctionIdentifier , NodeType , type TSESTreeFunction } from "@eslint-react/ast" ;
22import { O } from "@eslint-react/tools" ;
33import type { RuleContext } from "@eslint-react/types" ;
44import type { TSESTree } from "@typescript-eslint/types" ;
55
6+ import { isReactHookCallWithNameLoose } from "../hook" ;
67import { isForwardRefCall , isMemoCall } from "../react-api" ;
78
8- function isMemoOrForwardRefCall ( node : TSESTree . Node , context : RuleContext ) {
9+ function isComponentWrapperCall ( node : TSESTree . Node , context : RuleContext ) {
910 if ( node . type !== NodeType . CallExpression ) return false ;
1011
11- return isMemoCall ( node , context ) || isForwardRefCall ( node , context ) ;
12+ return isMemoCall ( node , context )
13+ || isForwardRefCall ( node , context )
14+ || isReactHookCallWithNameLoose ( "useCallback" ) ( node ) ;
1215}
1316
1417export function getFunctionComponentIdentifier (
1518 node : TSESTreeFunction ,
1619 context : RuleContext ,
1720) : O . Option < TSESTree . Identifier | TSESTree . Identifier [ ] > {
18- const { id, parent } = node ;
19- if ( node . type === NodeType . FunctionDeclaration ) return O . fromNullable ( id ) ;
20- if (
21- parent . type === NodeType . VariableDeclarator
22- && parent . id . type === NodeType . Identifier
23- && parent . parent . type === NodeType . VariableDeclaration
24- ) {
25- return O . some ( parent . id ) ;
21+ const functionId = getFunctionIdentifier ( node ) ;
22+
23+ if ( O . isSome ( functionId ) ) {
24+ return functionId ;
2625 }
2726
27+ const { parent } = node ;
28+
2829 if (
2930 parent . type === NodeType . CallExpression
30- && isMemoOrForwardRefCall ( parent , context )
31+ && isComponentWrapperCall ( parent , context )
3132 && parent . parent . type === NodeType . VariableDeclarator
3233 && parent . parent . id . type === NodeType . Identifier
3334 && parent . parent . parent . type === NodeType . VariableDeclaration
@@ -37,36 +38,15 @@ export function getFunctionComponentIdentifier(
3738
3839 if (
3940 parent . type === NodeType . CallExpression
40- && isMemoOrForwardRefCall ( parent , context )
41+ && isComponentWrapperCall ( parent , context )
4142 && parent . parent . type === NodeType . CallExpression
42- && isMemoOrForwardRefCall ( parent . parent , context )
43+ && isComponentWrapperCall ( parent . parent , context )
4344 && parent . parent . parent . type === NodeType . VariableDeclarator
4445 && parent . parent . parent . id . type === NodeType . Identifier
4546 && parent . parent . parent . parent . type === NodeType . VariableDeclaration
4647 ) {
4748 return O . some ( parent . parent . parent . id ) ;
4849 }
4950
50- if (
51- parent . type === NodeType . Property
52- && parent . key . type === NodeType . Identifier
53- && parent . parent . type === NodeType . ObjectExpression
54- && parent . parent . parent . type === NodeType . VariableDeclarator
55- && parent . parent . parent . id . type === NodeType . Identifier
56- && parent . parent . parent . parent . type === NodeType . VariableDeclaration
57- ) {
58- return O . some ( [ parent . parent . parent . id , parent . key ] ) ;
59- }
60-
61- if (
62- isOneOf ( [ NodeType . MethodDefinition , NodeType . PropertyDefinition ] ) ( parent )
63- && parent . key . type === NodeType . Identifier
64- && parent . parent . type === NodeType . ClassBody
65- && parent . parent . parent . type === NodeType . ClassDeclaration
66- && parent . parent . parent . id ?. type === NodeType . Identifier
67- ) {
68- return O . some ( [ parent . parent . parent . id , parent . key ] ) ;
69- }
70-
7151 return O . none ( ) ;
7252}
0 commit comments