@@ -112,7 +112,7 @@ namespace ts.formatting {
112
112
let previous : Node | undefined ;
113
113
let current = precedingToken ;
114
114
while ( current ) {
115
- if ( positionBelongsToNode ( current , position , sourceFile ) && shouldIndentChildNode ( current , previous , /*isNextChild*/ true ) ) {
115
+ if ( positionBelongsToNode ( current , position , sourceFile ) && shouldIndentChildNode ( current , previous , sourceFile , /*isNextChild*/ true ) ) {
116
116
const currentStart = getStartLineAndCharacterForNode ( current , sourceFile ) ;
117
117
const nextTokenKind = nextTokenIsCurlyBraceOnSameLineAsCursor ( precedingToken , current , lineAtPosition , sourceFile ) ;
118
118
const indentationDelta = nextTokenKind !== NextTokenKind . Unknown
@@ -193,7 +193,7 @@ namespace ts.formatting {
193
193
}
194
194
195
195
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
196
- if ( shouldIndentChildNode ( parent , current , isNextChild ) && ! parentAndChildShareLine ) {
196
+ if ( shouldIndentChildNode ( parent , current , sourceFile , isNextChild ) && ! parentAndChildShareLine ) {
197
197
indentationDelta += options . indentSize ;
198
198
}
199
199
@@ -531,21 +531,18 @@ namespace ts.formatting {
531
531
return false ;
532
532
}
533
533
534
- export function nodeWillIndentChild ( parent : TextRangeWithKind , child : Node | undefined , indentByDefault : boolean ) : boolean {
534
+ export function nodeWillIndentChild ( parent : TextRangeWithKind , child : TextRangeWithKind | undefined , sourceFile : SourceFileLike | undefined , indentByDefault : boolean ) : boolean {
535
535
const childKind = child ? child . kind : SyntaxKind . Unknown ;
536
536
537
537
switch ( parent . kind ) {
538
538
case SyntaxKind . VariableDeclaration :
539
539
case SyntaxKind . PropertyAssignment :
540
540
case SyntaxKind . ObjectLiteralExpression :
541
- if ( childKind === SyntaxKind . ObjectLiteralExpression ) {
542
- const sourceFile = child . getSourceFile ( ) ;
543
- if ( sourceFile ) {
544
- // May not be defined for synthesized nodes.
545
- const startLine = sourceFile . getLineAndCharacterOfPosition ( child . getStart ( ) ) . line ;
546
- const endLine = sourceFile . getLineAndCharacterOfPosition ( child . getEnd ( ) ) . line ;
547
- return startLine === endLine ;
548
- }
541
+ if ( sourceFile && childKind === SyntaxKind . ObjectLiteralExpression ) {
542
+ const childStart = skipTrivia ( sourceFile . text , child . pos ) ;
543
+ const startLine = sourceFile . getLineAndCharacterOfPosition ( childStart ) . line ;
544
+ const endLine = sourceFile . getLineAndCharacterOfPosition ( child . end ) . line ;
545
+ return startLine === endLine ;
549
546
}
550
547
break ;
551
548
case SyntaxKind . DoStatement :
@@ -599,8 +596,8 @@ namespace ts.formatting {
599
596
* True when the parent node should indent the given child by an explicit rule.
600
597
* @param isNextChild If true, we are judging indent of a hypothetical child *after* this one, not the current child.
601
598
*/
602
- export function shouldIndentChildNode ( parent : TextRangeWithKind , child ?: Node , isNextChild = false ) : boolean {
603
- return ( nodeContentIsAlwaysIndented ( parent . kind ) || nodeWillIndentChild ( parent , child , /*indentByDefault*/ false ) )
599
+ export function shouldIndentChildNode ( parent : TextRangeWithKind , child ?: Node , sourceFile ?: SourceFileLike , isNextChild = false ) : boolean {
600
+ return ( nodeContentIsAlwaysIndented ( parent . kind ) || nodeWillIndentChild ( parent , child , sourceFile , /*indentByDefault*/ false ) )
604
601
&& ! ( isNextChild && child && isControlFlowEndingStatement ( child . kind , parent ) ) ;
605
602
}
606
603
}
0 commit comments