Repository navigation
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
Conversation
…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]>
This was referenced Sep 25, 2026
This was referenced Sep 25, 2026
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]>
This was referenced Sep 25, 2026
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]>
This was referenced Sep 27, 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 #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
#skipTrailingLayoutWhitespace(), which moved the start of the next text past its leading whitespace when that whitespace had a line break. When that text was only layout before the next closing tag (<b>1</b>then a line break, then</span>), the text token then started at that tag's<. The branch ofparseTemplateBodythat recovers from "a stale text token whose start was advanced onto the following<" popped everytc_exprcontext, the enclosing element's too, so the token after</span>was read as code andskipSpaceate the space:</span> 2read"2"and rendered12." ", the comment left out) rendered as a space. After a self-closing tag nothing was skipped, and the same source rendered nothing.#shouldKeepTemplateTextNodeusedisWhitespaceTextNode, which tests\s, so text that is a non-breaking space next to a line break was dropped as layout.The fix
All in
packages/tsrx/src/plugin.js:#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.parseTemplateBodyis 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 onmain(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.#shouldKeepTemplateTextNodereads JSX whitespace as space, tab, and line breaks (regex_not_whitespace, already[^ \t\r\n]), as Babel and Prettier do. It no longer calls.teston the globalregex_newline_characters, whoselastIndexcarries over between calls.isWhitespaceTextNodekeeps\s: its other callers are an@switchcase 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// cbetween 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.#parseTemplateRawTextalready built the text's value that way, and both formatters assume it (pushJSXTextWithCommentsin@tsrx/prettier-plugin). The skip broke it only after a closing tag. So:<b>t</b>, a line break,/* c */ <i />(#540)"\n ", layout<b />, a line break,/* c */ <i />"\n ", layout<b>t</b> /* c */ <i />" "<b>t</b> // c, a line break,two" \n two"twoNon-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'sisWhiteSpaceSingleLineincludes 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>:@babel/plugin-transform-react-jsx) <b>x</b>@tsrx/vue <b>x</b>tsc<b>x</b>@tsrx/react,@tsrx/preact,@tsrx/hono<b>x</b>@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'sinnerHTMLwith 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. Onmain, 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
@tsrx/prettier-plugin: two expectations change, both<b>t</b>// cthenc. The comment is in the text after the closing tag now, as it already was after a self-closing tag, so the formatter keeps it glued, as it prints<b />// conmain("nothing where the comment touches its neighbor"); a new case pins<b />// c. It used to print<b>t</b> // c, because the skip made the comment trail<b>t</b>. The rendered markup is the same (<b>t</b>c). New tests: the [Bug]: core drops the space after the closing tag of an element whose body ends in a line break #442 and [Bug]: core: A block comment on the line after a closing tag makes the space after it render #540 sources format and render the same, and<div>\u00a0<b>x</b></div>formats to the non-breaking space starting a line and stays that way on the next pass. Onmainthe second pass dropped it ([Bug]: core drops template text that is a non-breaking space next to a line break #444's idempotence failure). A comment inprintJSXElementBodythat described the old skip is updated.tsrx-prettier-plugin: no change; its tests and the Prettier known-failures list pass as they are.Verification
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.tsrxfiles, 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."c"becomes"\n c"). With the text cleaned by Babel's JSX rule, the ASTs are identical, so all 30 render the same. In 6 of them a line comment right after the closing tag is now inside the text instead of trailing the element; the collected comments are the same.return, nested element, fragment,@if,@for,@switchcase,@{ … }, 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@switchcases ([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).raisedAtof a thrown error moved by one character, and in 2 broken inputs (<di\nv></div>,<d iv>…</div>) collect and loose mode's thrownUnexpected tokenmoved from the stray</div>after the mismatched tag's recovery to the line break before it, because the text token now starts there.vitest run --maxWorkers=4fortsrx-utils,tsrx-analyze, the five compilers, the five runtime projects,prettier-plugin, andtsrx-prettier-plugin: 50 files, 8,932 passed, 7 expected failures, 70 skipped. Alsolanguage-server,typescript-plugin,eslint-parser, andtsrx-vite: 19 files, 289 passed. New: parser tests for 12 shapes in strict, collect, and loose mode throughparse_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 typecheckpasses.tsrx-tscisn't linked afterpnpm install --ignore-scripts, so I built@tsrx/typescript-pluginand 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/coreand@tsrx/prettier-plugin.Findings
Filed, with other causes:
{…}container (a child or an attribute value), the text after a closing tag still loses its leading whitespace ({x && <div><b>1</b> 2</div>}renders12), and a non-breaking space there fails withUnexpected character.inside_parent_templaterequires no enclosing container, so#closingNativeTemplateNodeis still set when the token after the>is read.@switchcase, text is read as code: leading spaces are lost after any tag or container, a non-breaking space fails to parse, and text before a tag (<div>1<b /></div>) overflows the stack.#shouldReadTemplateRawTextTokenreturns false whenever aswitchlabel is in scope.For the user:
or{"\u00a0"}). TSRX would then render them where the same TSX doesn't, on oxc, TypeScript, and Solid. Related: the formatter, like Prettier, moves a non-breaking space next to a child onto its own line when the element breaks (<div>\u00a0<b>x</b></div>), so on TypeScript, oxc, and Solid formatting changes what renders. Prettier does the same for TSX.@switchsources that parsed now throw. A non-breaking space right after a nested element's closing tag in an element in an@switchcase (<span>…</span>\u00a02) parsed onmain, with the character dropped, because [Bug]: core drops the space after the closing tag of an element whose body ends in a line break #442's context over-pop made the token after</span>read as code, which skips it as whitespace. Now it fails withUnexpected character, as the same character after a closing tag of an element without a nested body already does onmain([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). No real input in the corpus has it; fixing [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 fixes both.Not TSRX's:
<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 . 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> 2are 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.#shouldKeepTemplateTextNodetreats 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-pluginupdates comment placement expectations (e.g.<b>t</b>// cstays 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.