Skip to content

Fix "Token end is child end" crash when range formatting is inside a JSDoc comment#4594

Open
johnfav03 wants to merge 1 commit into
microsoft:mainfrom
johnfav03:repro/f2-format-range
Open

Fix "Token end is child end" crash when range formatting is inside a JSDoc comment#4594
johnfav03 wants to merge 1 commit into
microsoft:mainfrom
johnfav03:repro/f2-format-range

Conversation

@johnfav03

Copy link
Copy Markdown
Contributor

Fixes an issue uncovered in #4459 (comment) and #4532 (comment); getScanStartPosition() calls astnav.FindPrecedingToken(originalRange.Pos()) to get the starting position for the scanner. However, if Pos() 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.

Copilot AI review requested due to automatic review settings July 10, 2026 20:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@johnfav03 johnfav03 requested a review from iisaduan July 10, 2026 20:39
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.

2 participants