Skip to content

Commit 7922547

Browse files
Improved React Component detection
1 parent 9301493 commit 7922547

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { LCEReactComponent } from "../concepts/react-component.concept";
33
import { LCEFunctionDeclaration } from "../../core/concepts/function-declaration.concept";
44
import { LCEVariableDeclaration } from "../../core/concepts/variable-declaration.concept";
55
import { LCEClassDeclaration } from "../../core/concepts/class-declaration.concept";
6-
import { LCETypeDeclared, LCETypeFunction } from "../../core/concepts/type.concept";
6+
import { LCEType, LCETypeDeclared, LCETypeFunction, LCETypeUnion } from "../../core/concepts/type.concept";
77
import { JSXDependencyContextProcessor } from "../processors/jsx-dependency.processor";
88
import { LCEProject } from "../../core/project";
99

@@ -16,10 +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 (
20-
func.returnType instanceof LCETypeDeclared &&
21-
isComponentReturnType(func.returnType.fqn.globalFqn)
22-
) {
19+
if (func.returnType instanceof LCETypeDeclared && isComponentReturnType(func.returnType)) {
2320
const component = new LCEReactComponent(func.fqn, func.functionName, []);
2421
if (func.metadata.has(JSXDependencyContextProcessor.JSX_DEPENDENCY_METADATA)) {
2522
component.renderedElements.push(...func.metadata.get(JSXDependencyContextProcessor.JSX_DEPENDENCY_METADATA));
@@ -34,7 +31,7 @@ export class ReactComponentPostProcessor extends PostProcessor {
3431
if (
3532
(variable.type instanceof LCETypeFunction &&
3633
variable.type.returnType instanceof LCETypeDeclared &&
37-
isComponentReturnType(variable.type.returnType.fqn.globalFqn)) ||
34+
isComponentReturnType(variable.type.returnType)) ||
3835
(variable.type instanceof LCETypeDeclared && isReactFunctionComponentType(variable.type.fqn.globalFqn))
3936
) {
4037
const component = new LCEReactComponent(variable.fqn, variable.variableName, []);
@@ -65,12 +62,16 @@ export class ReactComponentPostProcessor extends PostProcessor {
6562
/**
6663
* returns whether the provided fqn is a return type that indicates that a function is a React component
6764
*/
68-
function isComponentReturnType(globalFqn: string): boolean {
69-
return [
70-
'"react".React.ReactNode',
71-
'"react".React.JSX.Element',
72-
'"react".JSX.Element'
73-
].includes(globalFqn);
65+
function isComponentReturnType(type: LCEType): boolean {
66+
const validTypeFqns = ['"react".React.ReactNode', '"react".React.JSX.Element', '"react".JSX.Element'];
67+
68+
if (type instanceof LCETypeDeclared) {
69+
return validTypeFqns.includes(type.fqn.globalFqn);
70+
} else if (type instanceof LCETypeUnion) {
71+
return !!type.types.find((t) => t instanceof LCETypeDeclared && validTypeFqns.includes(t.fqn.globalFqn));
72+
} else {
73+
return false;
74+
}
7475
}
7576

7677
/**
@@ -82,6 +83,6 @@ function isReactFunctionComponentType(globalFqn: string): boolean {
8283
'"react".React.ExoticComponent',
8384
'"react".React.NamedExoticComponent',
8485
'"react".React.ForwardRefExoticComponent',
85-
'"react".React.MemoExoticComponent'
86+
'"react".React.MemoExoticComponent',
8687
].includes(globalFqn);
8788
}

0 commit comments

Comments
 (0)