@@ -4278,11 +4278,13 @@ namespace ts {
4278
4278
return rangeIsOnSingleLine ( parentNode , currentSourceFile ! ) ? 0 : 1 ;
4279
4279
}
4280
4280
else if ( ! positionIsSynthesized ( parentNode . pos ) && ! nodeIsSynthesized ( firstChild ) && firstChild . parent === parentNode ) {
4281
- let lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter ( firstChild . pos , currentSourceFile ! , /*includeComments*/ true ) ;
4282
- if ( lines === 0 ) {
4283
- lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter ( firstChild . pos , currentSourceFile ! , /*includeComments*/ false ) ;
4281
+ if ( preserveNewlines ) {
4282
+ const lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter ( firstChild . pos , currentSourceFile ! , /*includeComments*/ true ) ;
4283
+ return lines === 0
4284
+ ? getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter ( firstChild . pos , currentSourceFile ! , /*includeComments*/ false )
4285
+ : lines ;
4284
4286
}
4285
- return printerOptions . preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4287
+ return rangeStartPositionsAreOnSameLine ( parentNode , firstChild , currentSourceFile ! ) ? 0 : 1 ;
4286
4288
}
4287
4289
else if ( synthesizedNodeStartsOnNewLine ( firstChild , format ) ) {
4288
4290
return 1 ;
@@ -4297,8 +4299,8 @@ namespace ts {
4297
4299
return 0 ;
4298
4300
}
4299
4301
else if ( ! nodeIsSynthesized ( previousNode ) && ! nodeIsSynthesized ( nextNode ) && previousNode . parent === nextNode . parent ) {
4300
- const lines = getEffectiveLinesBetweenRanges ( previousNode , nextNode , getLinesBetweenRangeEndAndRangeStart ) ;
4301
- return printerOptions . preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4302
+ const lines = getEffectiveLinesBetweenRanges ( previousNode , nextNode ) ;
4303
+ return preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4302
4304
}
4303
4305
else if ( synthesizedNodeStartsOnNewLine ( previousNode , format ) || synthesizedNodeStartsOnNewLine ( nextNode , format ) ) {
4304
4306
return 1 ;
@@ -4322,7 +4324,7 @@ namespace ts {
4322
4324
}
4323
4325
else if ( ! positionIsSynthesized ( parentNode . pos ) && ! nodeIsSynthesized ( lastChild ) && lastChild . parent === parentNode ) {
4324
4326
const lines = getLinesBetweenRangeEndPositions ( lastChild , parentNode , currentSourceFile ! ) ;
4325
- return printerOptions . preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4327
+ return preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4326
4328
}
4327
4329
else if ( synthesizedNodeStartsOnNewLine ( lastChild , format ) ) {
4328
4330
return 1 ;
@@ -4334,26 +4336,28 @@ namespace ts {
4334
4336
return 0 ;
4335
4337
}
4336
4338
4337
- function getEffectiveLinesBetweenRanges (
4338
- node1 : TextRange ,
4339
- node2 : TextRange ,
4340
- getLinesBetweenPositions : ( range1 : TextRange , range2 : TextRange , sourceFile : SourceFile , includeComments : boolean ) => number
4341
- ) {
4342
- // We start by measuring the line difference from parentNode's start to node2's comments start,
4343
- // so that this is counted as a one line difference, not two:
4339
+ function getEffectiveLinesBetweenRanges ( node1 : TextRange , node2 : TextRange ) {
4340
+ // If 'preserveNewlines' is disabled, skip the more accurate check that might require
4341
+ // querying for source position twice.
4342
+ if ( ! preserveNewlines ) {
4343
+ return getLinesBetweenRangeEndAndRangeStart ( node1 , node2 , currentSourceFile ! , /*includeComments*/ false ) ;
4344
+ }
4345
+ // We start by measuring the line difference from node1's end to node2's comments start,
4346
+ // so that this is counted as a one-line difference, not two:
4344
4347
//
4345
4348
// node1;
4346
4349
// // NODE2 COMMENT
4347
4350
// node2;
4348
- const lines = getLinesBetweenPositions ( node1 , node2 , currentSourceFile ! , /*includeComments*/ true ) ;
4351
+ const lines = getLinesBetweenRangeEndAndRangeStart ( node1 , node2 , currentSourceFile ! , /*includeComments*/ true ) ;
4349
4352
if ( lines === 0 ) {
4350
4353
// However, if the line difference considering node2's comments was 0, we might have this:
4351
4354
//
4352
4355
// node1; // NODE2 COMMENT
4353
4356
// node2;
4354
4357
//
4355
- // in which case we should be ignoring node2's comment.
4356
- return getLinesBetweenPositions ( node1 , node2 , currentSourceFile ! , /*includeComments*/ false ) ;
4358
+ // in which case we should be ignoring node2's comment, so this too is counted as
4359
+ // a one-line difference, not zero.
4360
+ return getLinesBetweenRangeEndAndRangeStart ( node1 , node2 , currentSourceFile ! , /*includeComments*/ false ) ;
4357
4361
}
4358
4362
return lines ;
4359
4363
}
@@ -4386,7 +4390,7 @@ namespace ts {
4386
4390
}
4387
4391
4388
4392
if ( ! nodeIsSynthesized ( parent ) && ! nodeIsSynthesized ( node1 ) && ! nodeIsSynthesized ( node2 ) ) {
4389
- const lines = getEffectiveLinesBetweenRanges ( node1 , node2 , getLinesBetweenRangeEndAndRangeStart ) ;
4393
+ const lines = getEffectiveLinesBetweenRanges ( node1 , node2 ) ;
4390
4394
return preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4391
4395
}
4392
4396
0 commit comments