diff --git a/internal/format/span.go b/internal/format/span.go index 1f448da00f8..052dc17e3d5 100644 --- a/internal/format/span.go +++ b/internal/format/span.go @@ -795,7 +795,9 @@ func (w *formatSpanWorker) trimTrailingWhitespacesForRemainingRange(trivias []Te w.trimTrailingWitespacesForPositions(startPos, trivia.Loc.Pos()-1, w.previousRange) } - startPos = trivia.Loc.End() + 1 + if trivia.Loc.End()+1 > startPos { + startPos = trivia.Loc.End() + 1 + } } } diff --git a/internal/fourslash/tests/formatWhitespaceBetweenComments_test.go b/internal/fourslash/tests/formatWhitespaceBetweenComments_test.go new file mode 100644 index 00000000000..6f5f9a7bf3b --- /dev/null +++ b/internal/fourslash/tests/formatWhitespaceBetweenComments_test.go @@ -0,0 +1,52 @@ +package fourslash_test + +import ( + "testing" + + "github.com/microsoft/typescript-go/internal/core" + "github.com/microsoft/typescript-go/internal/fourslash" + "github.com/microsoft/typescript-go/internal/testutil" +) + +func TestFormatWhitespaceBetweenComments(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = ` + const x = "wont format" +// + +// +` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.FormatDocument(t, "") + f.VerifyCurrentFileContent(t, ` +const x = "wont format" +// + +// +`) +} + +func TestFormatWhitespaceBetweenCommentsPreserveTrailingWhitespace(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = ` + const x = "wont format" +// + +// +` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + opts := f.GetOptions() + opts.FormatCodeSettings.TrimTrailingWhitespace = core.TSFalse + f.Configure(t, opts) + f.FormatDocument(t, "") + f.VerifyCurrentFileContent(t, ` +const x = "wont format" +// + +// +`) +}