Skip to content

fix(core): read the text of elements in setup statements as template text, and report syntax errors in element values (#636, #638) - #657

Merged
leonidaz merged 2 commits into
mainfrom
fix/core-setup-statement-text
Sep 25, 2026
Merged

leonidaz merged 2 commits into
mainfrom
fix/core-setup-statement-text

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #636.
Closes #638.

Text in an element inside a setup statement now reads as it does everywhere else, and renders on every target what the same TSX renders. A syntax error in an element that is a value reports that error, instead of acorn-typescript's internal messages about its rollback. This builds on #641, which is on main.

The bugs

The fix

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

  • The gates apply only to code that started after the innermost element.
  • Every in-place shortening of this.context goes through parseEffects. This covers each pop(), length =, length -=, and the splice in parseElement. It uses #popContext and #truncateContext, which call parseEffects.pop/truncate as acorn-typescript's own parseMaybeAssign does.
    • Outside an attempt they're a plain pop and truncation, since parseEffects records nothing then.
    • The this.context = … swaps around directive bodies and setup statements replace the array and never shorten the one an attempt recorded, so they stay.
  • The self-closing /> rule in getTokenFromCode skips a / right after a tag start. That / is the / of a closing tag (</>).
  • A tag's missing > reports '>' expected. (TS1005), as TypeScript does, instead of Unexpected token, at the same position. expect already does this for '}' expected.. This covers a closing tag's > and a self-closing tag's > after its /.
  • The token after a self-closing tag with a space before its > (<div / >, <div /\n>) reads as code in a value.

It's narrow in that each gate still applies wherever it did, except to an element opened inside the code the gate describes. The context helpers change nothing outside a failed attempt.

No override: both causes are in TSRX. Plain acorn-typescript reports these syntax errors. One upstream cause turned up: at the top of a statement, x = </>; fails with RangeError: Invalid array length in plain acorn-typescript too. I filed it as sveltejs/acorn-typescript#134 and the TSRX override as #653. It's unchanged here (Findings).

Rendered output on the targets

The shared runtime suite from #615 and #641 (packages/tsrx/tests/shared/runtime/jsx-text-whitespace*) gets 10 cases, rendered on React, Preact, Solid, Vue, and Hono in jsdom. Each compares an element's innerHTML with what React renders for the same TSX. The elements are in setup statements of a component:

On main the fixture doesn't compile: Unexpected token (61:54), at the <b /> after 4. A shared compile test checks the same text in each compiler's output.

Formatters

I generated the 464 shapes of #641's TSRX-against-TSX matrix in 28 setup-statement places (12,992 sources), and in 6 template places for comparison (2,784). Both formatters format every source, and their output parses.

Verification

  • AST and error comparison, main (8eeec666) against this branch. Each input parses strict, with { collect: true, errors: [], comments: [], preserveParens: true }, and with { loose: true }, in 4 worker threads with a hard timeout. The inputs are 18,213: the 70 tracked .tsrx files, the input and output of all 2,040 Prettier snapshot cases (readSnapshotEntries/readFormatCase), and 14,063 distinct string literals from test files.

    • No hangs. One input changes, the same way in every mode: <style apply={theme} / at the end of the input (language-server tests) reports '>' expected. (5:24) instead of Unexpected token (5:24).
  • Mutated inputs. 30,008 inputs made from the same corpus (truncated, a span deleted, or a line break, comment, tag, brace, or non-breaking space inserted), in the three modes. No hangs, and nothing that parsed fails now. 460 inputs change:

    • 411: Unexpected token becomes '>' expected. at the same position. The sampled ones match TypeScript's TS1005 position.
    • 25: an internal message (… shortened …) becomes a syntax error. 18 of them are '>' expected. and 7 are Unexpected token, Unterminated comment, Unterminated regular expression, or an unclosed tag. None of these messages remain.
    • 8 now parse, as TypeScript parses them: an element value ending in <b /\n> (x = () => <b /\n>;).
    • 6 parse with a different AST, each now read as the same markup reads elsewhere:
      • <Bar / >;, <Bar /\n>;, and <div / >\n&& b; at the top level read the token after the tag as code, as after <Bar />. Before, ; and && b; were JSX text.
      • 3 are unclosed elements in setup statements whose following text now reads as text, as in a plain function (loose and collect recovery ranges).
    • 5 differ only in the tokenizer position a thrown error records (raisedAt).
    • 5 change message or position, each to what the same markup reports elsewhere:
      • const App = async<div />; reports Unexpected token (1:23), TypeScript's position, instead of (1:18).
      • An unclosed element or <style> at the end of a setup statement reports the unclosed tag in strict mode and '}' expected. or Unexpected token in collect and loose mode, as in a plain function or template, instead of Unterminated JSX contents.
      • Mutated markup with iv class={…}> in a fragment in a setup statement's function reports the > in text (Did you mean &gt;), as TSX does, instead of a mismatched closing tag.
    • const el = <div attr= <b>// c\n"foo">text</div>; from an earlier mutation run runs out of memory on main and here ([Bug]: tsrx-core: An element as an attribute value without braces never finishes parsing when a comment starts its text #654).
  • TSRX against TSX. 25,984 generated sources: fix(core): read the text of elements in {…} containers and switch cases like everywhere else (#612, #613) #641's 464 shapes in 56 places. Those are fix(core): read the text of elements in {…} containers and switch cases like everywhere else (#612, #613) #641's 29 plus 27 setup-statement places, and I compare the text children as fix(core): read the text of elements in {…} containers and switch cases like everywhere else (#612, #613) #641 does.

    • The 27 new places: let, return in a declared function and in the body, an argument, an array, an object, a conditional, an arrow and an arrow block, @if, @for, @switch, and @{ … } values, the setups of @if and @{ … } values, a component and a component's setup, a nested element, a container, an attribute value, and a spread argument in a setup statement; a spread argument in a template; and the setups of @if, @for, @{ … } child, @try bodies and a function in an @case.
    • main: 6,876 mismatches, 4,060 of them errors, in the 28 setup places (229 each) and the 2 spread places (232 each).
    • This branch: 464 mismatches, 0 errors, all in the 2 spread places and the same as on main. An element in a spread argument is parsed as plain JSX, so a comment between its children is text ([Bug]: tsrx-core: An element in a spread attribute's argument is parsed as plain JSX: comments and template syntax read as text #656). None in the other 54 places.
  • Tests. I ran vitest run --maxWorkers=4 for tsrx-utils, tsrx-analyze, tsrx-vite, the five compilers, the five runtime projects, prettier-plugin, and tsrx-prettier-plugin: 53 files, 9,420 passed, 77 skipped. New tests:

    All of them except two guards fail on main: the spread attribute test, and the no-progress error in a setup statement, which reports Unexpected token (2:14) on main too.

  • pnpm typecheck passes. tsrx-tsc isn't linked after pnpm install --ignore-scripts, so I built @tsrx/typescript-plugin and ran the @tsrx/vite-plugin-hono typecheck:tests step through packages/typescript-plugin/dist/tsc.js, then the steps after it.

  • pnpm format changed nothing else, and pnpm format:check and pnpm changeset:check pass. Changeset: patch for @tsrx/core.

Findings

Filed, with other causes:

For the user:

  • '>' expected. A tag's missing > now reports TypeScript's message instead of Unexpected token, at the same position. It's the bulk of the mutated-input changes (411). If you'd rather keep Unexpected token there, it's the jsxTagEnd branch in expect.

🤖 Generated with Claude Code


Note

Medium Risk
Touches core TSRX parser token/context logic used for all JSX and setup blocks; behavior changes are broad but covered by extensive tests.

Overview
Fixes template text parsing for JSX inside setup statements (@{ … }, directive bodies, @case) so elements in const, return, nested functions, control-flow values, etc. keep whitespace and adjacent text like normal template markup—instead of treating that region as plain script (lost spaces, NBSP/Hello<b /> failures).

The parser now records per-element setup depths (#elementStarts) and only blocks raw template text for code that started after that element opened; closing-tag parent detection uses the same rule.

Context stack mutations route through #popContext / #truncateContext and parseEffects, so failed speculative < parses surface real syntax errors instead of acorn-typescript rollback internals. Missing tag > reports '>' expected.; tokens after <div / > in value position tokenize as code.

Adds parser, compile, and runtime whitespace coverage for setup-statement cases.

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

leonidaz and others added 2 commits September 25, 2026 17:15
…text, and report syntax errors in element values (#636, #638)

Text in an element in a setup statement of a `@{ … }` or directive body, or
of an `@case`, was read as code: `#shouldReadTemplateRawTextToken` and the
closing tag's `inside_parent_template` checked `#templateScriptParsingDepth`
(and `#parsingJSXSwitchCaseScriptStatementDepth`) for the whole statement.
Each element now records both depths when it starts, next to `this.labels`
(`#elementStarts`), and the gates apply only to code that started after the
innermost element: a setup statement's own tokens, or a spread attribute's
argument.

acorn-typescript tries a value that starts with `<` as an element and undoes
the attempt when it fails, treating the token context stack as append-only.
TSRX shortened `this.context` directly, so a syntax error after a closing tag
surfaced as `A parse effect shortened an append-only array` or `A parse branch
shortened the token context stack below its checkpoint`. Every in-place
shortening now goes through `parseEffects` (`#popContext`,
`#truncateContext`), the self-closing `/>` rule no longer drains the stack for
the `/` of `</`, and a tag's missing `>` reports `'>' expected.` as
TypeScript does.

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

Labels

None yet

Projects

None yet

1 participant