@@ -18,15 +18,8 @@ import { hasNoneOrLooseComponentName } from "./component-name";
1818
1919const idGen = new IdGenerator ( "function_component_" ) ;
2020
21- type FunctionEntry = {
22- key : string ;
23- node : AST . TSESTreeFunction ;
24- hookCalls : TSESTree . CallExpression [ ] ;
25- isComponent : boolean ;
21+ type FunctionEntry = FunctionComponentSemanticNode & {
2622 isComponentDefinition : boolean ;
27- isExportDefault : boolean ;
28- isExportDefaultDeclaration : boolean ;
29- rets : TSESTree . ReturnStatement [ "argument" ] [ ] ;
3023} ;
3124
3225export declare namespace useComponentCollector {
@@ -71,12 +64,21 @@ export function useComponentCollector(
7164 const exp = AST . findParentNode ( node , ( n ) => n . type === T . ExportDefaultDeclaration ) ;
7265 const isExportDefault = exp != null ;
7366 const isExportDefaultDeclaration = exp != null && AST . getUnderlyingExpression ( exp . declaration ) === node ;
67+ const id = getFunctionComponentId ( context , node ) ;
68+ const name = id == null ? unit : AST . toStringFormat ( id , getText ) ;
69+ const initPath = AST . getFunctionInitPath ( node ) ;
7470 functionEntries . push ( {
71+ id : getFunctionComponentId ( context , node ) ,
7572 key,
73+ kind : "function" ,
74+ name,
7675 node,
76+ displayName : unit ,
77+ flag : getComponentFlagFromInitPath ( initPath ) ,
78+ hint,
7779 hookCalls : [ ] ,
78- isComponent : false ,
79- isComponentDefinition : isComponentDefinition ( context , node , hint ) ,
80+ initPath ,
81+ isComponentDefinition : hasNoneOrLooseComponentName ( context , node ) && isComponentDefinition ( context , node , hint ) ,
8082 isExportDefault,
8183 isExportDefaultDeclaration,
8284 rets : [ ] ,
@@ -103,32 +105,12 @@ export function useComponentCollector(
103105 "ArrowFunctionExpression[body.type!='BlockStatement']" ( ) {
104106 const entry = getCurrentEntry ( ) ;
105107 if ( entry == null ) return ;
106- // If the function is not a component definition, skip the rest of the checks
107- if ( ! entry . isComponentDefinition ) return ;
108108 const { body } = entry . node ;
109109 if ( body . type === T . BlockStatement ) return ;
110- const isComponent = hasNoneOrLooseComponentName ( context , entry . node )
111- && isJsxLike ( context . sourceCode , body , hint ) ;
112- if ( ! isComponent ) return ;
113- const initPath = AST . getFunctionInitPath ( entry . node ) ;
114- const id = getFunctionComponentId ( context , entry . node ) ;
115- const key = entry . key ;
116- const name = id == null ? unit : AST . toStringFormat ( id , getText ) ;
117- components . set ( key , {
118- id,
119- key,
120- kind : "function" ,
121- name,
122- node : entry . node ,
123- displayName : unit ,
124- flag : getComponentFlagFromInitPath ( initPath ) ,
125- hint,
126- hookCalls : entry . hookCalls ,
127- initPath,
128- isExportDefault : entry . isExportDefault ,
129- isExportDefaultDeclaration : entry . isExportDefaultDeclaration ,
130- rets : [ body ] ,
131- } ) ;
110+ entry . rets . push ( body ) ;
111+ if ( ! entry . isComponentDefinition ) return ;
112+ if ( ! components . has ( entry . key ) && ! isJsxLike ( context . sourceCode , body , hint ) ) return ;
113+ components . set ( entry . key , entry ) ;
132114 } ,
133115 ...collectDisplayName
134116 ? {
@@ -157,33 +139,11 @@ export function useComponentCollector(
157139 ReturnStatement ( node : TSESTree . ReturnStatement ) {
158140 const entry = getCurrentEntry ( ) ;
159141 if ( entry == null ) return ;
160- // If the function is not a component definition, skip the rest of the checks
142+ entry . rets . push ( node . argument ) ;
161143 if ( ! entry . isComponentDefinition ) return ;
162144 const { argument } = node ;
163- entry . rets . push ( argument ) ;
164- const isComponent = hasNoneOrLooseComponentName ( context , entry . node )
165- && isJsxLike ( context . sourceCode , argument , hint ) ;
166- if ( ! isComponent ) return ;
167- entry . isComponent = true ;
168- const initPath = AST . getFunctionInitPath ( entry . node ) ;
169- const id = getFunctionComponentId ( context , entry . node ) ;
170- const key = entry . key ;
171- const name = id == null ? unit : AST . toStringFormat ( id , getText ) ;
172- components . set ( key , {
173- id,
174- key,
175- kind : "function" ,
176- name,
177- node : entry . node ,
178- displayName : unit ,
179- flag : getComponentFlagFromInitPath ( initPath ) ,
180- hint,
181- hookCalls : entry . hookCalls ,
182- initPath,
183- isExportDefault : entry . isExportDefault ,
184- isExportDefaultDeclaration : entry . isExportDefaultDeclaration ,
185- rets : entry . rets ,
186- } ) ;
145+ if ( ! components . has ( entry . key ) && ! isJsxLike ( context . sourceCode , argument , hint ) ) return ;
146+ components . set ( entry . key , entry ) ;
187147 } ,
188148 } as const satisfies ESLintUtils . RuleListener ;
189149 return { ctx, visitor } as const ;
0 commit comments