diff --git a/internal/format/format_test.go b/internal/format/format_test.go index 1bf7945076..f38ef3f89e 100644 --- a/internal/format/format_test.go +++ b/internal/format/format_test.go @@ -58,3 +58,32 @@ func TestFormatNoTrailingNewline(t *testing.T) { }) } } + +func TestTernaryWithTabs(t *testing.T) { + t.Parallel() + + text := "const test = (a: string) => (\n\ta === '1' ? (\n\t\t10\n\t) : (\n\t\t12\n\t)\n)" + expected := "const test=(a: string) => (\n\ta==='1'? (\n\t\t10\n\t):(\n\t\t12\n\t)\n)" + + ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{ + EditorSettings: format.EditorSettings{ + TabSize: 4, + IndentSize: 4, + BaseIndentSize: 0, + NewLineCharacter: "\n", + ConvertTabsToSpaces: false, + IndentStyle: format.IndentStyleSmart, + TrimTrailingWhitespace: true, + }, + }, "\n") + + sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ + FileName: "/test.ts", + Path: "/test.ts", + }, text, core.ScriptKindTS) + + edits := format.FormatDocument(ctx, sourceFile) + newText := applyBulkEdits(text, edits) + + assert.Equal(t, expected, newText) +} diff --git a/internal/format/span.go b/internal/format/span.go index f8d73d40b6..faafb32801 100644 --- a/internal/format/span.go +++ b/internal/format/span.go @@ -1055,9 +1055,12 @@ func (w *formatSpanWorker) consumeTokenAndAdvanceScanner(currentTokenInfo tokenI indentNextTokenOrTrivia := true if len(currentTokenInfo.leadingTrivia) > 0 { commentIndentation := dynamicIndenation.getIndentationForComment(currentTokenInfo.token.Kind, tokenIndentation, container) - indentNextTokenOrTrivia = w.indentTriviaItems(currentTokenInfo.leadingTrivia, commentIndentation, indentNextTokenOrTrivia, func(item TextRangeWithKind) { - w.insertIndentation(item.Loc.Pos(), commentIndentation, false) - }) + // Only indent comments if we have a valid indentation value + if commentIndentation != -1 { + indentNextTokenOrTrivia = w.indentTriviaItems(currentTokenInfo.leadingTrivia, commentIndentation, indentNextTokenOrTrivia, func(item TextRangeWithKind) { + w.insertIndentation(item.Loc.Pos(), commentIndentation, false) + }) + } } // indent token only if is it is in target range and does not overlap with any error ranges @@ -1091,6 +1094,10 @@ func (i *dynamicIndenter) getIndentationForComment(kind ast.Kind, tokenIndentati // // comment // } case ast.KindCloseBraceToken, ast.KindCloseBracketToken, ast.KindCloseParenToken: + // If indentation is -1 (Unknown), we should not add delta to it + if i.indentation == -1 { + return -1 + } return i.indentation + i.getDelta(container) } if tokenIndentation != -1 { @@ -1113,6 +1120,10 @@ func (i *dynamicIndenter) getIndentationForComment(kind ast.Kind, tokenIndentati // // > yValue; func (i *dynamicIndenter) getIndentationForToken(line int, kind ast.Kind, container *ast.Node, suppressDelta bool) int { + // If indentation is -1 (Unknown), we should not add delta to it + if i.indentation == -1 { + return -1 + } if !suppressDelta && i.shouldAddDelta(line, kind, container) { return i.indentation + i.getDelta(container) }