@@ -229,33 +229,27 @@ const _super = (function (geti, seti) {
229
229
let isOwnFileEmit : boolean ;
230
230
let emitSkipped = false ;
231
231
232
- performance . mark ( "beforeTransform" ) ;
232
+ const sourceFiles = getSourceFilesToEmit ( host , targetSourceFile ) ;
233
233
234
234
// Transform the source files
235
- const transformed = transformFiles (
236
- resolver ,
237
- host ,
238
- getSourceFilesToEmit ( host , targetSourceFile ) ,
239
- transformers ) ;
240
-
241
- performance . measure ( "transformTime" , "beforeTransform" ) ;
242
-
243
- // Extract helpers from the result
235
+ performance . mark ( "beforeTransform" ) ;
244
236
const {
237
+ transformed,
245
238
emitNodeWithSubstitution,
246
239
emitNodeWithNotification
247
- } = transformed ;
248
-
249
- performance . mark ( "beforePrint" ) ;
240
+ } = transformFiles ( resolver , host , sourceFiles , transformers ) ;
241
+ performance . measure ( "transformTime" , "beforeTransform" ) ;
250
242
251
243
// Emit each output file
252
- forEachTransformedEmitFile ( host , transformed . getSourceFiles ( ) , emitFile ) ;
253
-
254
- // Clean up after transformation
255
- transformed . dispose ( ) ;
256
-
244
+ performance . mark ( "beforePrint" ) ;
245
+ forEachTransformedEmitFile ( host , transformed , emitFile ) ;
257
246
performance . measure ( "printTime" , "beforePrint" ) ;
258
247
248
+ // Clean up emit nodes on parse tree
249
+ for ( const sourceFile of sourceFiles ) {
250
+ disposeEmitNodes ( sourceFile ) ;
251
+ }
252
+
259
253
return {
260
254
emitSkipped,
261
255
diagnostics : emitterDiagnostics . getDiagnostics ( ) ,
@@ -346,111 +340,137 @@ const _super = (function (geti, seti) {
346
340
currentFileIdentifiers = node . identifiers ;
347
341
sourceMap . setSourceFile ( node ) ;
348
342
comments . setSourceFile ( node ) ;
349
- emitNodeWithNotification ( node , emitWithSubstitution ) ;
343
+ pipelineEmitWithNotification ( EmitContext . SourceFile , node ) ;
350
344
}
351
345
352
346
/**
353
347
* Emits a node.
354
348
*/
355
349
function emit ( node : Node ) {
356
- emitNodeWithNotification ( node , emitWithComments ) ;
350
+ pipelineEmitWithNotification ( EmitContext . Unspecified , node ) ;
357
351
}
358
352
359
353
/**
360
- * Emits a node with comments.
361
- *
362
- * NOTE: Do not call this method directly. It is part of the emit pipeline
363
- * and should only be called indirectly from emit.
354
+ * Emits an IdentifierName.
364
355
*/
365
- function emitWithComments ( node : Node ) {
366
- emitNodeWithComments ( node , emitWithSourceMap ) ;
356
+ function emitIdentifierName ( node : Identifier ) {
357
+ pipelineEmitWithNotification ( EmitContext . IdentifierName , node ) ;
367
358
}
368
359
369
360
/**
370
- * Emits a node with source maps.
371
- *
372
- * NOTE: Do not call this method directly. It is part of the emit pipeline
373
- * and should only be called indirectly from emitWithComments.
361
+ * Emits an expression node.
374
362
*/
375
- function emitWithSourceMap ( node : Node ) {
376
- emitNodeWithSourceMap ( node , emitWithSubstitution ) ;
363
+ function emitExpression ( node : Expression ) {
364
+ pipelineEmitWithNotification ( EmitContext . Expression , node ) ;
377
365
}
378
366
379
367
/**
380
- * Emits a node with possible substitution .
368
+ * Emits a node with possible notification .
381
369
*
382
370
* NOTE: Do not call this method directly. It is part of the emit pipeline
383
- * and should only be called indirectly from emitWithSourceMap or
384
- * emitIdentifierNameWithComments .
371
+ * and should only be called from printSourceFile, emit, emitExpression, or
372
+ * emitIdentifierName .
385
373
*/
386
- function emitWithSubstitution ( node : Node ) {
387
- emitNodeWithSubstitution ( node , /*isExpression*/ false , emitWorker ) ;
374
+ function pipelineEmitWithNotification ( emitContext : EmitContext , node : Node ) {
375
+ emitNodeWithNotification ( emitContext , node , pipelineEmitWithComments ) ;
388
376
}
389
377
390
378
/**
391
- * Emits an IdentifierName.
379
+ * Emits a node with comments.
380
+ *
381
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
382
+ * and should only be called indirectly from pipelineEmitWithNotification.
392
383
*/
393
- function emitIdentifierName ( node : Identifier ) {
394
- if ( node ) {
395
- emitNodeWithNotification ( node , emitIdentifierNameWithComments ) ;
384
+ function pipelineEmitWithComments ( emitContext : EmitContext , node : Node ) {
385
+ // Do not emit comments for SourceFile
386
+ if ( emitContext === EmitContext . SourceFile ) {
387
+ pipelineEmitWithSourceMap ( emitContext , node ) ;
388
+ return ;
396
389
}
390
+
391
+ emitNodeWithComments ( emitContext , node , pipelineEmitWithSourceMap ) ;
397
392
}
398
393
399
394
/**
400
- * Emits an IdentifierName with possible comments .
395
+ * Emits a node with source maps .
401
396
*
402
397
* NOTE: Do not call this method directly. It is part of the emit pipeline
403
- * and should only be called indirectly from emitIdentifierName .
398
+ * and should only be called indirectly from pipelineEmitWithComments .
404
399
*/
405
- function emitIdentifierNameWithComments ( node : Identifier ) {
406
- emitNodeWithComments ( node , emitWithSubstitution ) ;
400
+ function pipelineEmitWithSourceMap ( emitContext : EmitContext , node : Node ) {
401
+ // Do not emit source mappings for SourceFile or IdentifierName
402
+ if ( emitContext === EmitContext . SourceFile
403
+ || emitContext === EmitContext . IdentifierName ) {
404
+ pipelineEmitWithSubstitution ( emitContext , node ) ;
405
+ return ;
406
+ }
407
+
408
+ emitNodeWithSourceMap ( emitContext , node , pipelineEmitWithSubstitution ) ;
407
409
}
408
410
409
411
/**
410
- * Emits an expression node.
412
+ * Emits a node with possible substitution.
413
+ *
414
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
415
+ * and should only be called indirectly from pipelineEmitWithSourceMap or
416
+ * pipelineEmitInUnspecifiedContext (when pickign a more specific context).
411
417
*/
412
- function emitExpression ( node : Expression ) {
413
- emitNodeWithNotification ( node , emitExpressionWithComments ) ;
418
+ function pipelineEmitWithSubstitution ( emitContext : EmitContext , node : Node ) {
419
+ emitNodeWithSubstitution ( emitContext , node , pipelineEmitForContext ) ;
414
420
}
415
421
416
422
/**
417
- * Emits an expression with comments .
423
+ * Emits a node .
418
424
*
419
- * NOTE: Do not call this method directly. It is part of the emitExpression pipeline
420
- * and should only be called indirectly from emitExpression .
425
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
426
+ * and should only be called indirectly from pipelineEmitWithSubstitution .
421
427
*/
422
- function emitExpressionWithComments ( node : Expression ) {
423
- emitNodeWithComments ( node , emitExpressionWithSourceMap ) ;
428
+ function pipelineEmitForContext ( emitContext : EmitContext , node : Node ) : void {
429
+ switch ( emitContext ) {
430
+ case EmitContext . SourceFile : return pipelineEmitInSourceFileContext ( node ) ;
431
+ case EmitContext . IdentifierName : return pipelineEmitInIdentifierNameContext ( node ) ;
432
+ case EmitContext . Unspecified : return pipelineEmitInUnspecifiedContext ( node ) ;
433
+ case EmitContext . Expression : return pipelineEmitInExpressionContext ( node ) ;
434
+ }
424
435
}
425
436
426
437
/**
427
- * Emits an expression with possible source maps .
438
+ * Emits a node in the SourceFile EmitContext .
428
439
*
429
- * NOTE: Do not call this method directly. It is part of the emitExpression pipeline
430
- * and should only be called indirectly from emitExpressionWithComments .
440
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
441
+ * and should only be called indirectly from pipelineEmitForContext .
431
442
*/
432
- function emitExpressionWithSourceMap ( node : Expression ) {
433
- emitNodeWithSourceMap ( node , emitExpressionWithSubstitution ) ;
443
+ function pipelineEmitInSourceFileContext ( node : Node ) : void {
444
+ const kind = node . kind ;
445
+ switch ( kind ) {
446
+ // Top-level nodes
447
+ case SyntaxKind . SourceFile :
448
+ return emitSourceFile ( < SourceFile > node ) ;
449
+ }
434
450
}
435
451
436
452
/**
437
- * Emits an expression with possible substitution .
453
+ * Emits a node in the IdentifierName EmitContext .
438
454
*
439
- * NOTE: Do not call this method directly. It is part of the emitExpression pipeline
440
- * and should only be called indirectly from emitExpressionWithSourceMap or
441
- * from emitWorker.
455
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
456
+ * and should only be called indirectly from pipelineEmitForContext.
442
457
*/
443
- function emitExpressionWithSubstitution ( node : Expression ) {
444
- emitNodeWithSubstitution ( node , /*isExpression*/ true , emitExpressionWorker ) ;
458
+ function pipelineEmitInIdentifierNameContext ( node : Node ) : void {
459
+ const kind = node . kind ;
460
+ switch ( kind ) {
461
+ // Identifiers
462
+ case SyntaxKind . Identifier :
463
+ return emitIdentifier ( < Identifier > node ) ;
464
+ }
445
465
}
446
466
447
467
/**
448
- * Emits a node.
468
+ * Emits a node in the Unspecified EmitContext .
449
469
*
450
470
* NOTE: Do not call this method directly. It is part of the emit pipeline
451
- * and should only be called indirectly from emitNodeWithSubstitution .
471
+ * and should only be called indirectly from pipelineEmitForContext .
452
472
*/
453
- function emitWorker ( node : Node ) : void {
473
+ function pipelineEmitInUnspecifiedContext ( node : Node ) : void {
454
474
const kind = node . kind ;
455
475
switch ( kind ) {
456
476
// Pseudo-literals
@@ -486,7 +506,8 @@ const _super = (function (geti, seti) {
486
506
case SyntaxKind . StringKeyword :
487
507
case SyntaxKind . SymbolKeyword :
488
508
case SyntaxKind . GlobalKeyword :
489
- return emitTokenNode ( node ) ;
509
+ writeTokenText ( kind ) ;
510
+ return ;
490
511
491
512
// Parse tree nodes
492
513
@@ -691,27 +712,24 @@ const _super = (function (geti, seti) {
691
712
case SyntaxKind . EnumMember :
692
713
return emitEnumMember ( < EnumMember > node ) ;
693
714
694
- // Top-level nodes
695
- case SyntaxKind . SourceFile :
696
- return emitSourceFile ( < SourceFile > node ) ;
697
-
698
715
// JSDoc nodes (ignored)
699
-
700
716
// Transformation nodes (ignored)
701
717
}
702
718
719
+ // If the node is an expression, try to emit it as an expression with
720
+ // substitution.
703
721
if ( isExpression ( node ) ) {
704
- return emitExpressionWithSubstitution ( node ) ;
722
+ return pipelineEmitWithSubstitution ( EmitContext . Expression , node ) ;
705
723
}
706
724
}
707
725
708
726
/**
709
- * Emits an expression .
727
+ * Emits a node in the Expression EmitContext .
710
728
*
711
- * NOTE: Do not call this method directly. It is part of the emitExpression pipeline
712
- * and should only be called indirectly from emitExpressionWithNotification .
729
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
730
+ * and should only be called indirectly from pipelineEmitForContext .
713
731
*/
714
- function emitExpressionWorker ( node : Node ) {
732
+ function pipelineEmitInExpressionContext ( node : Node ) : void {
715
733
const kind = node . kind ;
716
734
switch ( kind ) {
717
735
// Literals
@@ -733,7 +751,8 @@ const _super = (function (geti, seti) {
733
751
case SyntaxKind . SuperKeyword :
734
752
case SyntaxKind . TrueKeyword :
735
753
case SyntaxKind . ThisKeyword :
736
- return emitTokenNode ( node ) ;
754
+ writeTokenText ( kind ) ;
755
+ return ;
737
756
738
757
// Expressions
739
758
case SyntaxKind . ArrayLiteralExpression :
@@ -2444,15 +2463,7 @@ const _super = (function (geti, seti) {
2444
2463
function writeTokenText ( token : SyntaxKind , pos ?: number ) {
2445
2464
const tokenString = tokenToString ( token ) ;
2446
2465
write ( tokenString ) ;
2447
- return positionIsSynthesized ( pos ) ? - 1 : pos + tokenString . length ;
2448
- }
2449
-
2450
- function emitTokenNode ( node : Node ) {
2451
- emitNodeWithSourceMap ( node , emitTokenNodeWorker ) ;
2452
- }
2453
-
2454
- function emitTokenNodeWorker ( node : Node ) {
2455
- writeTokenText ( node . kind ) ;
2466
+ return pos < 0 ? pos : pos + tokenString . length ;
2456
2467
}
2457
2468
2458
2469
function increaseIndentIf ( value : boolean , valueToWriteWhenNotIndenting ?: string ) {
0 commit comments