Skip to content

Commit e1adde6

Browse files
committed
Added a solution to address the aggressive indentation issue, but it causes test formattingConditionals to fail
1 parent 9088920 commit e1adde6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ export namespace SmartIndenter {
438438
// branch beginning on the line that the whenTrue branch ends.
439439
export function childIsUnindentedBranchOfConditionalExpression(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFileLike): boolean {
440440
if (isConditionalExpression(parent) && (child === parent.whenTrue || child === parent.whenFalse)) {
441+
441442
const conditionEndLine = getLineAndCharacterOfPosition(sourceFile, parent.condition.end).line;
442443
if (child === parent.whenTrue) {
443444
return childStartLine === conditionEndLine;
@@ -450,8 +451,20 @@ export namespace SmartIndenter {
450451
// ? 1 : ( L1: whenTrue indented because it's on a new line
451452
// 0 L2: indented two stops, one because whenTrue was indented
452453
// ); and one because of the parentheses spanning multiple lines
454+
455+
// However, when condition and whenTrue are on the same line, whenFalse should not be indented:
456+
//
457+
// const z =
458+
// 0 ? 1 : L1: whenTrue indented because it's on a new line
459+
// 2 ? 3 : L2: not indented
460+
// 4; L3: not indented
453461
const trueStartLine = getStartLineAndCharacterForNode(parent.whenTrue, sourceFile).line;
454462
const trueEndLine = getLineAndCharacterOfPosition(sourceFile, parent.whenTrue.end).line;
463+
464+
if (conditionEndLine === trueStartLine && trueStartLine === trueEndLine && trueEndLine !== childStartLine) {
465+
return true;
466+
}
467+
455468
return conditionEndLine === trueStartLine && trueEndLine === childStartLine;
456469
}
457470
}
@@ -654,7 +667,6 @@ export namespace SmartIndenter {
654667
case SyntaxKind.VariableStatement:
655668
case SyntaxKind.ExportAssignment:
656669
case SyntaxKind.ReturnStatement:
657-
case SyntaxKind.ConditionalExpression:
658670
case SyntaxKind.ArrayBindingPattern:
659671
case SyntaxKind.ObjectBindingPattern:
660672
case SyntaxKind.JsxOpeningElement:
@@ -695,6 +707,14 @@ export namespace SmartIndenter {
695707
return true;
696708
}
697709
break;
710+
case SyntaxKind.ConditionalExpression:
711+
if (child && sourceFile) {
712+
const childStartCharacter = sourceFile.getLineAndCharacterOfPosition(skipTrivia(sourceFile.text, child.pos)).character;
713+
if (!rangeIsOnOneLine(sourceFile, parent) && childStartCharacter !== SyntaxKind.QuestionToken) {
714+
return false;
715+
}
716+
}
717+
return true;
698718
case SyntaxKind.DoStatement:
699719
case SyntaxKind.WhileStatement:
700720
case SyntaxKind.ForInStatement:

0 commit comments

Comments
 (0)