@@ -34,6 +34,7 @@ interface ConvertAdditionalOptions {
34
34
interface ConvertConfig {
35
35
node : ts . Node ;
36
36
parent ?: ts . Node | null ;
37
+ inTypeMode ?: boolean ;
37
38
ast : ts . SourceFile ;
38
39
additionalOptions : ConvertAdditionalOptions ;
39
40
}
@@ -80,31 +81,45 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
80
81
} ) ;
81
82
}
82
83
83
- /**
84
- * Converts a TypeScript node into an ESTree node.
85
- * @param {ts.Node } child the child ts.Node
86
- * @returns {ESTreeNode|null } the converted ESTree node
87
- */
88
- function convertChild ( child ?: ts . Node ) : ESTreeNode | null {
84
+ function converter ( child ?: ts . Node , inTypeMode ?: boolean ) : ESTreeNode | null {
89
85
if ( ! child ) {
90
86
return null ;
91
87
}
92
88
return convert ( {
93
89
node : child ,
94
90
parent : node ,
91
+ inTypeMode,
95
92
ast,
96
93
additionalOptions
97
94
} ) ;
98
95
}
99
96
97
+ /**
98
+ * Converts a TypeScript node into an ESTree node.
99
+ * @param {ts.Node } child the child ts.Node
100
+ * @returns {ESTreeNode|null } the converted ESTree node
101
+ */
102
+ function convertChild ( child ?: ts . Node ) : ESTreeNode | null {
103
+ return converter ( child , config . inTypeMode ) ;
104
+ }
105
+
106
+ /**
107
+ * Converts a TypeScript node into an ESTree node.
108
+ * @param {ts.Node } child the child ts.Node
109
+ * @returns {ESTreeNode|null } the converted ESTree node
110
+ */
111
+ function convertChildType ( child ?: ts . Node ) : ESTreeNode | null {
112
+ return converter ( child , true ) ;
113
+ }
114
+
100
115
/**
101
116
* Converts a child into a type annotation. This creates an intermediary
102
117
* TypeAnnotation node to match what Flow does.
103
118
* @param {ts.TypeNode } child The TypeScript AST node to convert.
104
119
* @returns {ESTreeNode } The type annotation node.
105
120
*/
106
121
function convertTypeAnnotation ( child : ts . TypeNode ) : ESTreeNode {
107
- const annotation = convertChild ( child ) ;
122
+ const annotation = convertChildType ( child ) ;
108
123
const annotationStartCol = child . getFullStart ( ) - 1 ;
109
124
const loc = nodeUtils . getLocFor ( annotationStartCol , child . end , ast ) ;
110
125
return {
@@ -182,32 +197,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
182
197
type : AST_NODE_TYPES . TSTypeParameterInstantiation ,
183
198
range : [ start , end ] ,
184
199
loc : nodeUtils . getLocFor ( start , end , ast ) ,
185
- params : typeArguments . map ( typeArgument => {
186
- if ( nodeUtils . isTypeKeyword ( typeArgument . kind ) ) {
187
- return {
188
- type : AST_NODE_TYPES [ `TS${ SyntaxKind [ typeArgument . kind ] } ` ] ,
189
- range : [ typeArgument . getStart ( ast ) , typeArgument . getEnd ( ) ] ,
190
- loc : nodeUtils . getLoc ( typeArgument , ast )
191
- } ;
192
- }
193
- if ( typeArgument . kind === SyntaxKind . ImportType ) {
194
- return convert ( {
195
- node : typeArgument ,
196
- parent : null ,
197
- ast,
198
- additionalOptions
199
- } ) ;
200
- }
201
- return {
202
- type : AST_NODE_TYPES . TSTypeReference ,
203
- range : [ typeArgument . getStart ( ast ) , typeArgument . getEnd ( ) ] ,
204
- loc : nodeUtils . getLoc ( typeArgument , ast ) ,
205
- typeName : convertChild ( typeArgument . typeName || typeArgument ) ,
206
- typeParameters : typeArgument . typeArguments
207
- ? convertTypeArgumentsToTypeParameters ( typeArgument . typeArguments )
208
- : undefined
209
- } ;
210
- } )
200
+ params : typeArguments . map ( typeArgument => convertChildType ( typeArgument ) )
211
201
} ;
212
202
}
213
203
@@ -236,36 +226,9 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
236
226
greaterThanToken ! . end ,
237
227
ast
238
228
) ,
239
- params : typeParameters . map ( typeParameter => {
240
- const name = typeParameter . name . text ;
241
-
242
- const constraint = typeParameter . constraint
243
- ? convert ( {
244
- node : typeParameter . constraint ,
245
- parent : typeParameter ,
246
- ast,
247
- additionalOptions
248
- } )
249
- : undefined ;
250
-
251
- const defaultParameter = typeParameter . default
252
- ? convert ( {
253
- node : typeParameter . default ,
254
- parent : typeParameter ,
255
- ast,
256
- additionalOptions
257
- } )
258
- : typeParameter . default ;
259
-
260
- return {
261
- type : AST_NODE_TYPES . TSTypeParameter ,
262
- range : [ typeParameter . getStart ( ast ) , typeParameter . getEnd ( ) ] ,
263
- loc : nodeUtils . getLoc ( typeParameter , ast ) ,
264
- name,
265
- constraint,
266
- default : defaultParameter
267
- } ;
268
- } )
229
+ params : typeParameters . map ( typeParameter =>
230
+ convertChildType ( typeParameter )
231
+ )
269
232
} ;
270
233
}
271
234
@@ -2091,7 +2054,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
2091
2054
break ;
2092
2055
2093
2056
case SyntaxKind . NullKeyword : {
2094
- if ( nodeUtils . isWithinTypeAnnotation ( node ) ) {
2057
+ if ( config . inTypeMode ) {
2095
2058
Object . assign ( result , {
2096
2059
type : AST_NODE_TYPES . TSNullKeyword
2097
2060
} ) ;
@@ -2279,6 +2242,45 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
2279
2242
2280
2243
// TypeScript specific
2281
2244
2245
+ case SyntaxKind . TypeReference : {
2246
+ Object . assign ( result , {
2247
+ type : AST_NODE_TYPES . TSTypeReference ,
2248
+ typeName : convertChildType ( node . typeName ) ,
2249
+ typeParameters : node . typeArguments
2250
+ ? convertTypeArgumentsToTypeParameters ( node . typeArguments )
2251
+ : undefined
2252
+ } ) ;
2253
+ break ;
2254
+ }
2255
+
2256
+ case SyntaxKind . TypeParameter : {
2257
+ Object . assign ( result , {
2258
+ type : AST_NODE_TYPES . TSTypeParameter ,
2259
+ name : node . name . text ,
2260
+ constraint : node . constraint
2261
+ ? convertChildType ( node . constraint )
2262
+ : undefined ,
2263
+ default : node . default ? convertChildType ( node . default ) : undefined
2264
+ } ) ;
2265
+ break ;
2266
+ }
2267
+
2268
+ case SyntaxKind . AnyKeyword :
2269
+ case SyntaxKind . BigIntKeyword :
2270
+ case SyntaxKind . BooleanKeyword :
2271
+ case SyntaxKind . NeverKeyword :
2272
+ case SyntaxKind . NumberKeyword :
2273
+ case SyntaxKind . ObjectKeyword :
2274
+ case SyntaxKind . StringKeyword :
2275
+ case SyntaxKind . SymbolKeyword :
2276
+ case SyntaxKind . UnknownKeyword :
2277
+ case SyntaxKind . VoidKeyword : {
2278
+ Object . assign ( result , {
2279
+ type : `TS${ SyntaxKind [ node . kind ] } `
2280
+ } ) ;
2281
+ break ;
2282
+ }
2283
+
2282
2284
case SyntaxKind . ParenthesizedExpression :
2283
2285
return convert ( {
2284
2286
node : node . expression ,
@@ -2291,7 +2293,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
2291
2293
Object . assign ( result , {
2292
2294
type : AST_NODE_TYPES . TSTypeAliasDeclaration ,
2293
2295
id : convertChild ( node . name ) ,
2294
- typeAnnotation : convertChild ( node . type )
2296
+ typeAnnotation : convertChildType ( node . type )
2295
2297
} ) ;
2296
2298
2297
2299
if ( nodeUtils . hasModifier ( SyntaxKind . DeclareKeyword , node ) ) {
@@ -2583,42 +2585,42 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
2583
2585
case SyntaxKind . OptionalType : {
2584
2586
Object . assign ( result , {
2585
2587
type : AST_NODE_TYPES . TSOptionalType ,
2586
- typeAnnotation : convertChild ( node . type )
2588
+ typeAnnotation : convertChildType ( node . type )
2587
2589
} ) ;
2588
2590
break ;
2589
2591
}
2590
2592
case SyntaxKind . ParenthesizedType : {
2591
2593
Object . assign ( result , {
2592
2594
type : AST_NODE_TYPES . TSParenthesizedType ,
2593
- typeAnnotation : convertChild ( node . type )
2595
+ typeAnnotation : convertChildType ( node . type )
2594
2596
} ) ;
2595
2597
break ;
2596
2598
}
2597
2599
case SyntaxKind . TupleType : {
2598
2600
Object . assign ( result , {
2599
2601
type : AST_NODE_TYPES . TSTupleType ,
2600
- elementTypes : node . elementTypes . map ( convertChild )
2602
+ elementTypes : node . elementTypes . map ( convertChildType )
2601
2603
} ) ;
2602
2604
break ;
2603
2605
}
2604
2606
case SyntaxKind . UnionType : {
2605
2607
Object . assign ( result , {
2606
2608
type : AST_NODE_TYPES . TSUnionType ,
2607
- types : node . types . map ( convertChild )
2609
+ types : node . types . map ( convertChildType )
2608
2610
} ) ;
2609
2611
break ;
2610
2612
}
2611
2613
case SyntaxKind . IntersectionType : {
2612
2614
Object . assign ( result , {
2613
2615
type : AST_NODE_TYPES . TSIntersectionType ,
2614
- types : node . types . map ( convertChild )
2616
+ types : node . types . map ( convertChildType )
2615
2617
} ) ;
2616
2618
break ;
2617
2619
}
2618
2620
case SyntaxKind . RestType : {
2619
2621
Object . assign ( result , {
2620
2622
type : AST_NODE_TYPES . TSRestType ,
2621
- typeAnnotation : convertChild ( node . type )
2623
+ typeAnnotation : convertChildType ( node . type )
2622
2624
} ) ;
2623
2625
break ;
2624
2626
}
0 commit comments