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