Skip to content

fix(core): report an unclosed tag without a later error from the code after it - #1353

Merged
jonkwheeler merged 7 commits into
tsrx-org:mainfrom
jonkwheeler:w13/p-1305-unclosed-tag-tsrx1001
Nov 14, 2026
Merged

jonkwheeler merged 7 commits into
tsrx-org:mainfrom
jonkwheeler:w13/p-1305-unclosed-tag-tsrx1001

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1305. For the unclosed tags in the issue, a strict compile throws TSRX1001, but a collecting parse (what Prettier and @tsrx/eslint-parser run) and a loose one (the editor's) recorded TSRX1001 and then threw a later error that the unclosed tag caused, which the tools showed instead. Now each reports TSRX1001 and goes on.

  • A tag left open in code read the code after it as its text, so the code around it lost its ), ] or :. When the text after its last child starts with what the code around it goes on with, that text is read again as code: ) or , in parentheses, ] or , in brackets opened in the statement, : after a conditional's ?. Other text stays the element's, as before (@{ <div>: label }). A comment later in the code doesn't stop this: the comments in the code read again are kept as the code reads them, one copy of each, without those the text read where the code has a string, a template literal, a regular expression or a comment ('a // b', a;// /* b */).
  • An unclosed <style> or <script> body ran to the end of the file in a collecting parse (and in a loose one outside a template). It now ends at the next tag inside a template, as a loose parse did, or at the } that closes the block it's in (braces in strings and comments don't count), in both modes; an unclosed <style> body is read as partial CSS.
  • The editor's code no longer moves an unclosed element or fragment to a module-level constant, where it printed without the closing tag that keeps the code valid (const App__static1 = <b>;, found while testing the conditional row; it happened on main for <p>{ok && <b>}</p> too).

Results with @tsrx/react on main (8d2b839) and on this branch. Positions are line:column, 0-based for the parse and the editor, 1-based for Prettier and ESLint, as each prints them:

Source Collecting parse Editor (loose) Prettier ESLint
return (↵<div>↵); before: TSRX1001 at 3:3, then throws TS1012 at 5:0
after: TSRX1001 at 3:3
before: throws TS1012
after: TSRX1001
before: Unexpected token (5:1)
after: Unclosed tag '<div>' … (3:4)
before: 0:0 … Unexpected token (5:0)
after: 3:4 Parsing error: Unclosed tag '<div>'…
return <div>{props.x ? <b> : null}</div>; before: TSRX1001, throws TS1012 at 2:34
after: TSRX1001 at 2:25
before: throws TS1012
after: TSRX1001
after: Unclosed tag '<b>' … (2:26) after: 2:26 … Unclosed tag '<b>'…
return [<div>]; before: TSRX1001, throws TS1012 at 3:0
after: TSRX1001 at 2:10
before: throws TS1012
after: TSRX1001
after: … (2:11) after: 2:11 …
@{ <style>.a {} } before: TSRX1001, throws TSRX3013 Expected identifier
after: TSRX1001
before: throws TS1005 '}' expected at the end
after: TSRX1001, and TSRX3009 (a <style> as the block's output, also reported when it's closed)
before: Expected identifier
after: Unclosed tag '<style>' … (2:2)
after: 2:2 … Unclosed tag '<style>'…
@{ <div><style>.a { color: red; }</div> } before: TSRX1001, throws TSRX3013
after: TSRX1001
TSRX1001 (unchanged) after: … (3:3) after: 3:3 …
@{ <script>const a = 1; } before: TSRX1001 and TSRX2012
after: TSRX1001
before: throws TS1005 at the end
after: TSRX1001
TSRX1001 (unchanged) TSRX1001 (unchanged)
return (↵<div>↵);↵// later before: TSRX1001 at 3:3, then throws TS1012 at 6:0
after: TSRX1001 at 3:3, and the comment once
before: throws TS1012
after: TSRX1001
before: Unexpected token (6:1)
after: Unclosed tag '<div>' … (3:4)
before: 6:1 Parsing error: Unexpected token
after: 3:4 Parsing error: Unclosed tag '<div>'…
return [<div>];↵/* later */ before: TSRX1001, throws TS1012 at 4:0
after: TSRX1001 at 2:10, and the comment once
before: throws TS1012
after: TSRX1001
after: … (2:11) after: 2:11 …
return (↵<div>↵<b>x</b>↵);↵// later before: TSRX1001, throws TS1012 at 7:0
after: TSRX1001 at 3:3, and the comment once
before: throws TS1012
after: TSRX1001
after: … (3:4) after: 3:4 …
return [<div>, 'a // b']; before: TSRX1001, throws TS1012 at 3:0
after: TSRX1001 at 2:10, and no comment
before: throws TS1012
after: TSRX1001
after: … (2:11) after: 2:11 …

ESLint's positions come out right here because the error is now a collected one; a thrown error's 0:0 is #1308. The last four rows' before is main at a0f7749 (where #1308 is fixed) and this branch at cb1dcf9, which threw the same errors: a comment, or // or /* anywhere, from the code to the end of the template kept the code the element's text.

Not covered, and as on main: an unclosed tag in an @for header or in a dynamic tag's expression, which another element parser reads (@for (const x of [<b>]), <{c ? <b> : 'div'} />); a line comment between ? and the tag; a function parameter's default (function f(x = <b>) {}, whose children read the body's {}); an object literal's comma ({ a: <b>, c }, where , c stays text); a regular expression with a } in an unclosed <script> body; and, after a child of the element, a string or regular expression with /* and then a ), ] or , in it, as the last text ([<div><b />, 'a /* b */ ] c']), where the code is read again from that closer. An unclosed <> inside {…} now reaches the printer's TypeError that #1304 is about ({c ? <> : null} threw TS1012 before).

Tests

  • packages/tsrx/tests/shared/compile.js, for each of the five targets: the editor's compile reports TSRX1001 where a collecting parse records it, and nothing else, with valid TSX, for the issue's six shapes plus [<div>, 1], @{ <div>: label }, @{ <div>, hello }, and <style>/<script> bodies with a } in a string or comment.
  • The same tests, with the comments a collecting parse records (once each, and none where the code has none), for a tag left open with comments in the code after it: a line or block comment later in the function, after a child, in a call, in a speculative parse (async <T,>(a = [<div>, 1]) => a; // later), after a division, a line comment that touches the code, and // or /* in a string, a template literal or a regular expression.
  • packages/prettier-plugin/tests/tsrx.test.js and packages/eslint-parser/tests/index.test.ts: the issue's shapes report TSRX1001 at the tag, and so do two with a comment later in the function.
  • packages/tsrx/tests/utils/closing-brace.test.js: a collecting parse now recovers an unclosed <style> body like a loose one, so it stops at the missing } at the end, not at : red.
  • Mutation check: with the plugin.js and jsx-hoist.js changes reverted and the tests kept, 93 tests fail: 15 of the 18 shared cases on each of the six harnesses that run them (React, Preact, Solid, Vue, Hono and @tsrx/hono/dom), the closing-brace test, and the Prettier and ESLint tests. The 18 that pass are the three cases that guard text that must stay text (@{ <div>: label }, @{ <div>, hello }, [@if (c) { <div>, label }]), which pass on main too.
  • Mutation check for 7757322 (comments after the tag): with its plugin.js, parse/index.js and parse.d.ts changes reverted and the tests kept, 92 tests fail: 15 of its 17 shared cases on each of the six harnesses, and the Prettier and ESLint tests. The two that pass pin what this branch did before, a ) in the element's text before the real one ((<div><b />) y /* d */ );). Reverting each part alone, on the React harness: keeping every comment the text and the code read fails 14 shared cases, keeping those in strings, template literals and regular expressions 5, keeping a comment inside another 1, and not looking where the text after the last child starts 3.
  • Full Vitest run (226 files, 14,933 passed), format:check, typecheck and changeset:check pass locally on 7757322.
  • Codex (gpt-5.6-sol, read-only) reviewed every commit, in five rounds. 4c7a52e: braces in strings counted, @{ <div>: label } read as code, unclosed fragments hoisted, and the parsers noted above as not covered. dfe6a31: setup statements in @{ … } (f(<div>, x)), template literals, an outer [ leaking into directive bodies, a comment before ?, an escaped CRLF and nested templates in the scanner (plus object commas and function parameter defaults, which behave as on main). 83ab11b: errors of dropped children reported twice, recursion depth. 33129e9: shortening the errors outside the undo log. d100faa: no findings. Each finding is fixed in the next commit with a test, except those noted as not covered or as on main. 7757322, for Cursor Bugbot's finding on cb1dcf9, took five rounds before the commit. Round 1: a first version that still refused where // or /* sat in a string or in what might be a regular expression misjudged division ("x" / 2, x++ / 2) and regular expressions (after default), and refused for strings in JSX attributes and ${…}, so it was replaced by dropping, after the parse, the comments the text read inside a literal of the code. Rounds 2 to 4: each order of trying the text pieces picked a ) or ] that isn't the code's in one source ((<div>x /* c */ ) y /* d */ );, [<div><b />, 'a /* b */ ] c'];, (<div><b />) y /* d */ );); now this branch's earlier candidates come first, then where the text after the last child starts, and the case left is listed above as not covered. Round 5: no findings.

🤖 Generated with Claude Code

jonkwheeler and others added 5 commits November 13, 2026 22:14
… after it

A collecting or loose parse recorded TSRX1001 for a tag left open, then
threw a later error the unclosed tag caused, which Prettier, ESLint and
the editor showed instead:

- A tag left open in code read the code after it as its text
  (`return (<div>);`, `c ? <b> : null`, `[<div>]`). Text that starts
  with `)`, `]`, `:`, `,` or `;` is read again as code.
- An unclosed `<style>` or `<script>` body ran to the end of the file in
  a collecting parse. Its body now ends at the next tag inside a
  template or at the `}` that closes its block, in a collecting parse as
  in a loose one, and an unclosed `<style>` body is read as partial CSS.

An unclosed element is no longer moved to a static, where it lost the
closing tag the editor's code adds.

Fixes tsrx-org#1305

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es on

Codex review of the first commit:

- Text after an unclosed tag that starts with `:` or `,` is code only
  where the code around the tag goes on with it: `)` or `,` in
  parentheses, `]` or `,` in brackets opened in the statement, `:` after
  a conditional's `?`. `@{ <div>: label }` keeps its text again.
- The `}` that ends an unclosed `<style>` or `<script>` body skips braces
  in strings and comments, and in a script's template literals.
- An unclosed fragment isn't hoisted either.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex's second review:

- The code can go on right after the opening tag (`f(<div>, x)` in a
  `@{ … }` setup statement), where the children read the code; that is
  tried before the text after the last child.
- `]` or `,` goes on an element right after `[` or `,`, not any element
  while a `[` is open, which made text in a directive body inside an
  array read as code (`[@if (c) { <div>, label }]`).
- `}` goes on an element in a template literal's `${…}`, and `:` after a
  `?` with a block comment between.
- An element in a `@{ … }` body's setup code is in code.
- The `}` scan skips an escaped CRLF in a string and a template literal's
  `${…}`, nested templates included.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed templates iteratively

Codex's third review:

- When the code after an unclosed tag starts right after it, the
  children read in between are dropped and read again as code, so the
  errors they recorded (`f(<div>, <b>)`: TSRX1001 for `<b>`) are dropped
  too instead of being reported twice.
- The `}` scan follows nested template literals with a stack instead of
  recursion, which deep nesting could run out of.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex's fourth review: shortening the collected errors outside the undo
log made a speculative parse around the element throw when it rolled
back (`async <T>(a = [<div>, <style>.a {}]) => a`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tsrx-org#1344 on main parses a <style> body strictly and, on a CSS syntax
error, throws it or records it and reparses loosely. This branch parsed
an unclosed <style>'s partial body loosely. Keeps tsrx-org#1344's handling and
treats an unclosed <style> like a loose parse there: it gets its TSRX1001
alone, in every mode. Keeps both sides' new tests in the ESLint parser
and Prettier plugin test files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cb1dcf9. Configure here.

Comment thread packages/tsrx/src/plugin.js Resolved
…ment

Cursor Bugbot on cb1dcf9: the code after a tag left open was read
again only when no `//` or `/*` appeared from it to the end of the
template, often the rest of the function, so a later comment, or
`http://` in a string, kept it the element's text, and the parse still
threw the later error instead of reporting TSRX1001 alone.

Comments no longer stop it. The plugin records each span it reads
again (`textReadAsCode`), and the parser then keeps the comments there
as the code reads them: one copy of each, and none that the text read
where the code has a string, a template literal or a regular expression
(`'a // b'`), or inside a comment (`/* b */` in `a;// /* b */`, where the
text reads the `//` as text).

A comment also splits the text after the element's last child into
pieces, so where its last piece doesn't go on with the code, the code
is looked for where that text starts, past whitespace and comments
(`<div><b>x</b> );` and a comment after 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 14, 2026 16:10
@jonkwheeler
jonkwheeler merged commit 10059ac into tsrx-org:main Nov 14, 2026
10 checks passed
jonkwheeler added a commit that referenced this pull request Nov 18, 2026
…1369)

* fix(core): read the token before an unclosed tag past line comments

The recovery for a tag left open in code (#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
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