Repository navigation
fix(core): read comments in an element attribute value as text, report a closing tag where an element starts, and read a / in an opening tag as code (#653, #654, #655) - #695
Merged
Conversation
…t a closing tag where an element starts, and read a `/` in an opening tag as code (#653, #654, #655) An element as an attribute value without braces (`<div attr=<b>/* c */</b> />`) is read by acorn-typescript's JSX parser, which takes each text token as a child. Its text was read as template text, which ends at a comment, so the children loop read the same empty token until memory ran out. In such an element (`#insideScriptJSXElement`) a comment is now text, as in TSX, and `jsx_parseText` reports `Unexpected token` for a text token that reads nothing. A closing tag where an expression starts (`x = </>;`) failed with `RangeError: Invalid array length`: after the failed element attempt, acorn-typescript's generic arrow attempt read the `/` again and `updateContext` dropped the tag start's contexts a second time. `updateContext` now treats the `/` as a closing tag's only while the tag start's `tc_oTag` is on top (UPSTREAM(sveltejs/acorn-typescript#134)), and a closing tag where an element or statement starts is reported at its `<`, where TypeScript reports it. In a template, the rule that reads a `/` or `#` as text fired in an opening tag, whose element isn't on `#path` yet: `<div / >` wasn't self-closing, and a division, regular expression, or private name in a spread attribute's argument failed. The rule now skips an opening tag being read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Sep 25, 2026
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.
Closes #653.
Closes #654.
Closes #655.
Three element shapes that crashed, ran out of memory, or failed now parse, or report a syntax error where TypeScript reports one. They follow on from #657.
The bugs
<div attr=<b>/* c */</b> />) ran out of memory.jsx_parseElementhands it tosuperwhile the host's opening tag is read). Its children loop takes eachjsxTexttoken as a child and reads the next one.readToken, which read them as template text: the host element is the current template node. Template text ends at a comment, and the children context (tc_expr) preserves space, so nothing skipped the comment. At a/* … */anywhere in the text, or a//where a text run starts, the token was empty and never moved. The loop read it forever.attr=<>/* c */</>), in an element nested in one (attr=<b>a<i>/* c */</i></b>), and in every place I tried: a template, a setup statement, a function, a top-level value. Text, nested elements,{…}containers, and@if(read as text and a container there) parsed. So did a//at the start of a line, read by acorn-typescript's reader as text.RangeError: Invalid array length#653, aRangeError.x = </>;failed withRangeError: Invalid array length. The cause is upstream (A closing tag where an expression starts fails withRangeError: Invalid array lengthwithjsxsveltejs/acorn-typescript#134): after the failed element attempt,parseMaybeAssignpops the tag start's two contexts and tries a generic arrow from the same<. That attempt reads the/again, andupdateContextdrops two contexts again, below the stack's start at the top of a statement./in the opening tag of an element in a template is read as text:<div / >and division in a spread attribute fail #655, a/read as text. In a template, the rule ingetTokenFromCodethat reads a/or#as text fired in an opening tag, whose element isn't on#pathuntil its>. So<div / >and<div /\n>weren't self-closing (a mismatched closing tag, or an attribute named/in collect and loose mode), and a division, regular expression, or private name in a spread attribute's argument failed (<div {...{ a: b / 2 }} />,<div {...this.#p} />). The same held in an element in a plain function or a setup statement, inside an enclosing element. fix(core): read the text of elements in setup statements as template text, and report syntax errors in element values (#636, #638) #657 fixed<div / >where the element is a value.The fix
All in
packages/tsrx/src/plugin.js:#insideScriptJSXElementtells such a position:#scriptJSXElementDepthis above its value when the innermost TSRX element on#pathstarted. Each element start records that depth next to the others (#elementStarts), the technique of fix(core): read the text of elements in {…} containers and switch cases like everywhere else (#612, #613) #641 and fix(core): read the text of elements in setup statements as template text, and report syntax errors in element values (#636, #638) #657.#readTemplateRawTextTokenpasses it to#templateRawTextEnd, which then doesn't stop at a comment. Everything else about that text reads as before: raw values,<+ space as text,@ifand{…}.jsx_parseTextraisesUnexpected tokenwhen a text token reads nothing and the next token is the same one, asparseTemplateBodydoes since fix(core): read the text of elements in {…} containers and switch cases like everywhere else (#612, #613) #641. With the fix I found no input that reaches it. Without the fix it turns each runaway parse above intoUnexpected tokenat the comment.RangeError: Invalid array length#653.updateContexttreats a/right after a tag start as a closing tag's only while the tag start'stc_oTagis on top, as the upstream issue's fix direction says. Otherwise the/updates the context as acorn's ownupdateContextdoes. Only a failed attempt can take the contexts away, so a parse that succeeds reads the same.Unexpected tokenat the/. TypeScript reportsExpression expected.(TS1109) at the<, so a closing tag where an element starts now reportsUnexpected tokenat its<: injsx_parseElement, and inparseStatementwhere a statement starts (TypeScript's TS1128 is at the<there too). This moves the position ofx = (</>);,f(</>);,yield </>, and</div>by one, to TypeScript's./in the opening tag of an element in a template is read as text:<div / >and division in a spread attribute fail #655. The text rule ingetTokenFromCodeskips an opening tag being read:#openingNativeTemplateNodeis set and isn't the innermost node on#path. The element goes on#pathonly for the token after its>, its first child, which still reads/pathand#tagas text.#openingNativeTemplateNodearound a spread argument, which is [Bug]: tsrx-core: An element in a spread attribute's argument is parsed as plain JSX: comments and template syntax read as text #656's open question, so an element in the argument is still read by acorn-typescript's JSX parser, and its comments and@ifstay text. Its text is read by that parser, never by this rule. [Bug]: tsrx-core: An element in a spread attribute's argument is parsed as plain JSX: comments and template syntax read as text #656's reproduction prints the same children here as onmain. What changes is the/and#of the argument's own code, which failed to parse, and the tag of an element there ({...{ k: <i / > }}).Override
updateContext, markedUPSTREAM(sveltejs/acorn-typescript#134). It mirrors the issue's fix direction: the/falls through to acorn'supdateContextinstead of dropping contexts, and the AST of every input that parses is unchanged. The<position is TSRX's own and stays when the override goes.Verification
Each comparison parses with
main's parser (927aa91a) and this branch's, strict, with{ collect: true, errors: [], comments: [], preserveParens: true }, and with{ loose: true }, in 4 child processes with a 512 MB heap and a kill after 15 s without progress.Corpus, 17,954 inputs: the 70 tracked
.tsrxfiles, the input and output of all 2,040 Prettier snapshot cases (readSnapshotEntries/readFormatCase), and 14,779 distinct string literals from test files. 66 change, 33 of them this PR's new tests. No hangs on this branch.Unexpected tokenmoves from a closing tag's/to its<, TypeScript's position (</div>,x = import(</>);,x = -(< />);,yield </>).export function App() @{\n\t<>\n\t\t<div /at the end of the input reports'>' expected.instead ofUnexpected token, at the same position, as fix(core): read the text of elements in setup statements as template text, and report syntax errors in element values (#636, #638) #657 does for a self-closing tag without its>. The/is no longer text.RangeError: Invalid array lengthbecomesUnexpected tokenat the<([Bug]: tsrx-core: A closing tag where a statement's expression starts fails withRangeError: Invalid array length#653's inputs).mainand parse here, or report a closing tag mismatch ([Bug]: tsrx-core: An element as an attribute value without braces never finishes parsing when a comment starts its text #654's inputs).const el = <div attr=<b>\n// c\n</b> />;has one text child"\n// c\n"instead of"\n"and"// c\n". The text is the same.mainand parse here ([Bug]: tsrx-core: A/in the opening tag of an element in a template is read as text:<div / >and division in a spread attribute fail #655's inputs), 6 of them recovered in collect and loose mode with a mismatched closing tag and an attribute named/.Mutated inputs, 21,951 made from the corpus inputs with a
<(truncated, a span deleted, or a/,/, comment, closing tag,=<b>…</b>, spread argument, brace, or tag inserted, mostly next to a tag). 142 change. None that parsed fails, and nothing parses with a different AST.Unexpected tokenfrom a closing tag's/to its<.'>' expected.instead ofUnexpected token: truncated self-closing tags whose/was text.RangeErrors that becomeUnexpected token.main([Bug]: tsrx-core: An element as an attribute value without braces never finishes parsing when a comment starts its text #654 shapes, most of them truncated). None hangs here: 3 parse, and 19 report a syntax error./in the opening tag of an element in a template is read as text:<div / >and division in a spread attribute fail #655 inputs, whose first error was the/read as text.Generated shapes, 2,700 sources: 100 shapes in 9 places in three modes. The shapes are elements as unbraced attribute values with 20 kinds of children, and 20 opening-tag shapes with
/and spread arguments. The places are a top-level value, a function'sreturn, a braced attribute value, a template child and root, a setup statement, an element in a function, an@ifbody, and a container.main: 588 run out of memory. This branch: none.mainand parse here, and 109 parse with a different AST: the\n// c\nshapes (one text child), and<div / >shapes that collect and loose mode recovered as an element with an attribute named/.mainand fail here:<div / >x</div>in a template and in a function's element.mainread<div / >as an element with an attribute named/, closed by</div>. It's invalid TSX (TypeScript reports TS17002 at the</div>), and it now reports the mismatched closing tag.return), TypeScript's parser and this branch agree on all 200 about whether each parses, and the comment texts match TypeScript'sJSXText.Tests.
vitest run --maxWorkers=4fortsrx-utils,tsrx-analyze,prettier-plugin,tsrx-prettier-plugin,tsrx-react,tsrx-preact,tsrx-solid,tsrx-vue,tsrx-hono, andtsrx-vite: 41 files, 9,211 passed, 77 skipped. The Prettier known-failures list is unchanged. New tests, each throughparse_in_worker/parse_in_worker_with_astin strict, collect, and loose mode:/in the opening tag of an element in a template is read as text:<div / >and division in a spread attribute fail #655: 7 self-closing tags with a space, line break, or comment before>, each compared with the same tag without it, 5 spread arguments (division in 3 places, a regular expression, a private name), and a guard that a child's/and#stay text;RangeError: Invalid array length#653: the upstream test for 9 inputs at TypeScript's position, and a guard for generic arrows and elements. The 4 closing-tag cases from fix(core): read the text of elements in setup statements as template text, and report syntax errors in element values (#636, #638) #657 move to the<, and 2 join them (x = (</>);,</div>where a statement starts).All fail on
mainexcept the guards.pnpm typecheckpasses.tsrx-tscisn't linked afterpnpm install --ignore-scripts, so I built@tsrx/typescript-plugin, ran the@tsrx/vite-plugin-honotypecheck:testsstep throughpackages/typescript-plugin/dist/tsc.js, then the steps after it.pnpm formatchanged nothing else, andpnpm format:checkandpnpm changeset:checkpass. Changeset:patchfor@tsrx/core.Findings
Filed, with other causes:
{x}becomes an expression #693: character references in an element that acorn-typescript's JSX parser reads are decoded, and the output prints them bare. In a spread argument, or an unbraced attribute value in a container,{x}compiles to the expression{x}. [Bug]: tsrx-core: An element in a spread attribute's argument is parsed as plain JSX: comments and template syntax read as text #656 may change which elements this covers.>in template text compiles to output JSX rejects, and in a container drops the text before it #694: a>in template text compiles to a bare>that esbuild rejects, and in a container the text before it is dropped when it follows a tag.Same cause as here:
@ifis text,{…}a plain container), like an element in a spread argument. If [Bug]: tsrx-core: An element in a spread attribute's argument is parsed as plain JSX: comments and template syntax read as text #656 decides those elements should parse as TSRX elements, this one should follow, and its comments become comments.🤖 Generated with Claude Code
Note
Medium Risk
Changes core tokenizer and JSX parsing paths with an upstream workaround; behavior shifts for invalid inputs and some edge-case AST text splitting, but aimed at matching TypeScript error positions and fixing crashes/OOM.
Overview
Fixes three TSRX parser failure modes around JSX-like element syntax in
packages/tsrx/src/plugin.js, with a patch changeset and expanded parser tests.Unbraced attribute values (#654): When acorn-typescript parses a nested element (
<div attr=<b>…</b> />), comments in that inner element are treated as JSX text (TSX semantics) instead of ending template raw-text scans—avoiding infinite empty-token loops and OOM. Ajsx_parseTextguard raisesUnexpected tokenif a zero-length text token would repeat. Element starts now trackscriptJSXDepthfor#insideScriptJSXElement.Closing tags where expressions start (#653):
updateContextworks around sveltejs/acorn-typescript#134 so/after a failed tag start does not double-pop the context stack (RangeError→ proper error). Closing tags likex = </>;now error at<(TypeScript-aligned), via early checks injsx_parseElementandparseStatement.Opening tags in templates (#655):
/and#in an opening tag (self-close with space before>, spread args like{...b/2}orthis.#p) are tokenized as JavaScript, not template text, by skipping the text-literal rule while#openingNativeTemplateNodeis active.Reviewed by Cursor Bugbot for commit 598dbfa. Bugbot is set up for automated code reviews on this repo. Configure here.