@@ -17,13 +17,16 @@ import isStatelessComponent from '../utils/isStatelessComponent';
17
17
import isUnreachableFlowType from '../utils/isUnreachableFlowType' ;
18
18
import recast from 'recast' ;
19
19
import resolveToValue from '../utils/resolveToValue' ;
20
+ import {
21
+ isSupportedUtilityType ,
22
+ unwrapUtilityType ,
23
+ } from '../utils/flowUtilityTypes' ;
24
+ import resolveGenericTypeAnnotation from '../utils/resolveGenericTypeAnnotation' ;
20
25
21
26
const {
22
27
types : { namedTypes : types } ,
23
28
} = recast ;
24
29
25
- const supportedUtilityTypes = new Set ( [ '$Exact' , '$ReadOnly' ] ) ;
26
-
27
30
/**
28
31
* Given an React component (stateless or class) tries to find the
29
32
* flow type for the props. If not found or not one of the supported
@@ -93,41 +96,3 @@ export function applyToFlowTypeProperties(
93
96
}
94
97
}
95
98
}
96
-
97
- function resolveGenericTypeAnnotation ( path : NodePath ) : ?NodePath {
98
- // If the node doesn't have types or properties, try to get the type.
99
- let typePath : ?NodePath ;
100
- if ( path && isSupportedUtilityType ( path ) ) {
101
- typePath = unwrapUtilityType ( path ) ;
102
- } else if ( path && types . GenericTypeAnnotation . check ( path . node ) ) {
103
- typePath = resolveToValue ( path . get ( 'id' ) ) ;
104
- if ( isUnreachableFlowType ( typePath ) ) {
105
- return ;
106
- }
107
-
108
- typePath = typePath . get ( 'right' ) ;
109
- }
110
-
111
- return typePath ;
112
- }
113
-
114
- /**
115
- * See `supportedUtilityTypes` for which types are supported and
116
- * https://flow.org/en/docs/types/utilities/ for which types are available.
117
- */
118
- function isSupportedUtilityType ( path : NodePath ) : boolean {
119
- if ( types . GenericTypeAnnotation . check ( path . node ) ) {
120
- const idPath = path . get ( 'id' ) ;
121
- return Boolean ( idPath ) && supportedUtilityTypes . has ( idPath . node . name ) ;
122
- }
123
- return false ;
124
- }
125
-
126
- /**
127
- * Unwraps well known utility types. For example:
128
- *
129
- * $ReadOnly<T> => T
130
- */
131
- function unwrapUtilityType ( path : NodePath ) : ?NodePath {
132
- return path . get ( 'typeParameters' , 'params' , 0 ) ;
133
- }
0 commit comments