@@ -4564,16 +4564,26 @@ namespace ts {
4564
4564
// JsxText will be written with its leading whitespace, so don't add more manually.
4565
4565
return 0 ;
4566
4566
}
4567
- else if ( preserveSourceNewlines && siblingNodePositionsAreComparable ( previousNode , nextNode ) ) {
4568
- return getEffectiveLines (
4569
- includeComments => getLinesBetweenRangeEndAndRangeStart (
4570
- previousNode ,
4571
- nextNode ,
4572
- currentSourceFile ! ,
4573
- includeComments ) ) ;
4574
- }
4575
- else if ( ! preserveSourceNewlines && ! nodeIsSynthesized ( previousNode ) && ! nodeIsSynthesized ( nextNode ) ) {
4576
- return rangeEndIsOnSameLineAsRangeStart ( previousNode , nextNode , currentSourceFile ! ) ? 0 : 1 ;
4567
+ else if ( ! nodeIsSynthesized ( previousNode ) && ! nodeIsSynthesized ( nextNode ) ) {
4568
+ if ( preserveSourceNewlines && siblingNodePositionsAreComparable ( previousNode , nextNode ) ) {
4569
+ return getEffectiveLines (
4570
+ includeComments => getLinesBetweenRangeEndAndRangeStart (
4571
+ previousNode ,
4572
+ nextNode ,
4573
+ currentSourceFile ! ,
4574
+ includeComments ) ) ;
4575
+ }
4576
+ // If `preserveSourceNewlines` is `false` we do not intend to preserve the effective lines between the
4577
+ // previous and next node. Instead we naively check whether nodes are on separate lines within the
4578
+ // same node parent. If so, we intend to preserve a single line terminator. This is less precise and
4579
+ // expensive than checking with `preserveSourceNewlines` as above, but the goal is not to preserve the
4580
+ // effective source lines between two sibling nodes.
4581
+ else if ( ! preserveSourceNewlines && originalNodesHaveSameParent ( previousNode , nextNode ) ) {
4582
+ return rangeEndIsOnSameLineAsRangeStart ( previousNode , nextNode , currentSourceFile ! ) ? 0 : 1 ;
4583
+ }
4584
+ // If the two nodes are not comparable, add a line terminator based on the format that can indicate
4585
+ // whether new lines are preferred or not.
4586
+ return format & ListFormat . PreferNewLine ? 1 : 0 ;
4577
4587
}
4578
4588
else if ( synthesizedNodeStartsOnNewLine ( previousNode , format ) || synthesizedNodeStartsOnNewLine ( nextNode , format ) ) {
4579
4589
return 1 ;
@@ -5300,11 +5310,14 @@ namespace ts {
5300
5310
5301
5311
}
5302
5312
5303
- function siblingNodePositionsAreComparable ( previousNode : Node , nextNode : Node ) {
5304
- if ( nodeIsSynthesized ( previousNode ) || nodeIsSynthesized ( nextNode ) ) {
5305
- return false ;
5306
- }
5313
+ function originalNodesHaveSameParent ( nodeA : Node , nodeB : Node ) {
5314
+ nodeA = getOriginalNode ( nodeA ) ;
5315
+ // For performance, do not call `getOriginalNode` for `nodeB` if `nodeA` doesn't even
5316
+ // have a parent node.
5317
+ return nodeA . parent && nodeA . parent === getOriginalNode ( nodeB ) . parent ;
5318
+ }
5307
5319
5320
+ function siblingNodePositionsAreComparable ( previousNode : Node , nextNode : Node ) {
5308
5321
if ( nextNode . pos < previousNode . end ) {
5309
5322
return false ;
5310
5323
}
0 commit comments