From a39b9d3bbdda417dff103fffe950dbf9e82f7e9e Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:05:00 -0800 Subject: [PATCH] Preserve formatter alignment across tabs and enum decorators Mixed leading whitespace must advance to the next tab stop so multiline comments retain their relative indentation when formatting changes the surrounding block indentation. Match the enum keyword after decorators so partially written declarations stay aligned instead of gaining an extra indentation level. --- tsc/internal/format/indent.go | 2 +- tsc/internal/format/span.go | 2 +- .../fourslash/tests/formattingDecorators_test.go | 14 ++++++++++++++ .../formattingMultilineCommentsWithTabs1_test.go | 10 ++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/tsc/internal/format/indent.go b/tsc/internal/format/indent.go index f440423b64397..b98fd95ff21d0 100644 --- a/tsc/internal/format/indent.go +++ b/tsc/internal/format/indent.go @@ -486,7 +486,7 @@ func findFirstNonWhitespaceCharacterAndColumn(startPos int, endPos int, sourceFi if ch == '\t' { if options.TabSize > 0 { - column += options.TabSize + (column % options.TabSize) + column += options.TabSize - (column % options.TabSize) } } else { column++ diff --git a/tsc/internal/format/span.go b/tsc/internal/format/span.go index df9d11119b2cb..d5b37160d4d49 100644 --- a/tsc/internal/format/span.go +++ b/tsc/internal/format/span.go @@ -1227,7 +1227,7 @@ func getFirstNonDecoratorTokenOfNode(node *ast.Node) ast.Kind { case ast.KindFunctionDeclaration: return ast.KindFunctionKeyword case ast.KindEnumDeclaration: - return ast.KindEnumDeclaration + return ast.KindEnumKeyword case ast.KindGetAccessor: return ast.KindGetKeyword case ast.KindSetAccessor: diff --git a/tsc/internal/fourslash/tests/formattingDecorators_test.go b/tsc/internal/fourslash/tests/formattingDecorators_test.go index f7e3839fa08d4..0155373b71f9c 100644 --- a/tsc/internal/fourslash/tests/formattingDecorators_test.go +++ b/tsc/internal/fourslash/tests/formattingDecorators_test.go @@ -7,6 +7,20 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/testutil" ) +func TestFormattingDecoratedEnum(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + // Preserve alignment while editing an enum with an invalid decorator. + const content = `@decorator +enum E { + A +}` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.FormatDocument(t, "") + f.VerifyCurrentFileContent(t, content) +} + func TestFormattingDecorators(t *testing.T) { t.Parallel() defer testutil.RecoverAndFail(t, "Panic on fourslash test") diff --git a/tsc/internal/fourslash/tests/formattingMultilineCommentsWithTabs1_test.go b/tsc/internal/fourslash/tests/formattingMultilineCommentsWithTabs1_test.go index 18ce87bf55b90..c391282c4c193 100644 --- a/tsc/internal/fourslash/tests/formattingMultilineCommentsWithTabs1_test.go +++ b/tsc/internal/fourslash/tests/formattingMultilineCommentsWithTabs1_test.go @@ -7,6 +7,16 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/testutil" ) +func TestFormattingMultilineCommentWithMixedIndentation(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = "function f() {\n \t/* comment\n * continuation\n */\n}" + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.FormatDocument(t, "") + f.VerifyCurrentFileContent(t, "function f() {\n /* comment\n * continuation\n */\n}") +} + func TestFormattingMultilineCommentsWithTabs1(t *testing.T) { t.Parallel() defer testutil.RecoverAndFail(t, "Panic on fourslash test")