Skip to content

fix(core): start the text after a closing tag at the tag, and read a non-breaking space as text (#442, #444, #540) - #615

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

leonidaz merged 2 commits into
mainfrom
fix/core-jsx-text-whitespace

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #442.
Closes #444.
Closes #540.

Template text follows JSX's whitespace rules after closing tags, around the comments TSRX allows between children, and for non-breaking spaces. Each case now renders on every target what the same TSX renders.

The bugs

The fix

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

  • The text after a closing tag starts at the tag, as after an opening or self-closing tag: #skipTrailingLayoutWhitespace() and its call are gone. The text keeps its leading whitespace, which the JSX compiler trims as layout when it has a line break, as it does after a self-closing tag. Line comments in it are recorded by #parseTemplateRawText, as elsewhere.
  • The stale-text-token recovery in parseTemplateBody is gone too. Only the skip produced a text token that starts at a tag: the branch ran 2,944 times over the comparison corpus below on main (strict and collect), and 12,396 times over 28,668 mutated inputs in strict, collect, and loose mode, and 0 times on this branch in any mode. Removing it changes no result for any of those inputs.
  • #shouldKeepTemplateTextNode reads JSX whitespace as space, tab, and line breaks (regex_not_whitespace, already [^ \t\r\n]), as Babel and Prettier do. It no longer calls .test on the global regex_newline_characters, whose lastIndex carries over between calls. isWhitespaceTextNode keeps \s: its other callers are an @switch case consequent, which is code, where a non-breaking space is JavaScript whitespace, and comment metadata.

It's narrow in that it doesn't touch how the tokenizer reads text, only where the text after a closing tag starts and which text is layout. No upstream cause and no override.

Comments between children

TSX reads /* c */ and // c between JSX children as text. TSRX reads them as comments, and a comment adds nothing to the text around it: the whitespace on its two sides is one run, and JSX's rules apply to that run as if the comment weren't there. #parseTemplateRawText already built the text's value that way, and both formatters assume it (pushJSXTextWithComments in @tsrx/prettier-plugin). The skip broke it only after a closing tag. So:

Source Text value Renders, like the TSX without the comment
<b>t</b>, a line break, /* c */ <i /> (#540) "\n ", layout nothing between the tags
<b />, a line break, /* c */ <i /> "\n ", layout nothing between the tags
<b>t</b> /* c */ <i /> " " a space
<b>t</b> // c, a line break, two " \n two" two

Non-breaking spaces and the targets

JSX compilers disagree here. Babel reads only space, tab, and line breaks as JSX whitespace, like Prettier and like #184's rule for ~= and class splitting. TypeScript's isWhiteSpaceSingleLine includes U+00A0, so it trims a non-breaking space at the edge of a line, as #444 notes. The compilers the targets use split the same way. For <div>, a line break, \u00a0<b>x</b>:

JSX compiler Target Renders
Babel (@babel/plugin-transform-react-jsx) React or Preact with Babel &nbsp;<b>x</b>
vue-jsx-vapor @tsrx/vue &nbsp;<b>x</b>
TypeScript any, with tsc <b>x</b>
oxc (Vite) @tsrx/react, @tsrx/preact, @tsrx/hono <b>x</b>
dom-expressions @tsrx/solid <b>x</b>

The parser now keeps the character as text and every target passes it to its JSX compiler, so each target renders what the same TSX renders with its compiler. Before, the parser dropped it, so Vue (and Babel) rendered nothing where the TSX renders the space. Whether TSRX should also make it render where the compiler trims it is a question for the user (Findings).

Rendered output on the targets

A shared runtime fixture (packages/tsrx/tests/shared/runtime/jsx-text-whitespace*.{tsrx,js}) renders 9 cases on React, Preact, Solid, Vue, and Hono in jsdom and compares each element's innerHTML with what the same TSX renders: #442 in an element and in a fragment, text on the line after a closing tag, #540 after a closing and a self-closing tag, a line comment after a closing tag, a block comment between children on one line, and the two non-breaking space cases, with each target's compiler's rule for them. On main, 17 of the 45 fail: #442 (both) and #540 on all five targets, and the non-breaking spaces on Vue.

Two cases avoid host differences that aren't TSRX's: vue-jsx-vapor collapses two spaces into one (/* c */ between children has a space on one side only), and Solid doesn't allow a fragment under an element (the fragment case is a component's root).

Formatters

Verification

  • AST and error comparison, main (24386ca4) against this branch, strict, { collect: true, errors: [], comments: [], preserveParens: true }, and { loose: true }, in at most 4 worker threads with a hard timeout. 17,101 inputs: the 69 tracked .tsrx files, the input and output of all 2,040 Prettier snapshot cases (readSnapshotEntries/readFormatCase), and 12,952 distinct string literals from test files, taken before this PR's tests were added.
  • TSRX against TSX. 4,640 generated sources: 6 kinds of previous child, 16 kinds of whitespace, comments, and non-breaking spaces, and 5 kinds of next child, in 10 places (template, return, nested element, fragment, @if, @for, @switch case, @{ … }, child container, attribute value). The text children, cleaned by Babel's rule, against Babel's JSX children for the same TSX without the comments. main: 1,574 mismatches and 270 errors. This branch: 264 mismatches and 295 errors, all in {…} containers and @switch cases ([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, [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). 1,285 are fixed and none that matched before mismatches now. 25 of the errors are new, and need a look (Findings).
  • Mutated inputs. 28,668 inputs made from the JSX in the corpus by truncating, deleting a span, or inserting a line break, a comment, a tag, or a non-breaking space, in the three modes. No hangs, and the same inputs parse. Besides the text starting at closing tags, and non-breaking spaces kept: in 2 inputs only the internal raisedAt of a thrown error moved by one character, and in 2 broken inputs (<di\nv></div>, <d iv>…</div>) collect and loose mode's thrown Unexpected token moved from the stray </div> after the mismatched tag's recovery to the line break before it, because the text token now starts there.
  • Tests. vitest run --maxWorkers=4 for tsrx-utils, tsrx-analyze, the five compilers, the five runtime projects, prettier-plugin, and tsrx-prettier-plugin: 50 files, 8,932 passed, 7 expected failures, 70 skipped. Also language-server, typescript-plugin, eslint-parser, and tsrx-vite: 19 files, 289 passed. New: parser tests for 12 shapes in strict, collect, and loose mode through parse_in_worker_with_ast, shared compile tests (the space after </span>, the non-breaking space in the output), the runtime suite, and the formatter tests above. Two parser tests expected the text after a closing tag without its leading whitespace ('tail', '1') and now expect it with.
  • 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 and @tsrx/prettier-plugin.

Findings

Filed, with other causes:

For the user:

Not TSRX's:

  • dom-expressions writes an inline non-breaking space (<b>x</b>\u00a0<i />) as a plain space in Solid's template, for TSX too, so Solid renders a plain space where the other targets render &nbsp;. The runtime suite leaves that case out.

🤖 Generated with Claude Code


Note

Medium Risk
Changes core template-body parsing and emitted JSX text, affecting rendered output across all JSX targets; scope is narrow but behavior-sensitive and covered by extensive new tests.

Overview
Template text parsing and formatting now follow JSX whitespace rules for text after closing tags, comments between children, and non-breaking spaces, so TSRX markup renders like equivalent TSX on each target.

The parser stops skipping layout whitespace after </tag> and removes the stale-text-token recovery that depended on that skip. Text after a closing tag starts at the tag (as after self-closing tags), so spaces like </span> 2 are preserved in the AST and JSX layout trimming applies correctly. Comments between children no longer leave stray rendered spaces when a block comment sits on the line after a close tag.

#shouldKeepTemplateTextNode treats only space, tab, and line breaks as JSX whitespace, so U+00A0 is kept as text and passed through to each target’s JSX compiler (which may keep or trim it at line edges, as TSX does).

@tsrx/prettier-plugin updates comment placement expectations (e.g. <b>t</b>// c stays glued). New parser, compile, formatter, and shared cross-target runtime tests lock in the behavior.

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

…CII (#442, #444, #540)

The template body parser moved the start of the text after a closing tag
past its leading whitespace when that had a line break. When the text was
only layout before the next closing tag, the token then started at that
tag's `<`, and the recovery for it popped the enclosing element's tokenizer
context, so the text after the next closing tag was read as code and lost
its leading space (#442). The skip also stopped at a block comment, so the
text after one on the next line lost its line break and rendered a space
(#540). The text after a closing tag now starts at the tag, as after a
self-closing tag, and the recovery, which nothing else reached, is gone.

Whitespace-only text used `\s`, so a non-breaking space next to a line
break was dropped as layout (#444). JSX whitespace is space, tab, and line
breaks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit b98651c into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-jsx-text-whitespace branch September 25, 2026 19:35
leonidaz added a commit that referenced this pull request Sep 25, 2026
…t is between its children (#639)

A comment adds nothing to the text around it: the whitespace on its two sides
is one run (#615), which renders a space without a line break, and nothing
with one beside an element or the start or end of the children. The adapter
now describes each comment group's run (`describeCommentRuns`), including a
`{" "}` beside it, and `printCommentChild` prints a run that renders the same:
- a significant space is JSX whitespace, which prints `{" "}` where a line
  breaks, at the start or end of the children when the element breaks, and
  written out before a line comment, which ends its line;
- a line break at the start or end of the children breaks the element when a
  space in the run would otherwise render;
- whitespace and `{" "}`s in the run are printed by the comments;
- a line comment after another comment starts its own line, since the parser
  reads `/* c */ // c` as text.

Also fixes `isEmptyDoc`, which judged any doc starting with an empty string
empty, and lays out children with such a comment with `fill`, like text.

Checked on every sequence of up to three of text, an element, an expression,
`{" "}`, a block comment, and a line comment, with no separator, a space, or a
line break between them (12,546 cases): each renders the same after
formatting, and a second format changes nothing. Before, 137 of the 558
two-part cases didn't.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz added a commit that referenced this pull request Sep 27, 2026
…rs, and what an element renders around comments between its children (#630, #639) (#596)

* chore(tsrx-prettier-plugin): open the working PR for the plan after #511

The plan, decisions, and state carried over from #511 are in the PR
description. Work on the plugin lands here as commits.

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

* fix(tsrx-prettier-plugin): keep a comment in a class method's type parameters (#630)

The adapter moves a class method's type parameters onto its function, as
typescript-estree has them, and now also starts the function at them, so a
comment inside them belongs to the function instead of the method's name.

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

* fix(tsrx-prettier-plugin): keep what an element renders when a comment is between its children (#639)

A comment adds nothing to the text around it: the whitespace on its two sides
is one run (#615), which renders a space without a line break, and nothing
with one beside an element or the start or end of the children. The adapter
now describes each comment group's run (`describeCommentRuns`), including a
`{" "}` beside it, and `printCommentChild` prints a run that renders the same:
- a significant space is JSX whitespace, which prints `{" "}` where a line
  breaks, at the start or end of the children when the element breaks, and
  written out before a line comment, which ends its line;
- a line break at the start or end of the children breaks the element when a
  space in the run would otherwise render;
- whitespace and `{" "}`s in the run are printed by the comments;
- a line comment after another comment starts its own line, since the parser
  reads `/* c */ // c` as text.

Also fixes `isEmptyDoc`, which judged any doc starting with an empty string
empty, and lays out children with such a comment with `fill`, like text.

Checked on every sequence of up to three of text, an element, an expression,
`{" "}`, a block comment, and a line comment, with no separator, a space, or a
line break between them (12,546 cases): each renders the same after
formatting, and a second format changes nothing. Before, 137 of the 558
two-part cases didn't.

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

* fix(tsrx-prettier-plugin): keep the space a {" "} beside a comment renders, and a {" "} with its own comment (#639)

- A `{" "}` in a comment's run counts as a space on its side of the comments,
  so it prints as the run's space instead of being dropped.
- Where the comments keep a line break inside the run, the run's space is
  written out as `{" "}`, since a plain space next to the line break is trimmed.
- A line comment after text starts its own line (on the text's line it is
  text) and always ends its line.
- The space around a group of comments goes on one side, chosen once, so a
  narrow line breaks in one place and the next format is stable.
- A `{" "}` with a comment inside is an ordinary child that prints its comment.

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

---------

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