Skip to content

Skip whitespace text and comment {} children in the next @tsrx/core's tree #1353

Closed

Description

@leonidaz

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 the JSXEmptyExpression's innerComments. 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/@try block'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.

  1. normalizeAuthoredJsxLiterals trims whitespace text to value: '' but leaves the node, and several readers count '' as a child: lowerJsxChild emits '' arguments (createElement('div', {}, '', b, '', i, ''), positionalChildren(['', x, '']), then "Children.only" errors), onlyMeaningfulJsxChild misses the only child, and dom-bindings.js:935 turns a text leaf structural ("binding views support fixed native elements only"). Fix: drop the children that isLayoutWhitespace accepts from JSXElement/JSXFragment there (about 4 lines). Optionally also treat '' as empty in those three readers.
  2. 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.
  3. Specs: tests/compiler/style-syntax-fallback.test.ts lines 229 and 236 expect no JSXText; filter with isLayoutWhitespace.
  4. @tsrx/oxc drops whitespace text like core did (checked with @tsrx/oxc 0.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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions