fix: handle U+2028/U+2029 line terminators in multi-line comment emit#4183
Closed
edgar-montano wants to merge 3 commits into
Closed
fix: handle U+2028/U+2029 line terminators in multi-line comment emit#4183edgar-montano wants to merge 3 commits into
edgar-montano wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
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
lineTerminatorStartBeforehelper to find the start of the line terminator precedingnextLineStart. - 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 { |
Member
|
I think I already have this covered in #4181 |
Author
Thanks, will be closing this didn't see this one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
addresses the following 4119:
Steps to reproduce
Create
main.ts:tsgo --pretty false main.ts main.tsBehavior with
typescript@6.0Behavior with
tsgo