Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions internal/format/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down