From 4c4f8e10f8736427625b8140a1ece09b4bff58ea Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 22 Oct 2025 18:34:41 +0000 Subject: [PATCH 1/3] Initial plan From 1d49c0f89f2257d1aee606fc5c7538ef7baa8327 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 22 Oct 2025 18:46:34 +0000 Subject: [PATCH 2/3] Add test case for chained method call with comment formatting panic Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com> --- internal/format/api_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/internal/format/api_test.go b/internal/format/api_test.go index 048d816f58..2ea16beedc 100644 --- a/internal/format/api_test.go +++ b/internal/format/api_test.go @@ -64,6 +64,37 @@ func TestFormat(t *testing.T) { assert.Assert(t, len(newText) > 0) assert.Assert(t, text != newText) }) + + t.Run("chained method call with comment", func(t *testing.T) { + t.Parallel() + ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{ + EditorSettings: format.EditorSettings{ + TabSize: 4, + IndentSize: 4, + BaseIndentSize: 0, + NewLineCharacter: "\n", + ConvertTabsToSpaces: true, + IndentStyle: format.IndentStyleSmart, + TrimTrailingWhitespace: true, + }, + InsertSpaceBeforeTypeAnnotation: core.TSTrue, + }, "\n") + + text := `foo + .bar() + // A second call + .baz(); +` + sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ + FileName: "/test.ts", + Path: "/test.ts", + }, text, core.ScriptKindTS) + + // This should not panic + edits := format.FormatDocument(ctx, sourceFile) + newText := applyBulkEdits(text, edits) + assert.Assert(t, len(newText) > 0) + }) } func BenchmarkFormat(b *testing.B) { From 0490a8f85ba90270f375c7f7178cfbb16f1c25c1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 22 Oct 2025 18:50:08 +0000 Subject: [PATCH 3/3] Fix panic in formatting chained method calls with comments The issue was that getIndentationForComment was not handling the case where tokenIndentation is -1 (Unknown). In the TypeScript source, when tokenIndentation is not Unknown, it returns tokenIndentation, otherwise it returns the base indentation. This fixes the panic that occurred when formatting code like: ``` foo .bar() // A second call .baz(); ``` Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com> --- internal/format/api_test.go | 3 ++- internal/format/span.go | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/format/api_test.go b/internal/format/api_test.go index 2ea16beedc..99b2c30ebd 100644 --- a/internal/format/api_test.go +++ b/internal/format/api_test.go @@ -90,10 +90,11 @@ func TestFormat(t *testing.T) { Path: "/test.ts", }, text, core.ScriptKindTS) - // This should not panic + // This should not panic (was panic'ing before fix with "negative Repeat count") edits := format.FormatDocument(ctx, sourceFile) newText := applyBulkEdits(text, edits) assert.Assert(t, len(newText) > 0) + // The exact formatting is not important for this test, just that it doesn't panic }) } diff --git a/internal/format/span.go b/internal/format/span.go index c8d0096bed..3a1ea475cd 100644 --- a/internal/format/span.go +++ b/internal/format/span.go @@ -1093,6 +1093,9 @@ func (i *dynamicIndenter) getIndentationForComment(kind ast.Kind, tokenIndentati case ast.KindCloseBraceToken, ast.KindCloseBracketToken, ast.KindCloseParenToken: return i.indentation + i.getDelta(container) } + if tokenIndentation != -1 { + return tokenIndentation + } return i.indentation }