Skip to content

Commit ccd9922

Browse files
Copilotjakebailey
andcommitted
Add regression test for issue #1928 (panic formatting chained method call)
Co-authored-by: jakebailey <[email protected]>
1 parent 3e7ab19 commit ccd9922

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

internal/format/comment_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,40 @@ func TestCommentFormatting(t *testing.T) {
167167
// The comment should not lose its indentation
168168
assert.Check(t, !contains(formatted, "\n// A second call"), "comment should not lose indentation")
169169
})
170+
171+
// Regression test for issue #1928 - panic when formatting chained method call with comment
172+
t.Run("format chained method call with comment (issue #1928)", func(t *testing.T) {
173+
t.Parallel()
174+
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
175+
EditorSettings: format.EditorSettings{
176+
TabSize: 4,
177+
IndentSize: 4,
178+
BaseIndentSize: 0,
179+
NewLineCharacter: "\n",
180+
ConvertTabsToSpaces: false, // Use tabs
181+
IndentStyle: format.IndentStyleSmart,
182+
TrimTrailingWhitespace: true,
183+
},
184+
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
185+
}, "\n")
186+
187+
// This code previously caused a panic with "strings: negative Repeat count"
188+
// because tokenIndentation was -1 and was being used directly for indentation
189+
originalText := "foo\n\t.bar()\n\t// A second call\n\t.baz();"
190+
191+
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
192+
FileName: "/test.ts",
193+
Path: "/test.ts",
194+
}, originalText, core.ScriptKindTS)
195+
196+
// Apply formatting - should not panic
197+
edits := format.FormatDocument(ctx, sourceFile)
198+
formatted := applyBulkEdits(originalText, edits)
199+
200+
// Verify the comment maintains proper indentation and doesn't lose it
201+
assert.Check(t, contains(formatted, "\t// A second call") || contains(formatted, " // A second call"), "comment should be indented")
202+
assert.Check(t, !contains(formatted, "\n// A second call"), "comment should not be at column 0")
203+
})
170204
}
171205

172206
func contains(s, substr string) bool {

0 commit comments

Comments
 (0)