Skip to content

fix(core): read the text of elements in {…} containers and switch cases like everywhere else (#612, #613) - #641

Merged
leonidaz merged 3 commits into
mainfrom
fix/core-template-text-containers
Sep 25, 2026
Merged

leonidaz merged 3 commits into
mainfrom
fix/core-template-text-containers

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #612.
Closes #613.

Template text in an element inside a {…} container or a switch case now follows the JSX whitespace rules that #615 applied everywhere else, and renders on every target what the same TSX renders.

The bugs

The fix

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

  • #expressionContainerPathBaselines records the #path array with its length. #containerPathBaseline() gives that length while #path is the same array, and 0 once a directive body or setup statement in the container has replaced it, since every node on the new path opened inside the container.
  • The closing tag's inside_parent_template counts only an ancestor opened inside the innermost container (this.#path.slice(this.#containerPathBaseline(), -1)) instead of requiring no container. Above that ancestor the container's expression goes on ({x ? <div>…</div> : <p />}), so the token after the container's own element stays code, as before. #shouldReadTemplateRawTextToken uses the same baseline.
  • parseElement records this.labels and its length when each element starts (#elementLabels). The switch gate now applies only when a switch began after the innermost element (#insideSwitchStartedAfter), that is, between the cases of a switch, where the case tests and @case keywords are code. A function body starts a new this.labels, so a switch in it counts as started after any element outside it.
  • parseTemplateBody raises Unexpected token when a text read consumes nothing and leaves the same token, instead of reading it until the stack runs out, as [Bug]: tsrx-core: Text in an element in an @switch case is read as code: leading spaces are lost, and text before a tag overflows the stack #613's fix direction suggests. After the two changes above, nothing in this PR's scope reaches it.

It's narrow in that each gate still applies wherever it did, except to an element opened inside the construct the gate describes. The comparison below changes one existing input. No upstream cause and no override: plain acorn-typescript has no templates.

The closing tag still requires #templateScriptParsingDepth === 0, and #shouldReadTemplateRawTextToken still returns false while it is above 0. So text in an element in a setup statement (const a = <span><b>1</b> 2</span>; in a @{ … } body) is still read as code. That's a separate gate, filed as #636 (Findings).

Rendered output on the targets

The shared runtime suite from #615 (packages/tsrx/tests/shared/runtime/jsx-text-whitespace*) gets 11 cases, so it renders 20 on React, Preact, Solid, Vue, and Hono in jsdom. Each compares an element's innerHTML with what the same TSX renders:

On main the fixture doesn't compile: Not enough stack space to parse input at the @case's 4<b />.

The container cases select the element inside the container, not the container's parent, because the targets render the container's own slot differently: Vue adds a text anchor and <!--if-->. The suite leaves out an inline non-breaking space between children, as #615 did, since Solid's compiler writes it as a plain space for TSX too.

Formatters

Both formatters format every generated container and @switch source below (8,816), and their output parses. On main most of these sources failed to parse, so neither formatter could format them.

Verification

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

  • Mutated inputs. 28,852 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 the same inputs parse. 7 change, all intended:

    • 2 broken inputs whose < of a type parameter list sits where an element's text starts (const f1 = <b><T,>() => 1;) threw Not enough stack space to parse input. They now throw Unexpected token (1:14) from the no-progress check.
    • A non-breaking space after a closing tag in an element in an unclosed { now reads as text. That input fails with '}' expected. for the missing } instead of Unexpected character ([Bug]: tsrx-core: Text after a closing tag in an element inside {…} loses its leading space, and a non-breaking space there fails to parse #612).
    • The mutated copy of the " none" source above.
    • 3 in @switch cases now read as the same markup reads in an @if body (checked against it):
      • a line comment right after an @case element's opening tag now leads the {'Away'} after it;
      • < followed by a tab is text (<em>{'rest'}<\t…, unclosed);
      • in collect and loose mode, an unclosed <b> in an attribute value of an element in an @case no longer gets the a after it as a child.
  • TSRX against TSX. 13,456 generated sources: 464 shapes in 29 places. Each shape combines one of 6 kinds of previous child, 16 kinds of whitespace, comments, and non-breaking spaces, and 5 kinds of next child. The places are fix(core): start the text after a closing tag at the tag, and read a non-breaking space as text (#442, #444, #540) #615's 10 plus 19 more:

    • a nested element, a ternary, an arrow, an arrow with a block body, an @if, and an @switch inside a child container;
    • a nested attribute value and a TSX return with a container;
    • @default, a nested element, the component's top level, @if inside a case, a case inside @if, a nested @switch, and a container and an attribute value inside a case;
    • setup statements in a component, in an @case, and in a JS switch in a component.

    The text children, cleaned by Babel's JSX rule, are compared with Babel's JSX children for the same TSX without the comments.

  • Tests. vitest run --maxWorkers=4 for tsrx-utils, tsrx-analyze, the five compilers, the five runtime projects, prettier-plugin, and tsrx-prettier-plugin: 51 files, 9,214 passed, 77 skipped. New tests:

    • parser tests for 15 shapes in strict, collect, and loose mode, through parse_in_worker_with_ast, and one for the no-progress check;
    • 2 shared compile tests: the space after a closing tag in an element in a child container and in an attribute value, and the text of an element in an @case, with a non-breaking space;
    • the runtime cases above.

    All of them fail on main.

  • 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:

🤖 Generated with Claude Code


Note

Medium Risk
Touches core template/JSX tokenization in plugin.js; behavior changes are targeted but affect parsing of containers and switch cases broadly.

Overview
Fixes #612 and #613 so whitespace and text nodes inside elements nested in {…} expression containers, attribute JSX values, @switch cases, and plain switch returns parse like normal template markup instead of as JavaScript.

The TSRX parser now tracks expression-container path baselines with the actual #path reference (not only its length), so text after closing tags and child containers still counts as template text when control-flow bodies replace #path. Closing-tag handling uses the same baseline to decide whether the parent may read trailing text. Switch gating is scoped per element: raw text is blocked only when a switch label began after that element opened (between cases), not for markup inside a case body.

When raw-text consumption would leave the tokenizer unchanged, parseTemplateBody reports Unexpected token instead of looping until stack overflow.

Adds parser, compile, and shared runtime whitespace coverage for container, attribute, directive-in-container, @switch, and JS switch shapes.

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

leonidaz and others added 3 commits September 25, 2026 16:05
…s template text (#612, #613)

In an element inside a `{…}` container, the token after a closing tag was
read as code, because the closing tag only let the parent read it as text
outside every container. The text lost its leading space and a non-breaking
space there failed to parse (#612). Only a parent opened inside the innermost
container counts now, and the container's path baseline starts at 0 once a
control-flow body in the container has replaced the path, so text after a
closing tag or a child container in an element in such a body reads too.

In an element in a `switch` case (`@switch` or a function's `switch`), the
text was read as code whenever a `switch` label was in scope: leading spaces
were lost, a non-breaking space failed to parse, and text before a tag
overflowed the stack (#613). The label now only blocks text when the switch
started after the innermost element, which is between its cases.

The template body parser raises `Unexpected token` when a text read makes no
progress, instead of reading the same token until the stack runs out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 4701beb into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-template-text-containers branch September 25, 2026 20:33
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