Skip to content

Commit cbe5564

Browse files
committed
fix(parser): handle return type of JSX.Element | null
1 parent aa6add2 commit cbe5564

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Diff for: src/parser.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ export function parseFromProgram(
214214
}
215215
}
216216

217+
function isTypeJSXElementLike(type: ts.Type): boolean {
218+
if (type.isUnion()) {
219+
return type.types.every(
220+
subType => subType.flags & ts.TypeFlags.Null || isTypeJSXElementLike(subType),
221+
);
222+
} else if (type.symbol) {
223+
const name = checker.getFullyQualifiedName(type.symbol);
224+
return name === 'global.JSX.Element' || name === 'React.ReactElement';
225+
}
226+
227+
return false;
228+
}
229+
217230
function parseFunctionComponent(node: ts.VariableDeclaration | ts.FunctionDeclaration) {
218231
if (!node.name) {
219232
return;
@@ -228,14 +241,7 @@ export function parseFromProgram(
228241
.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)
229242
.getCallSignatures()[0];
230243

231-
const rawReturnType = signature.getReturnType();
232-
if (!rawReturnType.symbol) {
233-
return;
234-
}
235-
236-
const returnType = checker.getFullyQualifiedName(rawReturnType.symbol);
237-
238-
if (returnType !== 'global.JSX.Element' && returnType !== 'React.ReactElement') {
244+
if (!isTypeJSXElementLike(signature.getReturnType())) {
239245
return;
240246
}
241247

0 commit comments

Comments
 (0)