@@ -161,21 +161,16 @@ const _super = (function (geti, seti) {
161
161
162
162
const comments = createCommentWriter ( host , writer , sourceMap ) ;
163
163
const {
164
- getLeadingComments,
165
- getTrailingComments,
166
- getTrailingCommentsOfPosition,
167
- emitLeadingComments,
168
- emitTrailingComments,
169
- emitLeadingDetachedComments,
170
- emitTrailingDetachedComments
164
+ emitNodeWithComments,
165
+ emitBodyWithDetachedComments,
166
+ emitTrailingCommentsOfPosition
171
167
} = comments ;
172
168
173
169
let context : TransformationContext ;
174
170
let getNodeEmitFlags : ( node : Node ) => NodeEmitFlags ;
175
171
let setNodeEmitFlags : ( node : Node , flags : NodeEmitFlags ) => void ;
176
172
let getSourceMapRange : ( node : Node ) => TextRange ;
177
173
let getTokenSourceMapRange : ( node : Node , token : SyntaxKind ) => TextRange ;
178
- let getCommentRange : ( node : Node ) => TextRange ;
179
174
let isSubstitutionEnabled : ( node : Node ) => boolean ;
180
175
let isEmitNotificationEnabled : ( node : Node ) => boolean ;
181
176
let onSubstituteNode : ( node : Node , isExpression : boolean ) => Node ;
@@ -240,7 +235,6 @@ const _super = (function (geti, seti) {
240
235
setNodeEmitFlags = undefined ;
241
236
getSourceMapRange = undefined ;
242
237
getTokenSourceMapRange = undefined ;
243
- getCommentRange = undefined ;
244
238
isSubstitutionEnabled = undefined ;
245
239
isEmitNotificationEnabled = undefined ;
246
240
onSubstituteNode = undefined ;
@@ -262,7 +256,6 @@ const _super = (function (geti, seti) {
262
256
setNodeEmitFlags = context . setNodeEmitFlags ;
263
257
getSourceMapRange = context . getSourceMapRange ;
264
258
getTokenSourceMapRange = context . getTokenSourceMapRange ;
265
- getCommentRange = context . getCommentRange ;
266
259
isSubstitutionEnabled = context . isSubstitutionEnabled ;
267
260
isEmitNotificationEnabled = context . isEmitNotificationEnabled ;
268
261
onSubstituteNode = context . onSubstituteNode ;
@@ -276,7 +269,7 @@ const _super = (function (geti, seti) {
276
269
currentFileIdentifiers = node . identifiers ;
277
270
sourceMap . setSourceFile ( node ) ;
278
271
comments . setSourceFile ( node ) ;
279
- emitNodeWithNotificationOption ( node , emitWorker ) ;
272
+ emitNodeWithNotification ( node , emitWorker ) ;
280
273
return node ;
281
274
}
282
275
@@ -291,7 +284,7 @@ const _super = (function (geti, seti) {
291
284
* Emits a node.
292
285
*/
293
286
function emit ( node : Node ) {
294
- emitNodeWithNotificationOption ( node , emitWithoutNotificationOption ) ;
287
+ emitNodeWithNotification ( node , emitWithComments ) ;
295
288
}
296
289
297
290
/**
@@ -315,51 +308,71 @@ const _super = (function (geti, seti) {
315
308
}
316
309
317
310
/**
318
- * Emits a node without calling onEmitNode.
319
- * NOTE: Do not call this method directly.
311
+ * Emits a node with comments.
312
+ *
313
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
314
+ * and should only be called indirectly from emit.
320
315
*/
321
- function emitWithoutNotificationOption ( node : Node ) {
322
- emitNodeWithWorker ( node , emitWorker ) ;
316
+ function emitWithComments ( node : Node ) {
317
+ emitNodeWithComments ( node , emitWithSourceMap ) ;
318
+ }
319
+
320
+ /**
321
+ * Emits a node with source maps.
322
+ *
323
+ * NOTE: Do not call this method directly. It is part of the emit pipeline
324
+ * and should only be called indirectly from emitWithComments.
325
+ */
326
+ function emitWithSourceMap ( node : Node ) {
327
+ emitNodeWithSourceMap ( node , emitWorker ) ;
323
328
}
324
329
325
330
/**
326
331
* Emits an expression node.
327
332
*/
328
333
function emitExpression ( node : Expression ) {
329
- emitNodeWithNotificationOption ( node , emitExpressionWithoutNotificationOption ) ;
334
+ emitNodeWithNotification ( node , emitExpressionWithComments ) ;
335
+ }
336
+
337
+ /**
338
+ * Emits an expression with comments.
339
+ *
340
+ * NOTE: Do not call this method directly. It is part of the emitExpression pipeline
341
+ * and should only be called indirectly from emitExpression.
342
+ */
343
+ function emitExpressionWithComments ( node : Expression ) {
344
+ emitNodeWithComments ( node , emitExpressionWithSourceMap ) ;
330
345
}
331
346
332
347
/**
333
- * Emits an expression without calling onEmitNode.
334
- * NOTE: Do not call this method directly.
348
+ * Emits an expression with source maps.
349
+ *
350
+ * NOTE: Do not call this method directly. It is part of the emitExpression pipeline
351
+ * and should only be called indirectly from emitExpressionWithComments.
335
352
*/
336
- function emitExpressionWithoutNotificationOption ( node : Expression ) {
337
- emitNodeWithWorker ( node , emitExpressionWorker ) ;
353
+ function emitExpressionWithSourceMap ( node : Expression ) {
354
+ emitNodeWithSourceMap ( node , emitExpressionWorker ) ;
338
355
}
339
356
340
357
/**
341
358
* Emits a node with emit notification if available.
342
359
*/
343
- function emitNodeWithNotificationOption ( node : Node , emit : ( node : Node ) => void ) {
360
+ function emitNodeWithNotification ( node : Node , emitCallback : ( node : Node ) => void ) {
344
361
if ( node ) {
345
362
if ( isEmitNotificationEnabled ( node ) ) {
346
- onEmitNode ( node , emit ) ;
363
+ onEmitNode ( node , emitCallback ) ;
347
364
}
348
365
else {
349
- emit ( node ) ;
366
+ emitCallback ( node ) ;
350
367
}
351
368
}
352
369
}
353
370
354
- function emitNodeWithWorker ( node : Node , emitWorker : ( node : Node ) => void ) {
371
+ function emitNodeWithSourceMap ( node : Node , emitCallback : ( node : Node ) => void ) {
355
372
if ( node ) {
356
- const leadingComments = getLeadingComments ( /*range*/ node , /*contextNode*/ node , shouldSkipLeadingCommentsForNode , getCommentRange ) ;
357
- const trailingComments = getTrailingComments ( /*range*/ node , /*contextNode*/ node , shouldSkipTrailingCommentsForNode , getCommentRange ) ;
358
- emitLeadingComments ( /*range*/ node , leadingComments , /*contextNode*/ node , getCommentRange ) ;
359
373
emitStart ( /*range*/ node , /*contextNode*/ node , shouldSkipLeadingSourceMapForNode , shouldSkipSourceMapForChildren , getSourceMapRange ) ;
360
- emitWorker ( node ) ;
374
+ emitCallback ( node ) ;
361
375
emitEnd ( /*range*/ node , /*contextNode*/ node , shouldSkipTrailingSourceMapForNode , shouldSkipSourceMapForChildren , getSourceMapRange ) ;
362
- emitTrailingComments ( node , trailingComments ) ;
363
376
}
364
377
}
365
378
@@ -1642,24 +1655,33 @@ const _super = (function (geti, seti) {
1642
1655
}
1643
1656
1644
1657
increaseIndent ( ) ;
1645
- emitLeadingDetachedComments ( body . statements , body , shouldSkipLeadingCommentsForNode ) ;
1646
1658
1659
+ emitBodyWithDetachedComments ( body , body . statements ,
1660
+ shouldEmitBlockFunctionBodyOnSingleLine ( parentNode , body )
1661
+ ? emitBlockFunctionBodyOnSingleLine
1662
+ : emitBlockFunctionBodyWorker ) ;
1663
+
1664
+ decreaseIndent ( ) ;
1665
+ writeToken ( SyntaxKind . CloseBraceToken , body . statements . end , body ) ;
1666
+ }
1667
+
1668
+ function emitBlockFunctionBodyOnSingleLine ( body : Block ) {
1669
+ emitBlockFunctionBodyWorker ( body , /*emitBlockFunctionBodyOnSingleLine*/ true ) ;
1670
+ }
1671
+
1672
+ function emitBlockFunctionBodyWorker ( body : Block , emitBlockFunctionBodyOnSingleLine ?: boolean ) {
1647
1673
// Emit all the prologue directives (like "use strict").
1648
1674
const statementOffset = emitPrologueDirectives ( body . statements , /*startWithNewLine*/ true ) ;
1649
1675
const helpersEmitted = emitHelpers ( body ) ;
1650
1676
1651
- if ( statementOffset === 0 && ! helpersEmitted && shouldEmitBlockFunctionBodyOnSingleLine ( parentNode , body ) ) {
1677
+ if ( statementOffset === 0 && ! helpersEmitted && emitBlockFunctionBodyOnSingleLine ) {
1652
1678
decreaseIndent ( ) ;
1653
1679
emitList ( body , body . statements , ListFormat . SingleLineFunctionBodyStatements ) ;
1654
1680
increaseIndent ( ) ;
1655
1681
}
1656
1682
else {
1657
1683
emitList ( body , body . statements , ListFormat . MultiLineFunctionBodyStatements , statementOffset ) ;
1658
1684
}
1659
-
1660
- emitTrailingDetachedComments ( body . statements , body , shouldSkipTrailingCommentsForNode ) ;
1661
- decreaseIndent ( ) ;
1662
- writeToken ( SyntaxKind . CloseBraceToken , body . statements . end , body ) ;
1663
1685
}
1664
1686
1665
1687
function emitClassDeclaration ( node : ClassDeclaration ) {
@@ -2004,9 +2026,12 @@ const _super = (function (geti, seti) {
2004
2026
// }
2005
2027
// "comment1" is not considered to be leading comment for node.initializer
2006
2028
// but rather a trailing comment on the previous node.
2007
- if ( ! shouldSkipLeadingCommentsForNode ( node . initializer ) ) {
2008
- emitLeadingComments ( /*range*/ node . initializer , getTrailingComments ( collapseRangeToStart ( node . initializer ) ) , /*contextNode*/ node . initializer , getCommentRange ) ;
2029
+ const initializer = node . initializer ;
2030
+ if ( ! shouldSkipLeadingCommentsForNode ( initializer ) ) {
2031
+ const commentRange = initializer . commentRange || initializer ;
2032
+ emitTrailingCommentsOfPosition ( commentRange . pos ) ;
2009
2033
}
2034
+
2010
2035
emitExpression ( node . initializer ) ;
2011
2036
}
2012
2037
@@ -2034,23 +2059,17 @@ const _super = (function (geti, seti) {
2034
2059
function emitSourceFile ( node : SourceFile ) {
2035
2060
writeLine ( ) ;
2036
2061
emitShebang ( ) ;
2037
- emitLeadingDetachedComments ( node ) ;
2062
+ emitBodyWithDetachedComments ( node , node . statements , emitSourceFileWorker ) ;
2063
+ }
2038
2064
2065
+ function emitSourceFileWorker ( node : SourceFile ) {
2039
2066
const statements = node . statements ;
2040
2067
const statementOffset = emitPrologueDirectives ( statements ) ;
2041
- if ( getNodeEmitFlags ( node ) & NodeEmitFlags . NoLexicalEnvironment ) {
2042
- emitHelpers ( node ) ;
2043
- emitList ( node , statements , ListFormat . MultiLine , statementOffset ) ;
2044
- }
2045
- else {
2046
- const savedTempFlags = tempFlags ;
2047
- tempFlags = 0 ;
2048
- emitHelpers ( node ) ;
2049
- emitList ( node , statements , ListFormat . MultiLine , statementOffset ) ;
2050
- tempFlags = savedTempFlags ;
2051
- }
2052
-
2053
- emitTrailingDetachedComments ( node . statements ) ;
2068
+ const savedTempFlags = tempFlags ;
2069
+ tempFlags = 0 ;
2070
+ emitHelpers ( node ) ;
2071
+ emitList ( node , statements , ListFormat . MultiLine , statementOffset ) ;
2072
+ tempFlags = savedTempFlags ;
2054
2073
}
2055
2074
2056
2075
// Transformation nodes
@@ -2326,7 +2345,8 @@ const _super = (function (geti, seti) {
2326
2345
}
2327
2346
else {
2328
2347
// Write the opening line terminator or leading whitespace.
2329
- let shouldEmitInterveningComments = true ;
2348
+ const mayEmitInterveningComments = ( format & ListFormat . NoInterveningComments ) === 0 ;
2349
+ let shouldEmitInterveningComments = mayEmitInterveningComments ;
2330
2350
if ( shouldWriteLeadingLineTerminator ( parentNode , children , format ) ) {
2331
2351
writeLine ( ) ;
2332
2352
shouldEmitInterveningComments = false ;
@@ -2369,10 +2389,11 @@ const _super = (function (geti, seti) {
2369
2389
}
2370
2390
2371
2391
if ( shouldEmitInterveningComments ) {
2372
- emitLeadingComments ( /*node*/ child , getTrailingCommentsOfPosition ( child . pos ) , /*contextNode*/ child , getCommentRange ) ;
2392
+ const commentRange = child . commentRange || child ;
2393
+ emitTrailingCommentsOfPosition ( commentRange . pos ) ;
2373
2394
}
2374
2395
else {
2375
- shouldEmitInterveningComments = true ;
2396
+ shouldEmitInterveningComments = mayEmitInterveningComments ;
2376
2397
}
2377
2398
2378
2399
// Emit this child.
@@ -2876,6 +2897,7 @@ const _super = (function (geti, seti) {
2876
2897
// Other
2877
2898
PreferNewLine = 1 << 15 , // Prefer adding a LineTerminator between synthesized nodes.
2878
2899
NoTrailingNewLine = 1 << 16 , // Do not emit a trailing NewLine for a MultiLine list.
2900
+ NoInterveningComments = 1 << 17 , // Do not emit comments between each node
2879
2901
2880
2902
// Precomputed Formats
2881
2903
Modifiers = SingleLine | SpaceBetweenSiblings ,
@@ -2890,7 +2912,7 @@ const _super = (function (geti, seti) {
2890
2912
ArrayLiteralExpressionElements = PreserveLines | CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | Indented | SquareBrackets ,
2891
2913
CallExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis ,
2892
2914
NewExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis | OptionalIfUndefined ,
2893
- TemplateExpressionSpans = SingleLine ,
2915
+ TemplateExpressionSpans = SingleLine | NoInterveningComments ,
2894
2916
SingleLineBlockStatements = SpaceBetweenBraces | SpaceBetweenSiblings | SingleLine ,
2895
2917
MultiLineBlockStatements = Indented | MultiLine ,
2896
2918
VariableDeclarationList = CommaDelimited | SpaceBetweenSiblings | SingleLine ,
@@ -2902,8 +2924,8 @@ const _super = (function (geti, seti) {
2902
2924
EnumMembers = CommaDelimited | Indented | MultiLine ,
2903
2925
CaseBlockClauses = Indented | MultiLine ,
2904
2926
NamedImportsOrExportsElements = CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | SingleLine | SpaceBetweenBraces ,
2905
- JsxElementChildren = SingleLine ,
2906
- JsxElementAttributes = SingleLine | SpaceBetweenSiblings ,
2927
+ JsxElementChildren = SingleLine | NoInterveningComments ,
2928
+ JsxElementAttributes = SingleLine | SpaceBetweenSiblings | NoInterveningComments ,
2907
2929
CaseOrDefaultClauseStatements = Indented | MultiLine | NoTrailingNewLine | OptionalIfEmpty ,
2908
2930
HeritageClauseTypes = CommaDelimited | SpaceBetweenSiblings | SingleLine ,
2909
2931
SourceFileStatements = MultiLine | NoTrailingNewLine ,
0 commit comments