Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 4 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
45 changes: 45 additions & 0 deletions internal/format/bug_test.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this file. Copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted in 3a62c9b8.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package format_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/format"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/parser"
"gotest.tools/v3/assert"
)

func TestFormatWhitespaceBetweenComments(t *testing.T) {
t.Parallel()
// Regression test: whitespace-only line between two single-line comments
// should not cause overlapping/duplicate edits that break formatting.
text := " const x = \"wont format\"\n//\n \n//\n"
ctx := format.WithFormatCodeSettings(t.Context(), lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: core.TSTrue,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: core.TSTrue,
},
}, "\n")
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, text, core.ScriptKindTS)
edits := format.FormatDocument(ctx, sourceFile)

// Verify no overlapping or duplicate edits
for i := 1; i < len(edits); i++ {
assert.Assert(t, edits[i].Pos() >= edits[i-1].End(),
"Overlapping edits: edit %d [%d,%d) overlaps with edit %d [%d,%d)",
i-1, edits[i-1].Pos(), edits[i-1].End(), i, edits[i].Pos(), edits[i].End())
}

// Verify edits can be applied without error
result := applyBulkEdits(text, edits)
assert.Assert(t, len(result) > 0)
}
4 changes: 3 additions & 1 deletion internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions internal/fourslash/tests/formatWhitespaceBetweenComments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fourslash_test

import (
"testing"

"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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I prefer when the contents start on the next line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved content to start on next line in d90bcc77.

//

//
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.FormatDocument(t, "")
f.VerifyCurrentFileContent(t, `const x = "wont format"
//

//
`)
}
Loading