@@ -4271,8 +4271,9 @@ namespace ts {
4271
4271
if ( firstChild === undefined ) {
4272
4272
return rangeIsOnSingleLine ( parentNode , currentSourceFile ! ) ? 0 : 1 ;
4273
4273
}
4274
- else if ( ! positionIsSynthesized ( parentNode . pos ) && ! nodeIsSynthesized ( firstChild ) ) {
4275
- return getLinesBetweenRangeEndAndRangeStart ( parentNode , firstChild , currentSourceFile ! ) ;
4274
+ else if ( ! positionIsSynthesized ( parentNode . pos ) && ! nodeIsSynthesized ( firstChild ) && firstChild . parent === parentNode ) {
4275
+ const lines = getEffectiveLinesBetweenRanges ( parentNode , firstChild , getLinesBetweenRangeStartPositions ) ;
4276
+ return printerOptions . preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4276
4277
}
4277
4278
else if ( synthesizedNodeStartsOnNewLine ( firstChild , format ) ) {
4278
4279
return 1 ;
@@ -4286,8 +4287,9 @@ namespace ts {
4286
4287
if ( previousNode === undefined || nextNode === undefined ) {
4287
4288
return 0 ;
4288
4289
}
4289
- else if ( ! nodeIsSynthesized ( previousNode ) && ! nodeIsSynthesized ( nextNode ) ) {
4290
- return getLinesBetweenRangeEndAndRangeStart ( previousNode , nextNode , currentSourceFile ! ) ;
4290
+ else if ( ! nodeIsSynthesized ( previousNode ) && ! nodeIsSynthesized ( nextNode ) && previousNode . parent === nextNode . parent ) {
4291
+ const lines = getEffectiveLinesBetweenRanges ( previousNode , nextNode , getLinesBetweenRangeEndAndRangeStart ) ;
4292
+ return printerOptions . preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4291
4293
}
4292
4294
else if ( synthesizedNodeStartsOnNewLine ( previousNode , format ) || synthesizedNodeStartsOnNewLine ( nextNode , format ) ) {
4293
4295
return 1 ;
@@ -4309,19 +4311,44 @@ namespace ts {
4309
4311
if ( lastChild === undefined ) {
4310
4312
return rangeIsOnSingleLine ( parentNode , currentSourceFile ! ) ? 0 : 1 ;
4311
4313
}
4312
- else if ( ! positionIsSynthesized ( parentNode . pos ) && ! nodeIsSynthesized ( lastChild ) ) {
4313
- return getLinesBetweenRangeEndAndRangeStart ( parentNode , lastChild , currentSourceFile ! ) ;
4314
+ else if ( ! positionIsSynthesized ( parentNode . pos ) && ! nodeIsSynthesized ( lastChild ) && lastChild . parent === parentNode ) {
4315
+ const lines = getLinesBetweenRangeEndPositions ( lastChild , parentNode , currentSourceFile ! ) ;
4316
+ return printerOptions . preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4314
4317
}
4315
- else {
4316
- return Number ( synthesizedNodeStartsOnNewLine ( lastChild , format ) ) ;
4318
+ else if ( synthesizedNodeStartsOnNewLine ( lastChild , format ) ) {
4319
+ return 1 ;
4317
4320
}
4318
4321
}
4319
- if ( format & ListFormat . MultiLine ) {
4320
- return ( format & ListFormat . NoTrailingNewLine ) === 0 ? 1 : 0 ;
4322
+ if ( format & ListFormat . MultiLine && ! ( format & ListFormat . NoTrailingNewLine ) ) {
4323
+ return 1 ;
4321
4324
}
4322
4325
return 0 ;
4323
4326
}
4324
4327
4328
+ function getEffectiveLinesBetweenRanges (
4329
+ node1 : TextRange ,
4330
+ node2 : TextRange ,
4331
+ getLinesBetweenPositions : ( range1 : TextRange , range2 : TextRange , sourceFile : SourceFile , includeComments : boolean ) => number
4332
+ ) {
4333
+ // We start by measuring the line difference from parentNode's start to node2's comments start,
4334
+ // so that this is counted as a one line difference, not two:
4335
+ //
4336
+ // function node1() {
4337
+ // // NODE2 COMMENT
4338
+ // node2;
4339
+ const lines = getLinesBetweenPositions ( node1 , node2 , currentSourceFile ! , /*includeComments*/ true ) ;
4340
+ if ( lines === 0 ) {
4341
+ // However, if the line difference considering node2's comments was 0, we might have this:
4342
+ //
4343
+ // function node1() { // NODE2 COMMENT
4344
+ // node2;
4345
+ //
4346
+ // in which case we should be ignoring node2's comment.
4347
+ return getLinesBetweenPositions ( node1 , node2 , currentSourceFile ! , /*includeComments*/ false ) ;
4348
+ }
4349
+ return lines ;
4350
+ }
4351
+
4325
4352
function synthesizedNodeStartsOnNewLine ( node : Node , format : ListFormat ) {
4326
4353
if ( nodeIsSynthesized ( node ) ) {
4327
4354
const startsOnNewLine = getStartsOnNewLine ( node ) ;
0 commit comments