Skip to content

Commit 631adf4

Browse files
Sebastian WendorfSebastian Wendorf
authored andcommitted
Added detection of React function components returning ReactNode
1 parent 96ac933 commit 631adf4

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

typescript/src/react/post-processors/react-component.post-processor.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ReactComponentPostProcessor extends PostProcessor {
1818
for (const func of allFunctions) {
1919
if (
2020
func.returnType instanceof LCETypeDeclared &&
21-
(func.returnType.fqn.globalFqn === '"react".React.JSX.Element' || func.returnType.fqn.globalFqn === '"react".JSX.Element')
21+
isComponentReturnType(func.returnType.fqn.globalFqn)
2222
) {
2323
const component = new LCEReactComponent(func.fqn, func.functionName, []);
2424
if (func.metadata.has(JSXDependencyContextProcessor.JSX_DEPENDENCY_METADATA)) {
@@ -34,8 +34,7 @@ export class ReactComponentPostProcessor extends PostProcessor {
3434
if (
3535
(variable.type instanceof LCETypeFunction &&
3636
variable.type.returnType instanceof LCETypeDeclared &&
37-
(variable.type.returnType.fqn.globalFqn === '"react".React.JSX.Element' ||
38-
variable.type.returnType.fqn.globalFqn === '"react".JSX.Element')) ||
37+
isComponentReturnType(variable.type.returnType.fqn.globalFqn)) ||
3938
(variable.type instanceof LCETypeDeclared && isReactFunctionComponentType(variable.type.fqn.globalFqn))
4039
) {
4140
const component = new LCEReactComponent(variable.fqn, variable.variableName, []);
@@ -63,18 +62,23 @@ export class ReactComponentPostProcessor extends PostProcessor {
6362
}
6463
}
6564

65+
function isComponentReturnType(globalFqn: string): boolean {
66+
return [
67+
'"react".React.ReactNode',
68+
'"react".React.JSX.Element',
69+
'"react".JSX.Element'
70+
].includes(globalFqn);
71+
}
72+
6673
/**
6774
* returns whether the provided fqn belongs to an interface/type describing a React function component
6875
*/
6976
function isReactFunctionComponentType(globalFqn: string): boolean {
70-
switch (globalFqn) {
71-
case '"react".React.FC':
72-
case '"react".React.ExoticComponent':
73-
case '"react".React.NamedExoticComponent':
74-
case '"react".React.ForwardRefExoticComponent':
75-
case '"react".React.MemoExoticComponent':
76-
return true;
77-
default:
78-
return false;
79-
}
77+
return [
78+
'"react".React.FC',
79+
'"react".React.ExoticComponent',
80+
'"react".React.NamedExoticComponent',
81+
'"react".React.ForwardRefExoticComponent',
82+
'"react".React.MemoExoticComponent'
83+
].includes(globalFqn);
8084
}

0 commit comments

Comments
 (0)