diff --git a/internal/format/api_test.go b/internal/format/api_test.go index 048d816f58..99b2c30ebd 100644 --- a/internal/format/api_test.go +++ b/internal/format/api_test.go @@ -64,6 +64,38 @@ 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 (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 + }) } func BenchmarkFormat(b *testing.B) { 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 }