66 *
77 * @flow
88 */
9+
10+ import types from 'ast-types' ;
911import getPropertyName from './getPropertyName' ;
1012import printValue from './printValue' ;
11- import recast from 'recast' ;
1213import getTypeAnnotation from '../utils/getTypeAnnotation' ;
1314import resolveToValue from '../utils/resolveToValue' ;
1415import { resolveObjectToNameArray } from '../utils/resolveObjectKeysToArray' ;
1516import getTypeParameters , {
1617 type TypeParameters ,
1718} from '../utils/getTypeParameters' ;
1819import type {
19- FlowTypeDescriptor ,
2020 FlowElementsType ,
21- FlowFunctionSignatureType ,
2221 FlowFunctionArgumentType ,
2322 FlowObjectSignatureType ,
23+ FlowSimpleType ,
24+ FlowTypeDescriptor ,
25+ TSFunctionSignatureType ,
2426} from '../types' ;
2527
26- const {
27- types : { namedTypes : types } ,
28- } = recast ;
28+ const { namedTypes : t } = types ;
2929
3030const tsTypes = {
3131 TSAnyKeyword : 'any' ,
@@ -71,7 +71,7 @@ function handleTSTypeReference(
7171 typeParams : ?TypeParameters ,
7272) : ?FlowTypeDescriptor {
7373 let type : FlowTypeDescriptor ;
74- if ( types . TSQualifiedName . check ( path . node . typeName ) ) {
74+ if ( t . TSQualifiedName . check ( path . node . typeName ) ) {
7575 const typeName = path . get ( 'typeName' ) ;
7676
7777 if ( typeName . node . left . name === 'React' ) {
@@ -144,19 +144,23 @@ function handleTSTypeLiteral(
144144
145145 path . get ( 'members' ) . each ( param => {
146146 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 )
149149 ) {
150+ const propName = getPropertyName ( param ) ;
151+ if ( ! propName ) {
152+ return ;
153+ }
150154 type . signature . properties . push ( {
151- key : getPropertyName ( param ) ,
155+ key : propName ,
152156 value : getTSTypeWithRequirements (
153157 param . get ( 'typeAnnotation' ) ,
154158 typeParams ,
155159 ) ,
156160 } ) ;
157- } else if ( types . TSCallSignatureDeclaration . check ( param . node ) ) {
161+ } else if ( t . TSCallSignatureDeclaration . check ( param . node ) ) {
158162 type . signature . constructor = handleTSFunctionType ( param , typeParams ) ;
159- } else if ( types . TSIndexSignature . check ( param . node ) ) {
163+ } else if ( t . TSIndexSignature . check ( param . node ) ) {
160164 type . signature . properties . push ( {
161165 key : getTSTypeWithResolvedTypes (
162166 param
@@ -176,7 +180,7 @@ function handleTSTypeLiteral(
176180 return type ;
177181}
178182
179- function handleTSInterfaceDeclaration ( path : NodePath ) : FlowElementsType {
183+ function handleTSInterfaceDeclaration ( path : NodePath ) : FlowSimpleType {
180184 // Interfaces are handled like references which would be documented separately,
181185 // rather than inlined like type aliases.
182186 return {
@@ -213,8 +217,8 @@ function handleTSIntersectionType(
213217function handleTSFunctionType (
214218 path : NodePath ,
215219 typeParams : ?TypeParameters ,
216- ) : FlowFunctionSignatureType {
217- const type : FlowFunctionSignatureType = {
220+ ) : TSFunctionSignatureType {
221+ const type : TSFunctionSignatureType = {
218222 name : 'signature' ,
219223 type : 'function' ,
220224 raw : printValue ( path ) ,
@@ -233,7 +237,7 @@ function handleTSFunctionType(
233237 name : param . node . name || '' ,
234238 type : typeAnnotation
235239 ? getTSTypeWithResolvedTypes ( typeAnnotation , typeParams )
236- : null ,
240+ : undefined ,
237241 } ;
238242
239243 if ( param . node . name === 'this' ) {
@@ -284,13 +288,13 @@ function handleTSTypeQuery(
284288 return { name : path . node . exprName . name } ;
285289}
286290
287- function handleTSTypeOperator ( path : NodePath ) : FlowTypeDescriptor {
291+ function handleTSTypeOperator ( path : NodePath ) : ? FlowTypeDescriptor {
288292 if ( path . node . operator !== 'keyof' ) {
289293 return null ;
290294 }
291295
292296 let value = path . get ( 'typeAnnotation' ) ;
293- if ( types . TSTypeQuery . check ( value . node ) ) {
297+ if ( t . TSTypeQuery . check ( value . node ) ) {
294298 value = value . get ( 'exprName' ) ;
295299 } else if ( value . node . id ) {
296300 value = value . get ( 'id' ) ;
@@ -299,8 +303,8 @@ function handleTSTypeOperator(path: NodePath): FlowTypeDescriptor {
299303 const resolvedPath = resolveToValue ( value ) ;
300304 if (
301305 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 ) )
304308 ) {
305309 const keys = resolveObjectToNameArray ( resolvedPath , true ) ;
306310
@@ -320,13 +324,13 @@ function getTSTypeWithResolvedTypes(
320324 path : NodePath ,
321325 typeParams : ?TypeParameters ,
322326) : FlowTypeDescriptor {
323- if ( types . TSTypeAnnotation . check ( path . node ) ) {
327+ if ( t . TSTypeAnnotation . check ( path . node ) ) {
324328 path = path . get ( 'typeAnnotation' ) ;
325329 }
326330
327331 const node = path . node ;
328332 let type : ?FlowTypeDescriptor ;
329- const isTypeAlias = types . TSTypeAliasDeclaration . check ( path . parentPath . node ) ;
333+ const isTypeAlias = t . TSTypeAliasDeclaration . check ( path . parentPath . node ) ;
330334
331335 // When we see a typealias mark it as visited so that the next
332336 // call of this function does not run into an endless loop
@@ -345,7 +349,7 @@ function getTSTypeWithResolvedTypes(
345349
346350 if ( node . type in tsTypes ) {
347351 type = { name : tsTypes [ node . type ] } ;
348- } else if ( types . TSLiteralType . check ( node ) ) {
352+ } else if ( t . TSLiteralType . check ( node ) ) {
349353 type = {
350354 name : 'literal' ,
351355 value : node . literal . raw || `${ node . literal . value } ` ,
0 commit comments