Fix "Token end is child end" crash when range formatting is inside a JSDoc comment#4594
Open
johnfav03 wants to merge 1 commit into
Open
Fix "Token end is child end" crash when range formatting is inside a JSDoc comment#4594johnfav03 wants to merge 1 commit into
johnfav03 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a formatter crash when range formatting starts from a position inside a JSDoc comment (notably at the start of a file), where the formatter’s scanner previously could begin scanning inside the comment and mis-tokenize backticks as an unterminated template literal.
Changes:
- Update range-format scan start calculation to use
astnav.FindPrecedingTokenEx(..., excludeJSDoc=true)so the scanner won’t start inside a JSDoc comment. - Add a new fourslash regression test that range-formats a selection starting inside a leading JSDoc containing a backtick, ensuring the operation does not crash.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/fourslash/tests/rangeFormatInsideJSDocComment_test.go | Adds a regression test covering range formatting when the selection start is inside a leading JSDoc with a backtick. |
| internal/format/span.go | Adjusts scan start token discovery to exclude JSDoc so scanning doesn’t begin mid-comment. |
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.
Fixes an issue uncovered in #4459 (comment) and #4532 (comment);
getScanStartPosition()callsastnav.FindPrecedingToken(originalRange.Pos())to get the starting position for the scanner. However, ifPos()is inside of a JSDoc, the scanner never sees the/**opener, and mis-scans the backtick as an undetermined template literal running to the end of the file, which produces a token whose end doesn't match the child's end and triggers the crash.