[Bug]: Skip whitespace text and comment {} children in the next @tsrx/core's tree
[!WARNING] Don't start until the
@tsrx/corerelease that includes tsrx-org/tsrx#869 is published (0.5.0, with tsrx-org/tsrx#858). Do it together with #1525, which covers #858's comment rules: both change how Ripple reads template children.
Issue Category
Other (preparing for the next @tsrx/core)
Bug Description
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 #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 Ripple needs to do
Measured by injecting the new whitespace text into the trees of the 230 .tsrx files in this repo that compile (at 97d090b9; nothing changed here): client and server output don't change, because Ripple already drops this text in its runtime paths (is_droppable_template_text, rendered_template_children, normalize_child, the server JSXText visitor). What changes:
Component and portal children keep the text, so
<Card>+ a line break +</Card>would get achildrenprop. Addis_droppable_template_text(child, false)(orisLayoutWhitespace) to the filters atclient/index.js:4633,server/index.js:2583and the portal's atclient/index.js:4532.sole_template_if: whitespace around a lone inner@ifin element children defeats the@if-in-@iffolding. Skip droppable text there.The TS view keeps text as written, since nothing is droppable with
to_ts(205 of the 230 files' TS views change, mostly source mappings):- the children thunk (
client/index.js:6622,element.children.length > 0) would get"⏎\t";statements; - the
<head>and void-element checks (analyze/index.js:2135,:2260) pass!!state.to_tsand would diverge between views: passfalse.
The simplest fix is to make
is_droppable_template_textdrop whitespace-only text in both views, keeping as written only text with words.- the children thunk (
Comments with nothing rendering around them now have a
{}too:<Card>+// c+</Card>has children"⏎",{},"⏎". Where Ripple asks "does this have children" (the filters above), an empty{}renders nothing and should count as none.Tests likely to change: the 63
compile_to_volar_mappingstests inpackages/tsrx-ripple/tests(mostlybasic.test.jsandjsx-runtime-types.test.js), and the@iffixtures inpackages/ripple/tests/server/if.test.tsrxandtests/client/basic/basic.rendering.test.tsrxif item 2 isn't done.
About 4–6 places in all.
Reproduction Steps
After upgrading, compile:
component App() {
<Card>
// c
</Card>
}Without the fixes, Card gets a children prop.
Environment
- Ripple
mainat97d090b9,@tsrx/core0.3.x
Additional Context
- tsrx-org/tsrx#869: the change, with the tests.
- #1525: #858's comment rules, in the same release.
- Octane and
@tsrx/oxc: octanejs/octane#1338 and octanejs/octane#1353, tsrx-org/oxc#118.
Checklist
- I've searched for existing issues
Source: Ripple-TS/ripple