Skip to content

fix(core): read the token before an unclosed tag past line comments - #1369

Merged
jonkwheeler merged 1 commit into
tsrx-org:mainfrom
jonkwheeler:w14/1366-line-comment-unclosed-tag
Nov 18, 2026
Merged

jonkwheeler merged 1 commit into
tsrx-org:mainfrom
jonkwheeler:w14/1366-line-comment-unclosed-tag

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Since #1353, a tag left open in code is reported as TSRX1001. But with a line comment between a conditional's ? and the tag, the collecting parse and the editor compile threw TS1012 Unexpected token, so Prettier and ESLint showed that instead:

function App(props: { x: boolean }) {
	return <div>{props.x ? // c
		<b> : null}</div>;
}
Path main This PR
compile (strict) TSRX1001 TSRX1001
parseModule(…, { collect: true }) throws TS1012 Unexpected token (3:12) TSRX1001 at <b>
compile_to_volar_mappings(…, { loose: true }) throws TS1012 TSRX1001
Prettier Unexpected token (3:13) Unclosed tag '<b>'
ESLint 3:13 Unexpected token Unclosed tag '<b>'

What was wrong. To find what the code goes on with after the tag, #rereadTrailingCodeOfUnclosedElement scanned back from the < for the token before it, skipping whitespace and block comments only.

  • A line comment hid the ?, so the text after the tag stayed the tag's text. A line comment after [, or after , in an array, did the same.
  • The scan also read comment text as code. A // why? before <div>: label made the text read as code.
  • It misread /* a /* b */, because lastIndexOf('/*') finds the inner /*.

The fix. An element that starts in an expression takes the end of the token before its < from the tokenizer (lastTokEnd in jsx_parseElement), which already read the comments between as comments. That position is used only when nothing but whitespace and comments, as the tokenizer skips them (regex_space_and_comments), lies between that token and the <. A no-break space, form feed or byte order mark there still counts as whitespace, as it did before.

Not covered

The forward side of the same recovery has a matching gap, and it's the same on main. goes_on_at (#1353) finds where the code goes on after the tag with skip_whitespace_from, which skips only spaces, tabs and line breaks. So a no-break space or form feed right after the tag (const a = [<b> ];) still throws TS1012 in the collecting parse. That's a one-line follow-up.

Tests

  • UNCLOSED_TAG_RECOVERY (packages/tsrx/tests/shared/compile.js, all five targets), next to the block-comment case:
    • a line comment after ?;
    • comments with CRLF line endings;
    • /* a /* b */;
    • a line comment after [, and after , in an array;
    • a line comment followed by a no-break space in the next line's indent.
  • Two cases check that a comment ending in ? doesn't make the text after the tag read as code.
  • Four whitespace cases (a no-break space, form feed or byte order mark after ? or [) guard the whitespace check. They already pass on main.
  • The Prettier plugin and ESLint parser [Bug]: tsrx-core: for some unclosed tags, the editor, Prettier or ESLint show a later error (Unexpected token, Expected identifier, '}' expected) instead of TSRX1001 #1305 tests get the line-comment case.
  • Mutation: with main's plugin.js, 50 of these tests fail (8 core cases on 6 projects, plus the Prettier and ESLint tests).
  • npx -y [email protected] test: 15,098 passed, 102 skipped. typecheck, format:check and changeset:check pass.
  • Opus and Codex (gpt-6.1-sol) reviewed it independently. Both found that the first version's check skipped only spaces, tabs and line breaks, so a no-break space before the < threw. That's fixed above, with tests.
  • The Opus review also compared 23,328 generated inputs between main and the first version. Other than that whitespace case, every difference was main throwing where the fix reports TSRX1001. Its probes rerun on the final version show no new differences.

Fixes #1366

🤖 Generated with Claude Code

https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46


Note

Medium Risk
Touches expression-position JSX parsing and error recovery in the core parser; behavior change is localized to unclosed-tag paths but affects all tooling that uses collecting/loose parse.

Overview
Unclosed-tag recovery (TSRX1001) no longer scans backward from < to guess the preceding token. When a tag starts in an expression, the parser now remembers the tokenizer’s lastTokEnd before that < and, only if nothing but whitespace and comments lies between, uses that to tell whether code continues with ?, [, ,, etc.

Collecting/loose parses, Prettier, and ESLint therefore report Unclosed tag instead of Unexpected token for patterns like props.x ? // c then <b> : null, and comment text ending in ? no longer fools recovery into treating markup text as code (// why? before <div>: label).

Tests extend unclosed-tag recovery (line/block comments, CRLF, nested /*), plus the existing #1305 ESLint/Prettier cases.

Reviewed by Cursor Bugbot for commit 21d3f71. Bugbot is set up for automated code reviews on this repo. Configure here.

The recovery for a tag left open in code (tsrx-org#1353) looked back from its `<`
for the token before it, past whitespace and block comments only. A line
comment between a conditional's `?` and the tag (`c ? // note`, a line
break, then `<b> : null`) hid the `?`, so the text after the tag stayed
the tag's text, and a collecting or loose parse threw TS1012
`Unexpected token` at the `:`, which Prettier, ESLint and the editor
showed instead of TSRX1001. A line comment after `[`, or after `,` in an
array, did the same.

An element that starts in an expression now takes the end of the token
before its `<` from the tokenizer, which read the comments between as
comments. It is used where only whitespace and comments, as the
tokenizer skips them (`regex_space_and_comments`), lie between that
token and the `<`: a no-break space, a form feed or a byte order mark
there counts as whitespace, as it did before. Comment text is no longer
taken for that token either: a `// why?` before `<div>: label` made the
text read as code, and a block comment with `/*` in it (`/* a /* b */`)
hid the `?` before it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@jonkwheeler
jonkwheeler marked this pull request as ready for review November 18, 2026 17:36
@jonkwheeler
jonkwheeler merged commit 4500db9 into tsrx-org:main Nov 18, 2026
10 checks passed
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Nov 18, 2026
tsrx-org#1367 is now on main as bd00fda, and tsrx-org#1369 as 4500db9. This merge takes
main in with tsrx-org#1367's commit as the base, so the tree is main plus this
branch's own commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@github-actions github-actions Bot mentioned this pull request Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant