6
6
*
7
7
* @flow
8
8
*/
9
+
10
+ import types from 'ast-types' ;
9
11
import getPropertyName from './getPropertyName' ;
10
12
import printValue from './printValue' ;
11
- import recast from 'recast' ;
12
13
import getTypeAnnotation from '../utils/getTypeAnnotation' ;
13
14
import resolveToValue from '../utils/resolveToValue' ;
14
15
import { resolveObjectToNameArray } from '../utils/resolveObjectKeysToArray' ;
15
16
import getTypeParameters , {
16
17
type TypeParameters ,
17
18
} from '../utils/getTypeParameters' ;
18
19
import type {
19
- FlowTypeDescriptor ,
20
20
FlowElementsType ,
21
- FlowFunctionSignatureType ,
22
21
FlowFunctionArgumentType ,
23
22
FlowObjectSignatureType ,
23
+ FlowSimpleType ,
24
+ FlowTypeDescriptor ,
25
+ TSFunctionSignatureType ,
24
26
} from '../types' ;
25
27
26
- const {
27
- types : { namedTypes : types } ,
28
- } = recast ;
28
+ const { namedTypes : t } = types ;
29
29
30
30
const tsTypes = {
31
31
TSAnyKeyword : 'any' ,
@@ -71,7 +71,7 @@ function handleTSTypeReference(
71
71
typeParams : ?TypeParameters ,
72
72
) : ?FlowTypeDescriptor {
73
73
let type : FlowTypeDescriptor ;
74
- if ( types . TSQualifiedName . check ( path . node . typeName ) ) {
74
+ if ( t . TSQualifiedName . check ( path . node . typeName ) ) {
75
75
const typeName = path . get ( 'typeName' ) ;
76
76
77
77
if ( typeName . node . left . name === 'React' ) {
@@ -144,19 +144,23 @@ function handleTSTypeLiteral(
144
144
145
145
path . get ( 'members' ) . each ( param => {
146
146
if (
147
- types . TSPropertySignature . check ( param . node ) ||
148
- types . TSMethodSignature . check ( param . node )
147
+ t . TSPropertySignature . check ( param . node ) ||
148
+ t . TSMethodSignature . check ( param . node )
149
149
) {
150
+ const propName = getPropertyName ( param ) ;
151
+ if ( ! propName ) {
152
+ return ;
153
+ }
150
154
type . signature . properties . push ( {
151
- key : getPropertyName ( param ) ,
155
+ key : propName ,
152
156
value : getTSTypeWithRequirements (
153
157
param . get ( 'typeAnnotation' ) ,
154
158
typeParams ,
155
159
) ,
156
160
} ) ;
157
- } else if ( types . TSCallSignatureDeclaration . check ( param . node ) ) {
161
+ } else if ( t . TSCallSignatureDeclaration . check ( param . node ) ) {
158
162
type . signature . constructor = handleTSFunctionType ( param , typeParams ) ;
159
- } else if ( types . TSIndexSignature . check ( param . node ) ) {
163
+ } else if ( t . TSIndexSignature . check ( param . node ) ) {
160
164
type . signature . properties . push ( {
161
165
key : getTSTypeWithResolvedTypes (
162
166
param
@@ -176,7 +180,7 @@ function handleTSTypeLiteral(
176
180
return type ;
177
181
}
178
182
179
- function handleTSInterfaceDeclaration ( path : NodePath ) : FlowElementsType {
183
+ function handleTSInterfaceDeclaration ( path : NodePath ) : FlowSimpleType {
180
184
// Interfaces are handled like references which would be documented separately,
181
185
// rather than inlined like type aliases.
182
186
return {
@@ -213,8 +217,8 @@ function handleTSIntersectionType(
213
217
function handleTSFunctionType (
214
218
path : NodePath ,
215
219
typeParams : ?TypeParameters ,
216
- ) : FlowFunctionSignatureType {
217
- const type : FlowFunctionSignatureType = {
220
+ ) : TSFunctionSignatureType {
221
+ const type : TSFunctionSignatureType = {
218
222
name : 'signature' ,
219
223
type : 'function' ,
220
224
raw : printValue ( path ) ,
@@ -233,7 +237,7 @@ function handleTSFunctionType(
233
237
name : param . node . name || '' ,
234
238
type : typeAnnotation
235
239
? getTSTypeWithResolvedTypes ( typeAnnotation , typeParams )
236
- : null ,
240
+ : undefined ,
237
241
} ;
238
242
239
243
if ( param . node . name === 'this' ) {
@@ -284,13 +288,13 @@ function handleTSTypeQuery(
284
288
return { name : path . node . exprName . name } ;
285
289
}
286
290
287
- function handleTSTypeOperator ( path : NodePath ) : FlowTypeDescriptor {
291
+ function handleTSTypeOperator ( path : NodePath ) : ? FlowTypeDescriptor {
288
292
if ( path . node . operator !== 'keyof' ) {
289
293
return null ;
290
294
}
291
295
292
296
let value = path . get ( 'typeAnnotation' ) ;
293
- if ( types . TSTypeQuery . check ( value . node ) ) {
297
+ if ( t . TSTypeQuery . check ( value . node ) ) {
294
298
value = value . get ( 'exprName' ) ;
295
299
} else if ( value . node . id ) {
296
300
value = value . get ( 'id' ) ;
@@ -299,8 +303,8 @@ function handleTSTypeOperator(path: NodePath): FlowTypeDescriptor {
299
303
const resolvedPath = resolveToValue ( value ) ;
300
304
if (
301
305
resolvedPath &&
302
- ( types . ObjectExpression . check ( resolvedPath . node ) ||
303
- types . TSTypeLiteral . check ( resolvedPath . node ) )
306
+ ( t . ObjectExpression . check ( resolvedPath . node ) ||
307
+ t . TSTypeLiteral . check ( resolvedPath . node ) )
304
308
) {
305
309
const keys = resolveObjectToNameArray ( resolvedPath , true ) ;
306
310
@@ -320,13 +324,13 @@ function getTSTypeWithResolvedTypes(
320
324
path : NodePath ,
321
325
typeParams : ?TypeParameters ,
322
326
) : FlowTypeDescriptor {
323
- if ( types . TSTypeAnnotation . check ( path . node ) ) {
327
+ if ( t . TSTypeAnnotation . check ( path . node ) ) {
324
328
path = path . get ( 'typeAnnotation' ) ;
325
329
}
326
330
327
331
const node = path . node ;
328
332
let type : ?FlowTypeDescriptor ;
329
- const isTypeAlias = types . TSTypeAliasDeclaration . check ( path . parentPath . node ) ;
333
+ const isTypeAlias = t . TSTypeAliasDeclaration . check ( path . parentPath . node ) ;
330
334
331
335
// When we see a typealias mark it as visited so that the next
332
336
// call of this function does not run into an endless loop
@@ -345,7 +349,7 @@ function getTSTypeWithResolvedTypes(
345
349
346
350
if ( node . type in tsTypes ) {
347
351
type = { name : tsTypes [ node . type ] } ;
348
- } else if ( types . TSLiteralType . check ( node ) ) {
352
+ } else if ( t . TSLiteralType . check ( node ) ) {
349
353
type = {
350
354
name : 'literal' ,
351
355
value : node . literal . raw || `${ node . literal . value } ` ,
0 commit comments