Skip to content

fix: handle U+2028/U+2029 line terminators in multi-line comment emit#4183

Closed
edgar-montano wants to merge 3 commits into
microsoft:mainfrom
edgar-montano:edgar-montano/4119/handle-line-terminators
Closed

fix: handle U+2028/U+2029 line terminators in multi-line comment emit#4183
edgar-montano wants to merge 3 commits into
microsoft:mainfrom
edgar-montano:edgar-montano/4119/handle-line-terminators

Conversation

@edgar-montano
Copy link
Copy Markdown

@edgar-montano edgar-montano commented Jun 2, 2026

addresses the following 4119:

Steps to reproduce

Create main.ts:

printf '/* a\342\200\250b */ const x = 1;\n' > main.ts
tsgo --pretty false main.ts main.ts

Behavior with typescript@6.0

"use strict";
/* a
b */ const x = 1;

Behavior with tsgo

"use strict";
/* a
b */ const x = 1;

Copilot AI review requested due to automatic review settings June 2, 2026 20:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR improves comment printing to correctly exclude multi-byte and non-ASCII line terminators (e.g., U+2028/U+2029 and CRLF) when slicing comment text.

Changes:

  • Add lineTerminatorStartBefore helper to find the start of the line terminator preceding nextLineStart.
  • Update comment slicing logic in the printer to use the helper instead of nextLineStart-1.
  • Add targeted test cases for U+2028/U+2029 behavior and unit tests for the helper.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
internal/printer/utilities.go Adds lineTerminatorStartBefore to correctly handle CRLF and Unicode line separators.
internal/printer/printer.go Uses lineTerminatorStartBefore when computing end for comment line slicing.
internal/printer/utilities_test.go Adds unit coverage for lineTerminatorStartBefore.
internal/printer/printer_test.go Adds end-to-end printer tests for U+2028/U+2029 inside multi-line comments.

Comment on lines +108 to +114
for i, rec := range data {
t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
t.Parallel()
actual := lineTerminatorStartBefore(rec.text, rec.nextLineStart)
assert.Equal(t, rec.expected, actual)
})
}
Comment on lines +884 to +897
func lineTerminatorStartBefore(text string, nextLineStart int) int {
if nextLineStart <= 0 || nextLineStart > len(text) {
return nextLineStart
}
r, size := utf8.DecodeLastRuneInString(text[:nextLineStart])
if !stringutil.IsLineBreak(r) {
return nextLineStart
}
// Handle \r\n: the '\n' is preceded by a '\r'.
if r == '\n' && nextLineStart-size > 0 && text[nextLineStart-size-1] == '\r' {
return nextLineStart - size - 1
}
return nextLineStart - size
}
return currentLineIndent
}

func lineTerminatorStartBefore(text string, nextLineStart int) int {
@jakebailey
Copy link
Copy Markdown
Member

I think I already have this covered in #4181

@edgar-montano
Copy link
Copy Markdown
Author

I think I already have this covered in #4181

Thanks, will be closing this didn't see this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants