@@ -140,33 +140,39 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
140
140
}
141
141
142
142
/**
143
- * Coverts body ExpressionStatements to directives
143
+ * Coverts body Nodes and add directive field to StringLiterals
144
+ * @param {ts.NodeArray<ts.Statement> } nodes of ts.Node
145
+ * @returns {ESTreeNode[] } Array of body statements
144
146
*/
145
- function convertBodyExpressionsToDirectives ( ) {
146
- if ( result . body && nodeUtils . canContainDirective ( node ) ) {
147
- const unique : string [ ] = [ ] ;
148
-
149
- // directives has to be unique, if directive is registered twice pick only first one
150
- result . body
151
- . filter (
152
- ( child : ESTreeNode ) =>
153
- child . type === AST_NODE_TYPES . ExpressionStatement &&
147
+ function convertBodyExpressions (
148
+ nodes : ts . NodeArray < ts . Statement >
149
+ ) : ESTreeNode [ ] {
150
+ // directives has to be unique, if directive is registered twice pick only first one
151
+ const unique : string [ ] = [ ] ;
152
+ const allowDirectives = nodeUtils . canContainDirective ( node ) ;
153
+
154
+ return (
155
+ nodes
156
+ . map ( statement => {
157
+ const child = convertChild ( statement ) ;
158
+ if (
159
+ allowDirectives &&
160
+ child &&
154
161
child . expression &&
155
- child . expression . type === AST_NODE_TYPES . Literal &&
156
- ( child . expression as any ) . value &&
157
- typeof ( child . expression as any ) . value === 'string' &&
158
- // ignore parenthesized expressions
159
- ast . text . charAt ( child . range [ 0 ] ) !== '('
160
- )
161
- . forEach (
162
- ( child : { directive : string ; expression : { raw : string } } ) => {
163
- if ( ! unique . includes ( ( child . expression as any ) . raw ) ) {
164
- child . directive = child . expression . raw . slice ( 1 , - 1 ) ;
165
- unique . push ( child . expression . raw ) ;
162
+ ts . isExpressionStatement ( statement ) &&
163
+ ts . isStringLiteral ( statement . expression )
164
+ ) {
165
+ const raw = child . expression . raw ! ;
166
+ if ( ! unique . includes ( raw ) ) {
167
+ child . directive = raw . slice ( 1 , - 1 ) ;
168
+ unique . push ( raw ) ;
166
169
}
167
170
}
168
- ) ;
169
- }
171
+ return child ! ; // child can be null but it's filtered below
172
+ } )
173
+ // filter out unknown nodes for now
174
+ . filter ( statement => statement )
175
+ ) ;
170
176
}
171
177
172
178
/**
@@ -244,45 +250,22 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
244
250
}
245
251
246
252
/**
247
- * Converts a child into a class implements node. This creates an intermediary
248
- * ClassImplements node to match what Flow does.
249
- * @param {ts.ExpressionWithTypeArguments } child The TypeScript AST node to convert.
250
- * @returns {ESTreeNode } The type annotation node.
251
- */
252
- function convertClassImplements (
253
- child : ts . ExpressionWithTypeArguments
254
- ) : ESTreeNode {
255
- const id = convertChild ( child . expression ) as ESTreeNode ;
256
- const classImplementsNode : ESTreeNode = {
257
- type : AST_NODE_TYPES . ClassImplements ,
258
- loc : id . loc ,
259
- range : id . range ,
260
- id
261
- } ;
262
- if ( child . typeArguments && child . typeArguments . length ) {
263
- classImplementsNode . typeParameters = convertTypeArgumentsToTypeParameters (
264
- child . typeArguments
265
- ) ;
266
- }
267
- return classImplementsNode ;
268
- }
269
-
270
- /**
271
- * Converts a child into a interface heritage node.
253
+ * Converts a child into a specified heritage node.
254
+ * @param {AST_NODE_TYPES } nodeType Type of node to be used
272
255
* @param {ts.ExpressionWithTypeArguments } child The TypeScript AST node to convert.
273
- * @returns {ESTreeNode } The type annotation node.
256
+ * @returns {ESTreeNode } The heritage node.
274
257
*/
275
- function convertInterfaceHeritageClause (
258
+ function convertHeritageClause (
259
+ nodeType : AST_NODE_TYPES ,
276
260
child : ts . ExpressionWithTypeArguments
277
261
) : ESTreeNode {
278
- const id = convertChild ( child . expression ) as ESTreeNode ;
262
+ const id = convertChild ( child . expression ) ! ;
279
263
const classImplementsNode : ESTreeNode = {
280
- type : AST_NODE_TYPES . TSInterfaceHeritage ,
264
+ type : nodeType ,
281
265
loc : id . loc ,
282
266
range : id . range ,
283
267
id
284
268
} ;
285
-
286
269
if ( child . typeArguments && child . typeArguments . length ) {
287
270
classImplementsNode . typeParameters = convertTypeArgumentsToTypeParameters (
288
271
child . typeArguments
@@ -488,21 +471,11 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
488
471
case SyntaxKind . SourceFile :
489
472
Object . assign ( result , {
490
473
type : AST_NODE_TYPES . Program ,
491
- body : [ ] ,
474
+ body : convertBodyExpressions ( node . statements ) ,
492
475
// externalModuleIndicator is internal field in TSC
493
476
sourceType : ( node as any ) . externalModuleIndicator ? 'module' : 'script'
494
477
} ) ;
495
478
496
- // filter out unknown nodes for now
497
- node . statements . forEach ( ( statement : any ) => {
498
- const convertedStatement = convertChild ( statement ) ;
499
- if ( convertedStatement ) {
500
- result . body . push ( convertedStatement ) ;
501
- }
502
- } ) ;
503
-
504
- convertBodyExpressionsToDirectives ( ) ;
505
-
506
479
result . range [ 1 ] = node . endOfFileToken . end ;
507
480
result . loc = nodeUtils . getLocFor (
508
481
node . getStart ( ast ) ,
@@ -514,10 +487,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
514
487
case SyntaxKind . Block :
515
488
Object . assign ( result , {
516
489
type : AST_NODE_TYPES . BlockStatement ,
517
- body : node . statements . map ( convertChild )
490
+ body : convertBodyExpressions ( node . statements )
518
491
} ) ;
519
-
520
- convertBodyExpressionsToDirectives ( ) ;
521
492
break ;
522
493
523
494
case SyntaxKind . Identifier :
@@ -1605,8 +1576,9 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
1605
1576
} ) ;
1606
1577
1607
1578
if ( implementsClause ) {
1608
- ( result as any ) . implements = implementsClause . types . map (
1609
- convertClassImplements
1579
+ ( result as any ) . implements = implementsClause . types . map ( el =>
1580
+ // ClassImplements node to match what Flow does.
1581
+ convertHeritageClause ( AST_NODE_TYPES . ClassImplements , el )
1610
1582
) ;
1611
1583
}
1612
1584
@@ -1636,10 +1608,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
1636
1608
case SyntaxKind . ModuleBlock :
1637
1609
Object . assign ( result , {
1638
1610
type : AST_NODE_TYPES . TSModuleBlock ,
1639
- body : node . statements . map ( convertChild )
1611
+ body : convertBodyExpressions ( node . statements )
1640
1612
} ) ;
1641
-
1642
- convertBodyExpressionsToDirectives ( ) ;
1643
1613
break ;
1644
1614
1645
1615
case SyntaxKind . ImportDeclaration :
@@ -2236,7 +2206,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
2236
2206
case SyntaxKind . TypeParameter : {
2237
2207
Object . assign ( result , {
2238
2208
type : AST_NODE_TYPES . TSTypeParameter ,
2239
- name : node . name . text ,
2209
+ name : convertChildType ( node . name ) ,
2240
2210
constraint : node . constraint
2241
2211
? convertChildType ( node . constraint )
2242
2212
: undefined ,
@@ -2525,8 +2495,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
2525
2495
2526
2496
const interfaceBody = {
2527
2497
type : AST_NODE_TYPES . TSInterfaceBody ,
2528
- body : node . members . map ( ( member : any ) => convertChild ( member ) ) ,
2529
- range : [ interfaceOpenBrace . getStart ( ast ) , result . range [ 1 ] ] ,
2498
+ body : node . members . map ( member => convertChild ( member ) ) ,
2499
+ range : [ interfaceOpenBrace . getStart ( ast ) , node . end ] ,
2530
2500
loc : nodeUtils . getLocFor (
2531
2501
interfaceOpenBrace . getStart ( ast ) ,
2532
2502
node . end ,
@@ -2539,8 +2509,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
2539
2509
body : interfaceBody ,
2540
2510
id : convertChild ( node . name ) ,
2541
2511
heritage : hasImplementsClause
2542
- ? interfaceHeritageClauses [ 0 ] . types . map (
2543
- convertInterfaceHeritageClause
2512
+ ? interfaceHeritageClauses [ 0 ] . types . map ( el =>
2513
+ convertHeritageClause ( AST_NODE_TYPES . TSInterfaceHeritage , el )
2544
2514
)
2545
2515
: [ ]
2546
2516
} ) ;
0 commit comments