Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 078b97d

Browse files
authored
fix: do not fail in case of inference errors (#223)
1 parent 5cc69d1 commit 078b97d

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

Diff for: src/types.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ function getTypeDeclaration(type: any, optional: boolean): TypeDeclaration {
1515
}
1616

1717
export function get(astq: ASTQ, propertyAst: any, propTypesName: string|undefined): TypeDeclaration {
18+
try {
19+
const simpleType = getSimpleType(astq, propertyAst, propTypesName);
20+
if (simpleType) {
21+
return simpleType;
22+
}
23+
const complexType = getComplexType(astq, propertyAst, propTypesName);
24+
if (complexType) {
25+
return complexType;
26+
}
27+
} catch (e) {
28+
console.error('Failed to infer PropType; Fallback to any');
29+
console.error(e.stack);
30+
}
31+
return {
32+
type: 'any',
33+
optional: true
34+
};
35+
}
36+
37+
function getSimpleType(astq: ASTQ, propertyAst: any, propTypesName: string|undefined): TypeDeclaration|undefined {
1838
const [required, simpleTypeName] = getSimpleTypeName(astq, propertyAst, propTypesName);
1939
switch (simpleTypeName) {
2040
case 'any':
@@ -42,8 +62,11 @@ export function get(astq: ASTQ, propertyAst: any, propTypesName: string|undefine
4262
case 'symbol':
4363
return getTypeDeclaration(dom.create.typeof(dom.create.namedTypeReference('Symbol')), !required);
4464
}
65+
return undefined;
66+
}
4567

46-
const [, complexTypeName, typeAst] = getComplexTypeName(astq, propertyAst, propTypesName);
68+
function getComplexType(astq: ASTQ, propertyAst: any, propTypesName: string|undefined): TypeDeclaration|undefined {
69+
const [required, complexTypeName, typeAst] = getComplexTypeName(astq, propertyAst, propTypesName);
4770
switch (complexTypeName) {
4871
case 'instanceOf':
4972
return getTypeDeclaration(dom.create.typeof(
@@ -67,11 +90,7 @@ export function get(astq: ASTQ, propertyAst: any, propTypesName: string|undefine
6790
});
6891
return getTypeDeclaration(dom.create.objectType(entries), !required);
6992
}
70-
71-
return {
72-
type: 'any',
73-
optional: true
74-
};
93+
return undefined;
7594
}
7695

7796
function isRequired(astq: ASTQ, propertyAst: any): [boolean, any] {

0 commit comments

Comments
 (0)