Skip to content

feat(core): keep every character between the tags in an element's children - #869

Merged
leonidaz merged 2 commits into
mainfrom
fix/parser-keeps-whitespace-841
Sep 27, 2026
Merged

leonidaz merged 2 commits into
mainfrom
fix/parser-keeps-whitespace-841

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Core's parser now gives an element's and a fragment's children exactly the shape TSX's parsers give them. Every character between the tags is in the tree, and each comment is where {/* … */} would be in TSX:

<div>
  <b />
  // c
  <i />
</div>
children of <div>
before <b />, <i /> (the whitespace dropped, the comment attached to <i />)
now "⏎ ", <b />, "⏎ ", {}, "⏎ ", <i />, "⏎"

TypeScript, Babel, oxc, SWC, typescript-estree and acorn-jsx all keep that whitespace text, and apply JSX's whitespace rule only when compiling. TSRX's parser applied it while parsing, a leftover of Ripple's statement-based templates (Ripple-TS/ripple#1199). So both formatters had to rebuild the text from the source.

What renders doesn't change, with one fix:

<div>
  @if (true) {
    <b>t</b>
  } // c
  two
</div>

This used to render <b>t</b>two: the parser read what follows the block's } as code, and lost the space before the comment. It now renders <b>t</b> two, as TSX does.

Changes

@tsrx/core (minor, ships in 0.5.0 with #858; decision 51–52 in #852):

  • plugin.js:
  • New export isLayoutWhitespace(node): JSX's rule for text that renders nothing (empty, or only spaces, tabs and line breaks with a line break among them; a non-breaking space is text). It's the one place the rule lives, for core and for Ripple, Octane and the formatters (decision 52 in [PLAN]: tsrx-prettier-plugin: plan, decisions, and the Phase 1 queue #852).
  • utils/ast.js render_children(node): the children a compiler renders, without layout whitespace, and without an empty {} that separates nothing. A {} between two texts stays, since it keeps them apart as in TSX, and so does one holding a tooling comment (// @ts-expect-error), with the line breaks around it. Core's JSX transform reads children through it at the five places that did, so compiled output is unchanged. It doesn't change the parsed tree: a compile leaves its input as it is.

tsrx-prettier-plugin (private): rebuildJsxChildren and addJsxText are gone (−69 lines, +23). The adapter takes the parser's children as they are and turns each brace-less {} into its comment child (markJsxComments). This is what #841 set out to do.

@tsrx/prettier-plugin: no source change. Its tests compare the tree before and after a format, and now leave out layout whitespace there, as Prettier's own check of the tree does.

Verification

  • 11,071 tests pass (77 skipped): core's parser and analyzer, the five compilers and their runtimes, both formatters, the ESLint parser and rules, the TypeScript plugin, the language server and tsrx-mcp. Typecheck, pnpm format:check and pnpm changeset:check pass.
  • New tests:
    • the parser's children for comments touching tags, text and comments after a directive block, layout whitespace between elements, a {} for each of several comments;
    • an isLayoutWhitespace table;
    • runtime cases on all five targets: two comment lines in a row, comments touching tags, and the space before a comment after a block.
  • Parser tests that list children for another purpose (style recovery, code blocks, …) skip layout whitespace with isLayoutWhitespace; the ones about children list it.
  • An experiment before this PR, keeping all whitespace with no compiler change, failed 81 tests and no runtime test: the compilers printed the whitespace into their output and counted it in "is it empty" checks. render_children covers both.

Downstream

This changes the tree Ripple, Octane and @tsrx/oxc read, in the same release as #858. Measured with the change simulated (no downstream code changed):

  • Ripple: client and server output unchanged (it already drops this text); the editor's TypeScript view and a few children checks need is_droppable_template_text at 4–6 places.
  • Octane: about 151 tests fail until normalizeAuthoredJsxLiterals drops the text (4 lines); then only 2 parser-shape specs change.
  • @tsrx/oxc: drops whitespace like core did, so it has to change with core.

Upgrade issues: Ripple-TS/ripple#1526 and octanejs/octane#1353 (with #858's Ripple-TS/ripple#1525 and octanejs/octane#1338), and tsrx-org/oxc#118, rewritten for the final shape.

Closes #841.

🤖 Generated with Claude Code


Note

Medium Risk
Minor AST shape change in the core parser affects formatters and downstream consumers, though compile output is guarded by render_children; parser/comment attachment behavior is complex.

Overview
Aligns @tsrx/core JSX/fragment child lists with TSX parsers: layout whitespace stays as JSXText, and each inter-child comment becomes its own brace-less JSXExpressionContainer + JSXEmptyExpression (comment in innerComments). The parser also recovers tokenizer-skipped gaps after @if/@for/etc. and before the next child via #readSkippedTemplateText, fixing lost spaces/text (e.g. } // c then two, and trailing a /* c */ inside {…}).

Compiled output stays the same via new isLayoutWhitespace (exported) and render_children, which the JSX transform uses instead of dropping empty text at parse time. Comment-attachment metadata (CommentMetaData, elementLeadingComments, commentContainerId) is removed.

tsrx-prettier-plugin drops source-based rebuildJsxChildren in favor of markJsxComments on the parser tree; formatter tests ignore layout whitespace when comparing AST shape (like Prettier).

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

…ldren

The parser now gives an element's and a fragment's children exactly the
shape TSX's parsers give them: whitespace-only text (layout indentation)
is kept as JSXText, every comment between children is an empty `{}` of
its own with the comment in its JSXEmptyExpression's innerComments, and
the whitespace and comments right after a control-flow block are read as
text and comments too, which fixes a lost space before a comment there.

Adds the `isLayoutWhitespace` export, JSX's rule for text that renders
nothing, and `render_children`, which core's compilers use to read the
children that render, so compiled output is unchanged. The new Prettier
plugin reads the parser's children directly instead of rebuilding them
from the source (rebuildJsxChildren and addJsxText are gone).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ead, keep text before a comment in nested elements

Every comment between an element's children is now in a `{}` child,
so this code no longer does anything:

- the comment metadata the tokenizer attached (`#createCommentMetadata`,
  `commentContainerId`, a comment's `context`, `CommentMetaData`, and
  onComment's `metadata` argument), which nothing read;
- `elementLeadingComments`, written but never read, and its branch;
- `getEmptyElementInnerCommentTarget` and its four call sites, which
  only an element whose text had been dropped could reach;
- the hand-built text node for whitespace before `@{`, now the shared
  `#addSkippedTemplateText`.

Fixes the tokenizer for an element in a `{…}` container or an attribute
value: it dropped the text before a comment that ended the run, so
`{x && <span>a /* c */</span>}` rendered an empty `<span>`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit f78fada into main Sep 27, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/parser-keeps-whitespace-841 branch September 27, 2026 18:14
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