Repository navigation
Description
Warning
Don't start until the @tsrx/core release that includes tsrx-org/tsrx#869 is published (0.5.0, with tsrx-org/tsrx#858), and the @tsrx/oxc release that follows it (tsrx-org/oxc#118). Upgrade both together, and do it with #1338, which covers #858's comment rules.
Summary
The next @tsrx/core keeps whitespace-only text and gives every comment between children its own empty {} in the parse tree, as TSX's parsers do.
What changes in @tsrx/core's tree
The parser gives an element's and a fragment's children exactly the shape TSX's parsers (TypeScript, Babel, oxc, SWC) give them (tsrx-org/tsrx#869). Every character between the tags is in the tree:
<div>
<b />
// c
<i />
</div>
children of <div> |
|
|---|---|
@tsrx/core 0.4 |
<b />, <i /> (whitespace dropped, the comment attached to <i />) |
| with tsrx-org/tsrx#858 alone | the same |
| with tsrx-org/tsrx#869 (0.5.0) | "⏎ ", <b />, "⏎ ", {}, "⏎ ", <i />, "⏎" |
- Whitespace-only text is kept as a
JSXText, including layout indentation (a line break) between children, after the opening tag and before the closing tag. Before, the parser dropped it. - Every comment between children is an empty
{}of its own (JSXExpressionContainer+JSXEmptyExpression, spanning exactly the comment, no braces in the source), with the comment in theJSXEmptyExpression'sinnerComments. With perf(octane): skip unnecessary host prop sorts #858 alone, a{}appeared only between two texts that render. - The whitespace and comments right after an
@if/@for/@switch/@tryblock's}are text and{}children too. @{ … }and control-flow bodies are unchanged: they're code, so no whitespace text appears in them.
What renders is unchanged: JSX's whitespace rule says that text renders nothing. The one fix: a space before a comment after a block (} // c, then two on the next line) was lost, and now renders, as in TSX.
The rule is exported as isLayoutWhitespace(node) from @tsrx/core: true for a JSXText that is empty, or only spaces, tabs and line breaks with a line break among them. A space or tab without a line break renders, and a non-breaking space is text. Core's compilers read children through the same rule (render_children in packages/tsrx/src/utils/ast.js): they leave out layout whitespace and any {} that separates nothing, and keep a {} between two texts (it keeps them apart, as in TSX) and one holding a tooling comment such as // @ts-expect-error, with the line breaks around it.
What Octane needs to do
Measured by adding the new whitespace text to the trees of both parsers with a loader shim and running the octane project (13,404 tests, at b7bade7c; nothing changed here): about 151 tests fail (behavior-root 92, binding-handoff 24, auto-memo 10, scoped-jsx-values 10, plus hydration, descriptor children and fragment reconciliation). With the first fix below, only the 2 parser-shape specs remain.
normalizeAuthoredJsxLiteralstrims whitespace text tovalue: ''but leaves the node, and several readers count''as a child:lowerJsxChildemits''arguments (createElement('div', {}, '', b, '', i, ''),positionalChildren(['', x, '']), then "Children.only" errors),onlyMeaningfulJsxChildmisses the only child, anddom-bindings.js:935turns a text leaf structural ("binding views support fixed native elements only"). Fix: drop the children thatisLayoutWhitespaceaccepts fromJSXElement/JSXFragmentthere (about 4 lines). Optionally also treat''as empty in those three readers.- A comment with nothing rendering around it now has a
{}(<div>+// c+<b />+</div>has children"⏎",{},"⏎",<b />,"⏎"). Only-child and child-count checks should skip an empty{}like layout text. - Specs:
tests/compiler/style-syntax-fallback.test.tslines 229 and 236 expect noJSXText; filter withisLayoutWhitespace. @tsrx/oxcdrops whitespace text like core did (checked with@tsrx/oxc0.16). Until it keeps it (Read // like @tsrx/core and give an element's children TSX's shape, with comments as {} children tsrx-org/oxc#118), Node and the browser parse to different trees.
Already fine: the template path (normalizeChildren), the universal and Valdi compilers, and the places that test /^\s*$/. volar.js keeps the text in its TSX, where TypeScript applies the same rule. Note that the TypeScript-retry parser (parser.browser.js, acorn-typescript) already produces whitespace text, so item 1's bugs can happen on that path today.
References
- feat(core): keep every character between the tags in an element's children tsrx-org/tsrx#869: the change, with the tests.
- Render template comments like {/* */} and read // like JavaScript, for the next @tsrx/core #1338: perf(octane): skip unnecessary host prop sorts #858's comment rules, in the same release.
- [Bug]: Skip whitespace text and comment {} children in the next @tsrx/core's tree Ripple-TS/ripple#1526: Ripple's side of the same change.
Activity
fix: address the TSRX 0.5 upgrade issues
fix: update the TSRX toolchain to @tsrx/core 0.5 and the new formatter (