@@ -15,6 +15,26 @@ function getTypeDeclaration(type: any, optional: boolean): TypeDeclaration {
15
15
}
16
16
17
17
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 {
18
38
const [ required , simpleTypeName ] = getSimpleTypeName ( astq , propertyAst , propTypesName ) ;
19
39
switch ( simpleTypeName ) {
20
40
case 'any' :
@@ -42,8 +62,11 @@ export function get(astq: ASTQ, propertyAst: any, propTypesName: string|undefine
42
62
case 'symbol' :
43
63
return getTypeDeclaration ( dom . create . typeof ( dom . create . namedTypeReference ( 'Symbol' ) ) , ! required ) ;
44
64
}
65
+ return undefined ;
66
+ }
45
67
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 ) ;
47
70
switch ( complexTypeName ) {
48
71
case 'instanceOf' :
49
72
return getTypeDeclaration ( dom . create . typeof (
@@ -67,11 +90,7 @@ export function get(astq: ASTQ, propertyAst: any, propTypesName: string|undefine
67
90
} ) ;
68
91
return getTypeDeclaration ( dom . create . objectType ( entries ) , ! required ) ;
69
92
}
70
-
71
- return {
72
- type : 'any' ,
73
- optional : true
74
- } ;
93
+ return undefined ;
75
94
}
76
95
77
96
function isRequired ( astq : ASTQ , propertyAst : any ) : [ boolean , any ] {
0 commit comments