@@ -170,14 +170,11 @@ const _super = (function (geti, seti) {
170
170
} = comments ;
171
171
172
172
let context : TransformationContext ;
173
- let startLexicalEnvironment : ( ) => void ;
174
- let endLexicalEnvironment : ( ) => Statement [ ] ;
175
173
let getNodeEmitFlags : ( node : Node ) => NodeEmitFlags ;
176
174
let setNodeEmitFlags : ( node : Node , flags : NodeEmitFlags ) => void ;
177
- let isExpressionSubstitutionEnabled : ( node : Node ) => boolean ;
175
+ let isSubstitutionEnabled : ( node : Node ) => boolean ;
178
176
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 ;
181
178
let onEmitNode : ( node : Node , emit : ( node : Node ) => void ) => void ;
182
179
let nodeToGeneratedName : string [ ] ;
183
180
let generatedNameSet : Map < string > ;
@@ -233,14 +230,11 @@ const _super = (function (geti, seti) {
233
230
comments . reset ( ) ;
234
231
writer . reset ( ) ;
235
232
236
- startLexicalEnvironment = undefined ;
237
- endLexicalEnvironment = undefined ;
238
233
getNodeEmitFlags = undefined ;
239
234
setNodeEmitFlags = undefined ;
240
- isExpressionSubstitutionEnabled = undefined ;
235
+ isSubstitutionEnabled = undefined ;
241
236
isEmitNotificationEnabled = undefined ;
242
- expressionSubstitution = undefined ;
243
- identifierSubstitution = undefined ;
237
+ onSubstituteNode = undefined ;
244
238
onEmitNode = undefined ;
245
239
tempFlags = TempFlags . Auto ;
246
240
currentSourceFile = undefined ;
@@ -255,14 +249,11 @@ const _super = (function (geti, seti) {
255
249
256
250
function initializePrinter ( _context : TransformationContext ) {
257
251
context = _context ;
258
- startLexicalEnvironment = context . startLexicalEnvironment ;
259
- endLexicalEnvironment = context . endLexicalEnvironment ;
260
252
getNodeEmitFlags = context . getNodeEmitFlags ;
261
253
setNodeEmitFlags = context . setNodeEmitFlags ;
262
- isExpressionSubstitutionEnabled = context . isExpressionSubstitutionEnabled ;
254
+ isSubstitutionEnabled = context . isSubstitutionEnabled ;
263
255
isEmitNotificationEnabled = context . isEmitNotificationEnabled ;
264
- expressionSubstitution = context . expressionSubstitution ;
265
- identifierSubstitution = context . identifierSubstitution ;
256
+ onSubstituteNode = context . onSubstituteNode ;
266
257
onEmitNode = context . onEmitNode ;
267
258
return printSourceFile ;
268
259
}
@@ -416,6 +407,10 @@ const _super = (function (geti, seti) {
416
407
}
417
408
418
409
function emitWorker ( node : Node ) : void {
410
+ if ( tryEmitSubstitute ( node , emitWorker , /*isExpression*/ false ) ) {
411
+ return ;
412
+ }
413
+
419
414
const kind = node . kind ;
420
415
switch ( kind ) {
421
416
// Pseudo-literals
@@ -426,10 +421,6 @@ const _super = (function (geti, seti) {
426
421
427
422
// Identifiers
428
423
case SyntaxKind . Identifier :
429
- if ( tryEmitSubstitute ( node , identifierSubstitution ) ) {
430
- return ;
431
- }
432
-
433
424
return emitIdentifier ( < Identifier > node ) ;
434
425
435
426
// Reserved words
@@ -675,11 +666,11 @@ const _super = (function (geti, seti) {
675
666
}
676
667
677
668
function emitExpressionWorker ( node : Node ) {
678
- const kind = node . kind ;
679
- if ( isExpressionSubstitutionEnabled ( node ) && tryEmitSubstitute ( node , expressionSubstitution ) ) {
669
+ if ( tryEmitSubstitute ( node , emitExpressionWorker , /*isExpression*/ true ) ) {
680
670
return ;
681
671
}
682
672
673
+ const kind = node . kind ;
683
674
switch ( kind ) {
684
675
// Literals
685
676
case SyntaxKind . NumericLiteral :
@@ -1531,7 +1522,6 @@ const _super = (function (geti, seti) {
1531
1522
1532
1523
const savedTempFlags = tempFlags ;
1533
1524
tempFlags = 0 ;
1534
- startLexicalEnvironment ( ) ;
1535
1525
emitSignatureHead ( node ) ;
1536
1526
emitBlockFunctionBodyAndEndLexicalEnvironment ( node , body ) ;
1537
1527
if ( indentedFlag ) {
@@ -1609,7 +1599,6 @@ const _super = (function (geti, seti) {
1609
1599
write ( " {" ) ;
1610
1600
}
1611
1601
1612
- const startingLine = writer . getLine ( ) ;
1613
1602
increaseIndent ( ) ;
1614
1603
emitLeadingDetachedComments ( body . statements , body , shouldSkipLeadingCommentsForNode ) ;
1615
1604
@@ -1626,8 +1615,6 @@ const _super = (function (geti, seti) {
1626
1615
emitList ( body , body . statements , ListFormat . MultiLineFunctionBodyStatements , statementOffset ) ;
1627
1616
}
1628
1617
1629
- const endingLine = writer . getLine ( ) ;
1630
- emitLexicalEnvironment ( endLexicalEnvironment ( ) , /*newLine*/ startingLine !== endingLine ) ;
1631
1618
emitTrailingDetachedComments ( body . statements , body , shouldSkipTrailingCommentsForNode ) ;
1632
1619
decreaseIndent ( ) ;
1633
1620
writeToken ( SyntaxKind . CloseBraceToken , body . statements . end ) ;
@@ -1725,15 +1712,9 @@ const _super = (function (geti, seti) {
1725
1712
else {
1726
1713
const savedTempFlags = tempFlags ;
1727
1714
tempFlags = 0 ;
1728
- startLexicalEnvironment ( ) ;
1729
1715
write ( "{" ) ;
1730
1716
increaseIndent ( ) ;
1731
-
1732
- const startingLine = writer . getLine ( ) ;
1733
1717
emitBlockStatements ( node ) ;
1734
-
1735
- const endingLine = writer . getLine ( ) ;
1736
- emitLexicalEnvironment ( endLexicalEnvironment ( ) , /*newLine*/ startingLine !== endingLine ) ;
1737
1718
write ( "}" ) ;
1738
1719
tempFlags = savedTempFlags ;
1739
1720
}
@@ -2022,10 +2003,8 @@ const _super = (function (geti, seti) {
2022
2003
else {
2023
2004
const savedTempFlags = tempFlags ;
2024
2005
tempFlags = 0 ;
2025
- startLexicalEnvironment ( ) ;
2026
2006
emitHelpers ( node ) ;
2027
2007
emitList ( node , statements , ListFormat . MultiLine , statementOffset ) ;
2028
- emitLexicalEnvironment ( endLexicalEnvironment ( ) , /*newLine*/ true ) ;
2029
2008
tempFlags = savedTempFlags ;
2030
2009
}
2031
2010
@@ -2038,28 +2017,6 @@ const _super = (function (geti, seti) {
2038
2017
emitExpression ( node . expression ) ;
2039
2018
}
2040
2019
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
-
2063
2020
/**
2064
2021
* Emits any prologue directives at the start of a Statement list, returning the
2065
2022
* number of prologue directives written to the output.
@@ -2218,12 +2175,12 @@ const _super = (function (geti, seti) {
2218
2175
}
2219
2176
}
2220
2177
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 ) ;
2224
2181
if ( substitute !== node ) {
2225
2182
setNodeEmitFlags ( substitute , NodeEmitFlags . NoSubstitution | getNodeEmitFlags ( substitute ) ) ;
2226
- emitWorker ( substitute ) ;
2183
+ emitNode ( substitute ) ;
2227
2184
return true ;
2228
2185
}
2229
2186
}
0 commit comments