Skip to content

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
leonidaz merged 1 commit into
mainfrom
fix/core-element-value-crashes
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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

The fix

All in packages/tsrx/src/plugin.js:

Override

  • updateContext, marked UPSTREAM(sveltejs/acorn-typescript#134). It mirrors the issue's fix direction: the / falls through to acorn's updateContext instead 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.

Findings

Filed, with other causes:

Same cause as here:

🤖 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. A jsx_parseText guard raises Unexpected token if a zero-length text token would repeat. Element starts now track scriptJSXDepth for #insideScriptJSXElement.

Closing tags where expressions start (#653): updateContext works around sveltejs/acorn-typescript#134 so / after a failed tag start does not double-pop the context stack (RangeError → proper error). Closing tags like x = </>; now error at < (TypeScript-aligned), via early checks in jsx_parseElement and parseStatement.

Opening tags in templates (#655): / and # in an opening tag (self-close with space before >, spread args like {...b/2} or this.#p) are tokenized as JavaScript, not template text, by skipping the text-literal rule while #openingNativeTemplateNode is active.

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

…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment