@@ -170,14 +170,11 @@ const _super = (function (geti, seti) {
170170 } = comments ;
171171
172172 let context : TransformationContext ;
173- let startLexicalEnvironment : ( ) => void ;
174- let endLexicalEnvironment : ( ) => Statement [ ] ;
175173 let getNodeEmitFlags : ( node : Node ) => NodeEmitFlags ;
176174 let setNodeEmitFlags : ( node : Node , flags : NodeEmitFlags ) => void ;
177- let isExpressionSubstitutionEnabled : ( node : Node ) => boolean ;
175+ let isSubstitutionEnabled : ( node : Node ) => boolean ;
178176 let isEmitNotificationEnabled : ( node : Node ) => boolean ;
179- let expressionSubstitution : ( node : Expression ) => Expression ;
180- let identifierSubstitution : ( node : Identifier ) => Identifier ;
177+ let onSubstituteNode : ( node : Node , isExpression : boolean ) => Node ;
181178 let onEmitNode : ( node : Node , emit : ( node : Node ) => void ) => void ;
182179 let nodeToGeneratedName : string [ ] ;
183180 let generatedNameSet : Map < string > ;
@@ -233,14 +230,11 @@ const _super = (function (geti, seti) {
233230 comments . reset ( ) ;
234231 writer . reset ( ) ;
235232
236- startLexicalEnvironment = undefined ;
237- endLexicalEnvironment = undefined ;
238233 getNodeEmitFlags = undefined ;
239234 setNodeEmitFlags = undefined ;
240- isExpressionSubstitutionEnabled = undefined ;
235+ isSubstitutionEnabled = undefined ;
241236 isEmitNotificationEnabled = undefined ;
242- expressionSubstitution = undefined ;
243- identifierSubstitution = undefined ;
237+ onSubstituteNode = undefined ;
244238 onEmitNode = undefined ;
245239 tempFlags = TempFlags . Auto ;
246240 currentSourceFile = undefined ;
@@ -255,14 +249,11 @@ const _super = (function (geti, seti) {
255249
256250 function initializePrinter ( _context : TransformationContext ) {
257251 context = _context ;
258- startLexicalEnvironment = context . startLexicalEnvironment ;
259- endLexicalEnvironment = context . endLexicalEnvironment ;
260252 getNodeEmitFlags = context . getNodeEmitFlags ;
261253 setNodeEmitFlags = context . setNodeEmitFlags ;
262- isExpressionSubstitutionEnabled = context . isExpressionSubstitutionEnabled ;
254+ isSubstitutionEnabled = context . isSubstitutionEnabled ;
263255 isEmitNotificationEnabled = context . isEmitNotificationEnabled ;
264- expressionSubstitution = context . expressionSubstitution ;
265- identifierSubstitution = context . identifierSubstitution ;
256+ onSubstituteNode = context . onSubstituteNode ;
266257 onEmitNode = context . onEmitNode ;
267258 return printSourceFile ;
268259 }
@@ -416,6 +407,10 @@ const _super = (function (geti, seti) {
416407 }
417408
418409 function emitWorker ( node : Node ) : void {
410+ if ( tryEmitSubstitute ( node , emitWorker , /*isExpression*/ false ) ) {
411+ return ;
412+ }
413+
419414 const kind = node . kind ;
420415 switch ( kind ) {
421416 // Pseudo-literals
@@ -426,10 +421,6 @@ const _super = (function (geti, seti) {
426421
427422 // Identifiers
428423 case SyntaxKind . Identifier :
429- if ( tryEmitSubstitute ( node , identifierSubstitution ) ) {
430- return ;
431- }
432-
433424 return emitIdentifier ( < Identifier > node ) ;
434425
435426 // Reserved words
@@ -675,11 +666,11 @@ const _super = (function (geti, seti) {
675666 }
676667
677668 function emitExpressionWorker ( node : Node ) {
678- const kind = node . kind ;
679- if ( isExpressionSubstitutionEnabled ( node ) && tryEmitSubstitute ( node , expressionSubstitution ) ) {
669+ if ( tryEmitSubstitute ( node , emitExpressionWorker , /*isExpression*/ true ) ) {
680670 return ;
681671 }
682672
673+ const kind = node . kind ;
683674 switch ( kind ) {
684675 // Literals
685676 case SyntaxKind . NumericLiteral :
@@ -1531,7 +1522,6 @@ const _super = (function (geti, seti) {
15311522
15321523 const savedTempFlags = tempFlags ;
15331524 tempFlags = 0 ;
1534- startLexicalEnvironment ( ) ;
15351525 emitSignatureHead ( node ) ;
15361526 emitBlockFunctionBodyAndEndLexicalEnvironment ( node , body ) ;
15371527 if ( indentedFlag ) {
@@ -1609,7 +1599,6 @@ const _super = (function (geti, seti) {
16091599 write ( " {" ) ;
16101600 }
16111601
1612- const startingLine = writer . getLine ( ) ;
16131602 increaseIndent ( ) ;
16141603 emitLeadingDetachedComments ( body . statements , body , shouldSkipLeadingCommentsForNode ) ;
16151604
@@ -1626,8 +1615,6 @@ const _super = (function (geti, seti) {
16261615 emitList ( body , body . statements , ListFormat . MultiLineFunctionBodyStatements , statementOffset ) ;
16271616 }
16281617
1629- const endingLine = writer . getLine ( ) ;
1630- emitLexicalEnvironment ( endLexicalEnvironment ( ) , /*newLine*/ startingLine !== endingLine ) ;
16311618 emitTrailingDetachedComments ( body . statements , body , shouldSkipTrailingCommentsForNode ) ;
16321619 decreaseIndent ( ) ;
16331620 writeToken ( SyntaxKind . CloseBraceToken , body . statements . end ) ;
@@ -1725,15 +1712,9 @@ const _super = (function (geti, seti) {
17251712 else {
17261713 const savedTempFlags = tempFlags ;
17271714 tempFlags = 0 ;
1728- startLexicalEnvironment ( ) ;
17291715 write ( "{" ) ;
17301716 increaseIndent ( ) ;
1731-
1732- const startingLine = writer . getLine ( ) ;
17331717 emitBlockStatements ( node ) ;
1734-
1735- const endingLine = writer . getLine ( ) ;
1736- emitLexicalEnvironment ( endLexicalEnvironment ( ) , /*newLine*/ startingLine !== endingLine ) ;
17371718 write ( "}" ) ;
17381719 tempFlags = savedTempFlags ;
17391720 }
@@ -2022,10 +2003,8 @@ const _super = (function (geti, seti) {
20222003 else {
20232004 const savedTempFlags = tempFlags ;
20242005 tempFlags = 0 ;
2025- startLexicalEnvironment ( ) ;
20262006 emitHelpers ( node ) ;
20272007 emitList ( node , statements , ListFormat . MultiLine , statementOffset ) ;
2028- emitLexicalEnvironment ( endLexicalEnvironment ( ) , /*newLine*/ true ) ;
20292008 tempFlags = savedTempFlags ;
20302009 }
20312010
@@ -2038,28 +2017,6 @@ const _super = (function (geti, seti) {
20382017 emitExpression ( node . expression ) ;
20392018 }
20402019
2041- function emitLexicalEnvironment ( declarations : Statement [ ] , newLine : boolean ) {
2042- if ( declarations && declarations . length > 0 ) {
2043- for ( const node of declarations ) {
2044- if ( newLine ) {
2045- writeLine ( ) ;
2046- }
2047- else {
2048- write ( " " ) ;
2049- }
2050-
2051- emit ( node ) ;
2052- }
2053-
2054- if ( newLine ) {
2055- writeLine ( ) ;
2056- }
2057- else {
2058- write ( " " ) ;
2059- }
2060- }
2061- }
2062-
20632020 /**
20642021 * Emits any prologue directives at the start of a Statement list, returning the
20652022 * number of prologue directives written to the output.
@@ -2218,12 +2175,12 @@ const _super = (function (geti, seti) {
22182175 }
22192176 }
22202177
2221- function tryEmitSubstitute ( node : Node , substitution : ( node : Node ) => Node ) {
2222- if ( substitution && ( getNodeEmitFlags ( node ) & NodeEmitFlags . NoSubstitution ) === 0 ) {
2223- const substitute = substitution ( node ) ;
2178+ function tryEmitSubstitute ( node : Node , emitNode : ( node : Node ) => void , isExpression : boolean ) {
2179+ if ( isSubstitutionEnabled ( node ) && ( getNodeEmitFlags ( node ) & NodeEmitFlags . NoSubstitution ) === 0 ) {
2180+ const substitute = onSubstituteNode ( node , isExpression ) ;
22242181 if ( substitute !== node ) {
22252182 setNodeEmitFlags ( substitute , NodeEmitFlags . NoSubstitution | getNodeEmitFlags ( substitute ) ) ;
2226- emitWorker ( substitute ) ;
2183+ emitNode ( substitute ) ;
22272184 return true ;
22282185 }
22292186 }
0 commit comments