Skip to content

Commit 1f4bc7f

Browse files
Updated React Component Type detection
1 parent 332d252 commit 1f4bc7f

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

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

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ReactComponentPostProcessor extends PostProcessor {
1616
// Function Components (standard functions)
1717
const allFunctions: LCEFunctionDeclaration[] = (concepts.get(LCEFunctionDeclaration.conceptId) ?? []) as LCEFunctionDeclaration[];
1818
for (const func of allFunctions) {
19-
if (isComponentReturnType(func.returnType)) {
19+
if (this.isComponentReturnType(func.returnType)) {
2020
const component = new LCEReactComponent(func.fqn, func.functionName, []);
2121
if (func.metadata.has(JSXDependencyContextProcessor.JSX_DEPENDENCY_METADATA)) {
2222
component.renderedElements.push(...func.metadata.get(JSXDependencyContextProcessor.JSX_DEPENDENCY_METADATA));
@@ -29,9 +29,8 @@ export class ReactComponentPostProcessor extends PostProcessor {
2929
const allVariables: LCEVariableDeclaration[] = (concepts.get(LCEVariableDeclaration.conceptId) ?? []) as LCEVariableDeclaration[];
3030
for (const variable of allVariables) {
3131
if (
32-
(variable.type instanceof LCETypeFunction &&
33-
isComponentReturnType(variable.type.returnType)) ||
34-
(variable.type instanceof LCETypeDeclared && isReactFunctionComponentType(variable.type.fqn.globalFqn))
32+
(variable.type instanceof LCETypeFunction && this.isComponentReturnType(variable.type.returnType)) ||
33+
(variable.type instanceof LCETypeDeclared && this.isReactFunctionComponentType(variable.type.fqn.globalFqn))
3534
) {
3635
const component = new LCEReactComponent(variable.fqn, variable.variableName, []);
3736
if (variable.metadata.has(JSXDependencyContextProcessor.JSX_DEPENDENCY_METADATA)) {
@@ -56,32 +55,38 @@ export class ReactComponentPostProcessor extends PostProcessor {
5655
concepts.set(LCEReactComponent.conceptId, [...reactComponents.values()]);
5756
}
5857
}
59-
}
6058

61-
/**
62-
* returns whether the provided fqn is a return type that indicates that a function is a React component
63-
*/
64-
function isComponentReturnType(type: LCEType): boolean {
65-
const validTypeFqns = ['"react".React.ReactNode', '"react".React.JSX.Element', '"react".JSX.Element'];
59+
/**
60+
* returns whether the provided fqn is a return type that indicates that a function is a React component
61+
*/
62+
isComponentReturnType(type: LCEType): boolean {
63+
const validTypeFqns = [
64+
'"react".React.ReactNode',
65+
'"react".React.ReactElement',
66+
'"react".React.ReactPortal',
67+
'"react".React.JSX.Element',
68+
'"react".JSX.Element',
69+
];
6670

67-
if (type instanceof LCETypeDeclared) {
68-
return validTypeFqns.includes(type.fqn.globalFqn);
69-
} else if (type instanceof LCETypeUnion) {
70-
return !!type.types.find((t) => t instanceof LCETypeDeclared && validTypeFqns.includes(t.fqn.globalFqn));
71-
} else {
72-
return false;
71+
if (type instanceof LCETypeDeclared) {
72+
return validTypeFqns.includes(type.fqn.globalFqn);
73+
} else if (type instanceof LCETypeUnion) {
74+
return !!type.types.find((t) => t instanceof LCETypeDeclared && validTypeFqns.includes(t.fqn.globalFqn));
75+
} else {
76+
return false;
77+
}
7378
}
74-
}
7579

76-
/**
77-
* returns whether the provided fqn belongs to an interface/type describing a React function component
78-
*/
79-
function isReactFunctionComponentType(globalFqn: string): boolean {
80-
return [
81-
'"react".React.FC',
82-
'"react".React.ExoticComponent',
83-
'"react".React.NamedExoticComponent',
84-
'"react".React.ForwardRefExoticComponent',
85-
'"react".React.MemoExoticComponent',
86-
].includes(globalFqn);
80+
/**
81+
* returns whether the provided fqn belongs to an interface/type describing a React function component
82+
*/
83+
isReactFunctionComponentType(globalFqn: string): boolean {
84+
return [
85+
'"react".React.FC',
86+
'"react".React.ExoticComponent',
87+
'"react".React.NamedExoticComponent',
88+
'"react".React.ForwardRefExoticComponent',
89+
'"react".React.MemoExoticComponent',
90+
].includes(globalFqn);
91+
}
8792
}

0 commit comments

Comments
 (0)