Skip to content

Fix comment formatting bug that mangled */ tokens #1401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
71 changes: 71 additions & 0 deletions internal/format/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package format_test

import (
"strings"
"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/parser"
"gotest.tools/v3/assert"
)

func TestCommentFormatting(t *testing.T) {
t.Parallel()

t.Run("format comment issue reproduction", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: format.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
}, "\n")

// Original code that causes the bug
originalText := `class C {
/**
*
*/
async x() {}
}`

sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, originalText, core.ScriptKindTS)

// Apply formatting once
edits := format.FormatDocument(ctx, sourceFile)
firstFormatted := applyBulkEdits(originalText, edits)

// Check that the asterisk is not corrupted
assert.Check(t, !contains(firstFormatted, "*/\n /"), "should not corrupt */ to /")
assert.Check(t, contains(firstFormatted, "*/"), "should preserve */ token")
assert.Check(t, contains(firstFormatted, "async"), "should preserve async keyword")

// Apply formatting a second time to test stability
sourceFile2 := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, firstFormatted, core.ScriptKindTS)

edits2 := format.FormatDocument(ctx, sourceFile2)
secondFormatted := applyBulkEdits(firstFormatted, edits2)

// Check that second formatting doesn't introduce corruption
assert.Check(t, !contains(secondFormatted, " sync x()"), "should not corrupt async to sync")
assert.Check(t, contains(secondFormatted, "async"), "should preserve async keyword on second pass")
})
}

func contains(s, substr string) bool {
return len(substr) > 0 && strings.Contains(s, substr)
}
18 changes: 15 additions & 3 deletions internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ func (w *formatSpanWorker) indentMultilineComment(commentRange core.TextRange, i
for line := startLine; line < endLine; line++ {
endOfLine := scanner.GetEndLinePosition(w.sourceFile, line)
parts = append(parts, core.NewTextRange(startPos, endOfLine))
startPos = int(scanner.GetLineStarts(w.sourceFile)[line])
startPos = int(scanner.GetLineStarts(w.sourceFile)[line+1])
}

if indentFinalLine {
Expand Down Expand Up @@ -953,12 +953,24 @@ func (w *formatSpanWorker) indentMultilineComment(commentRange core.TextRange, i
if i != 0 {
nonWhitespaceCharacter, nonWhitespaceColumn = findFirstNonWhitespaceCharacterAndColumn(parts[i].Pos(), parts[i].End(), w.sourceFile, w.formattingContext.Options)
}

// Check if the first non-whitespace character is '*' (comment continuation)
// If so, we should only replace the whitespace before the '*', not the '*' itself
charactersToReplace := nonWhitespaceCharacter
if nonWhitespaceCharacter > 0 && startLinePos+nonWhitespaceCharacter < len(w.sourceFile.Text()) {
firstNonWhitespaceChar := w.sourceFile.Text()[startLinePos+nonWhitespaceCharacter]
if firstNonWhitespaceChar == '*' {
// Only replace whitespace before the '*', not the '*' itself
charactersToReplace = nonWhitespaceCharacter - 1
}
}

newIndentation := nonWhitespaceColumn + delta
if newIndentation > 0 {
indentationString := getIndentationString(newIndentation, w.formattingContext.Options)
w.recordReplace(startLinePos, nonWhitespaceCharacter, indentationString)
w.recordReplace(startLinePos, charactersToReplace, indentationString)
} else {
w.recordDelete(startLinePos, nonWhitespaceCharacter)
w.recordDelete(startLinePos, charactersToReplace)
}

startLine++
Expand Down