@@ -2279,10 +2279,10 @@ namespace ts {
2279
2279
function emitPropertyAccessExpression ( node : PropertyAccessExpression ) {
2280
2280
const expression = cast ( emitExpression ( node . expression ) , isExpression ) ;
2281
2281
const token = getDotOrQuestionDotToken ( node ) ;
2282
- const indentBeforeDot = needsIndentation ( node , node . expression , token ) ;
2283
- const indentAfterDot = needsIndentation ( node , token , node . name ) ;
2282
+ const linesBeforeDot = getLinesBetweenNodes ( node , node . expression , token ) ;
2283
+ const linesAfterDot = getLinesBetweenNodes ( node , token , node . name ) ;
2284
2284
2285
- increaseIndentIf ( indentBeforeDot , /*writeSpaceIfNotIndenting*/ false ) ;
2285
+ writeLinesAndIndent ( linesBeforeDot , /*writeSpaceIfNotIndenting*/ false ) ;
2286
2286
2287
2287
const shouldEmitDotDot =
2288
2288
token . kind !== SyntaxKind . QuestionDotToken &&
@@ -2295,9 +2295,9 @@ namespace ts {
2295
2295
}
2296
2296
2297
2297
emitTokenWithComment ( token . kind , node . expression . end , writePunctuation , node ) ;
2298
- increaseIndentIf ( indentAfterDot , /*writeSpaceIfNotIndenting*/ false ) ;
2298
+ writeLinesAndIndent ( linesAfterDot , /*writeSpaceIfNotIndenting*/ false ) ;
2299
2299
emit ( node . name ) ;
2300
- decreaseIndentIf ( indentBeforeDot , indentAfterDot ) ;
2300
+ decreaseIndentIf ( linesBeforeDot , linesAfterDot ) ;
2301
2301
}
2302
2302
2303
2303
// 1..toString is a valid property access, emit a dot after the literal
@@ -2462,20 +2462,20 @@ namespace ts {
2462
2462
}
2463
2463
case EmitBinaryExpressionState . EmitRight : {
2464
2464
const isCommaOperator = node . operatorToken . kind !== SyntaxKind . CommaToken ;
2465
- const indentBeforeOperator = needsIndentation ( node , node . left , node . operatorToken ) ;
2466
- const indentAfterOperator = needsIndentation ( node , node . operatorToken , node . right ) ;
2467
- increaseIndentIf ( indentBeforeOperator , isCommaOperator ) ;
2465
+ const linesBeforeOperator = getLinesBetweenNodes ( node , node . left , node . operatorToken ) ;
2466
+ const linesAfterOperator = getLinesBetweenNodes ( node , node . operatorToken , node . right ) ;
2467
+ writeLinesAndIndent ( linesBeforeOperator , isCommaOperator ) ;
2468
2468
emitLeadingCommentsOfPosition ( node . operatorToken . pos ) ;
2469
2469
writeTokenNode ( node . operatorToken , node . operatorToken . kind === SyntaxKind . InKeyword ? writeKeyword : writeOperator ) ;
2470
2470
emitTrailingCommentsOfPosition ( node . operatorToken . end , /*prefixSpace*/ true ) ; // Binary operators should have a space before the comment starts
2471
- increaseIndentIf ( indentAfterOperator , /*writeSpaceIfNotIndenting*/ true ) ;
2471
+ writeLinesAndIndent ( linesAfterOperator , /*writeSpaceIfNotIndenting*/ true ) ;
2472
2472
maybePipelineEmitExpression ( node . right ) ;
2473
2473
break ;
2474
2474
}
2475
2475
case EmitBinaryExpressionState . FinishEmit : {
2476
- const indentBeforeOperator = needsIndentation ( node , node . left , node . operatorToken ) ;
2477
- const indentAfterOperator = needsIndentation ( node , node . operatorToken , node . right ) ;
2478
- decreaseIndentIf ( indentBeforeOperator , indentAfterOperator ) ;
2476
+ const linesBeforeOperator = getLinesBetweenNodes ( node , node . left , node . operatorToken ) ;
2477
+ const linesAfterOperator = getLinesBetweenNodes ( node , node . operatorToken , node . right ) ;
2478
+ decreaseIndentIf ( linesBeforeOperator , linesAfterOperator ) ;
2479
2479
stackIndex -- ;
2480
2480
break ;
2481
2481
}
@@ -2519,23 +2519,23 @@ namespace ts {
2519
2519
}
2520
2520
2521
2521
function emitConditionalExpression ( node : ConditionalExpression ) {
2522
- const indentBeforeQuestion = needsIndentation ( node , node . condition , node . questionToken ) ;
2523
- const indentAfterQuestion = needsIndentation ( node , node . questionToken , node . whenTrue ) ;
2524
- const indentBeforeColon = needsIndentation ( node , node . whenTrue , node . colonToken ) ;
2525
- const indentAfterColon = needsIndentation ( node , node . colonToken , node . whenFalse ) ;
2522
+ const linesBeforeQuestion = getLinesBetweenNodes ( node , node . condition , node . questionToken ) ;
2523
+ const linesAfterQuestion = getLinesBetweenNodes ( node , node . questionToken , node . whenTrue ) ;
2524
+ const linesBeforeColon = getLinesBetweenNodes ( node , node . whenTrue , node . colonToken ) ;
2525
+ const linesAfterColon = getLinesBetweenNodes ( node , node . colonToken , node . whenFalse ) ;
2526
2526
2527
2527
emitExpression ( node . condition ) ;
2528
- increaseIndentIf ( indentBeforeQuestion , /*writeSpaceIfNotIndenting*/ true ) ;
2528
+ writeLinesAndIndent ( linesBeforeQuestion , /*writeSpaceIfNotIndenting*/ true ) ;
2529
2529
emit ( node . questionToken ) ;
2530
- increaseIndentIf ( indentAfterQuestion , /*writeSpaceIfNotIndenting*/ true ) ;
2530
+ writeLinesAndIndent ( linesAfterQuestion , /*writeSpaceIfNotIndenting*/ true ) ;
2531
2531
emitExpression ( node . whenTrue ) ;
2532
- decreaseIndentIf ( indentBeforeQuestion , indentAfterQuestion ) ;
2532
+ decreaseIndentIf ( linesBeforeQuestion , linesAfterQuestion ) ;
2533
2533
2534
- increaseIndentIf ( indentBeforeColon , /*writeSpaceIfNotIndenting*/ true ) ;
2534
+ writeLinesAndIndent ( linesBeforeColon , /*writeSpaceIfNotIndenting*/ true ) ;
2535
2535
emit ( node . colonToken ) ;
2536
- increaseIndentIf ( indentAfterColon , /*writeSpaceIfNotIndenting*/ true ) ;
2536
+ writeLinesAndIndent ( linesAfterColon , /*writeSpaceIfNotIndenting*/ true ) ;
2537
2537
emitExpression ( node . whenFalse ) ;
2538
- decreaseIndentIf ( indentBeforeColon , indentAfterColon ) ;
2538
+ decreaseIndentIf ( linesBeforeColon , linesAfterColon ) ;
2539
2539
}
2540
2540
2541
2541
function emitTemplateExpression ( node : TemplateExpression ) {
@@ -4025,7 +4025,7 @@ namespace ts {
4025
4025
// Emit each child.
4026
4026
let previousSibling : Node | undefined ;
4027
4027
let previousSourceFileTextKind : ReturnType < typeof recordBundleFileInternalSectionStart > ;
4028
- let shouldDecreaseIndentAfterEmit = false ;
4028
+ let shouldDecreaselinesAfterEmit = false ;
4029
4029
for ( let i = 0 ; i < count ; i ++ ) {
4030
4030
const child = children ! [ start + i ] ;
4031
4031
@@ -4055,7 +4055,7 @@ namespace ts {
4055
4055
// line, we should increase the indent.
4056
4056
if ( ( format & ( ListFormat . LinesMask | ListFormat . Indented ) ) === ListFormat . SingleLine ) {
4057
4057
increaseIndent ( ) ;
4058
- shouldDecreaseIndentAfterEmit = true ;
4058
+ shouldDecreaselinesAfterEmit = true ;
4059
4059
}
4060
4060
4061
4061
writeLine ( separatingLineTerminatorCount ) ;
@@ -4080,9 +4080,9 @@ namespace ts {
4080
4080
4081
4081
emit ( child ) ;
4082
4082
4083
- if ( shouldDecreaseIndentAfterEmit ) {
4083
+ if ( shouldDecreaselinesAfterEmit ) {
4084
4084
decreaseIndent ( ) ;
4085
- shouldDecreaseIndentAfterEmit = false ;
4085
+ shouldDecreaselinesAfterEmit = false ;
4086
4086
}
4087
4087
4088
4088
previousSibling = child ;
@@ -4244,10 +4244,10 @@ namespace ts {
4244
4244
}
4245
4245
}
4246
4246
4247
- function increaseIndentIf ( value : boolean , writeSpaceIfNotIndenting : boolean ) {
4248
- if ( value ) {
4247
+ function writeLinesAndIndent ( lineCount : number , writeSpaceIfNotIndenting : boolean ) {
4248
+ if ( lineCount ) {
4249
4249
increaseIndent ( ) ;
4250
- writeLine ( ) ;
4250
+ writeLine ( lineCount ) ;
4251
4251
}
4252
4252
else if ( writeSpaceIfNotIndenting ) {
4253
4253
writeSpace ( ) ;
@@ -4258,7 +4258,7 @@ namespace ts {
4258
4258
// previous indent values to be considered at a time. This also allows caller to just
4259
4259
// call this once, passing in all their appropriate indent values, instead of needing
4260
4260
// to call this helper function multiple times.
4261
- function decreaseIndentIf ( value1 : boolean , value2 : boolean ) {
4261
+ function decreaseIndentIf ( value1 : boolean | number , value2 : boolean | number ) {
4262
4262
if ( value1 ) {
4263
4263
decreaseIndent ( ) ;
4264
4264
}
@@ -4278,7 +4278,10 @@ 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
- const lines = getEffectiveLinesBetweenRanges ( parentNode , firstChild , getLinesBetweenRangeStartPositions ) ;
4281
+ let lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter ( firstChild . pos , currentSourceFile ! , /*includeComments*/ true ) ;
4282
+ if ( lines === 0 ) {
4283
+ lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter ( firstChild . pos , currentSourceFile ! , /*includeComments*/ false ) ;
4284
+ }
4282
4285
return printerOptions . preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4283
4286
}
4284
4287
else if ( synthesizedNodeStartsOnNewLine ( firstChild , format ) ) {
@@ -4339,15 +4342,15 @@ namespace ts {
4339
4342
// We start by measuring the line difference from parentNode's start to node2's comments start,
4340
4343
// so that this is counted as a one line difference, not two:
4341
4344
//
4342
- // function node1() {
4343
- // // NODE2 COMMENT
4344
- // node2;
4345
+ // node1;
4346
+ // // NODE2 COMMENT
4347
+ // node2;
4345
4348
const lines = getLinesBetweenPositions ( node1 , node2 , currentSourceFile ! , /*includeComments*/ true ) ;
4346
4349
if ( lines === 0 ) {
4347
4350
// However, if the line difference considering node2's comments was 0, we might have this:
4348
4351
//
4349
- // function node1() { // NODE2 COMMENT
4350
- // node2;
4352
+ // node1; // NODE2 COMMENT
4353
+ // node2;
4351
4354
//
4352
4355
// in which case we should be ignoring node2's comment.
4353
4356
return getLinesBetweenPositions ( node1 , node2 , currentSourceFile ! , /*includeComments*/ false ) ;
@@ -4368,9 +4371,9 @@ namespace ts {
4368
4371
return ( format & ListFormat . PreferNewLine ) !== 0 ;
4369
4372
}
4370
4373
4371
- function needsIndentation ( parent : Node , node1 : Node , node2 : Node ) : boolean {
4374
+ function getLinesBetweenNodes ( parent : Node , node1 : Node , node2 : Node ) : number {
4372
4375
if ( getEmitFlags ( parent ) & EmitFlags . NoIndentation ) {
4373
- return false ;
4376
+ return 0 ;
4374
4377
}
4375
4378
4376
4379
parent = skipSynthesizedParentheses ( parent ) ;
@@ -4379,13 +4382,15 @@ namespace ts {
4379
4382
4380
4383
// Always use a newline for synthesized code if the synthesizer desires it.
4381
4384
if ( getStartsOnNewLine ( node2 ) ) {
4382
- return true ;
4385
+ return 1 ;
4386
+ }
4387
+
4388
+ if ( ! nodeIsSynthesized ( parent ) && ! nodeIsSynthesized ( node1 ) && ! nodeIsSynthesized ( node2 ) ) {
4389
+ const lines = getEffectiveLinesBetweenRanges ( node1 , node2 , getLinesBetweenRangeEndAndRangeStart ) ;
4390
+ return preserveNewlines ? lines : Math . min ( lines , 1 ) ;
4383
4391
}
4384
4392
4385
- return ! nodeIsSynthesized ( parent )
4386
- && ! nodeIsSynthesized ( node1 )
4387
- && ! nodeIsSynthesized ( node2 )
4388
- && ! rangeEndIsOnSameLineAsRangeStart ( node1 , node2 , currentSourceFile ! ) ;
4393
+ return 0 ;
4389
4394
}
4390
4395
4391
4396
function isEmptyBlock ( block : BlockLike ) {
0 commit comments