Skip to content

perf(core): hoist static JSX that an expression container renders - #1279

Merged
jonkwheeler merged 17 commits into
tsrx-org:mainfrom
jonkwheeler:fix/hoist-static-in-containers
Nov 12, 2026
Merged

jonkwheeler merged 17 commits into
tsrx-org:mainfrom
jonkwheeler:fix/hoist-static-in-containers

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1238; review the commits after 9ec7177; merge after #1238.

Summary

React, Preact and Hono: a static element inside an expression container is hoisted like a bare one (#81). These targets hoist a static element written bare in a component's output to a module-level constant, which gives React a stable element to skip. The same element wrapped in {…} stayed in the render.

export function App() {
	return <>{<b>a</b>}<b>b</b></>;
}
#1238 (9ec7177) This branch
React, Preact, Hono const App__static1 = <b>b</b>; … return <>{<b>a</b>}{App__static1}</>; const App__static1 = <b>a</b>; const App__static2 = <b>b</b>; … return <>{App__static1}{App__static2}</>;

Containers are hoisted wherever they sit in the rendered JSX:

  • in an element's or a fragment's children, including inside a dynamic element;
  • in an attribute value, as in <Card header={<h1>Title</h1>} n={props.n} /> → header={App__static1} (not key or ref);
  • in either branch of ?:, and on the right of &&, || and ??;
  • in a nested container: {<div title={label}>{<span>nested</span>}</div>} → {<div title={label}>{App__static1}</div>};
  • in the JSX that a function in the component body returns, such as a render helper or a callback (const cb = () => <>{<span>row</span>}</>). That JSX is lowered like the component's own output, and fix(solid,vue): leave static JSX in place so each use gets its own node #1238 already hoisted its bare statics; this PR hoists its containers too.

In a branch that may not render, only host elements are hoisted. A static reads its tags when the module loads, so {false && <ui.Button x="x" />} must not become a static: in a cyclic import, ui.Button can still be uninitialized then. Two kinds of container are treated differently:

  • in a ?: branch or on the right of &&, || or ?? (and anything nested under one), an element is hoisted only if it is a host element and its subtree has no component or member tag. {on ? <Icon size={1} /> : <span>off</span>} hoists only the <span>.
  • an unconditional container hoists components, as a bare static does: {<ui.Button x="x" />} and <Card header={<Icon size={1} />} … />.

A static's name skips names the source already uses. Before, an authored const App__static1 in the component captured the static's references, and one at module level redeclared it. The name now moves to the next number, through the same check as #1121's spread-props temporaries; like #1229, that check now escapes a $ in the name.

Known limitation (unchanged): a function declaration used as a tag can be reassigned after the module loads (Tag = Replacement), and a static keeps the function it read at load. This already applied to bare statics; it is now noted in the comment on collect_hoistable_tags. In conditional containers it can't happen, since those hoist only host elements.

The parsed tree keeps the authored JSX. The walk copies each node on the way to a hoisted element instead of changing it, so the sourceAst that compile_to_volar_mappings returns, and that editor features read, still holds the container JSX as written.

What stays the same:

  • No merging. Following the decision on [Bug]: TSRX v2 | static hoisting improvements #81, consecutive statics are not merged into one fragment, since that would change the children React sees. A static fragment in a container keeps its children, each hoisted on its own, as a component's top-level fragment already does: {<><a>one</a><a>two</a></>} → {<>{App__static1}{App__static2}</>}.
  • Never hoisted:
    • an element with a key;
    • JSX that reads a binding, including a component with props from scope;
    • a bare <Helper /> (as before);
    • JSX inside a function, an array or any other expression within a container ({items.map(() => <li>row</li>)}, {[<u key="a">a</u>]}): the walk doesn't enter them. A function elsewhere in the component body whose returned JSX is lowered as render output gets its own pass, as above;
    • a component or member tag in a conditional branch.
  • Bare children of a dynamic element stay in place, as before. <div title={x}><p>bare</p>{<p>contained</p>}</div> hoists only the contained <p>. [Bug]: TSRX v2 | static hoisting improvements #81 is about containers; hoisting bare nested children too would be a separate change.
  • Solid, Vue and Hono DOM don't hoist since fix(solid,vue): leave static JSX in place so each use gets its own node #1238 and Hono DOM's own hook, so their output is byte-identical.

Output on the repo's 78 .tsrx files (sources as at 9ec7177, compiled at 9ec7177 and at this head; none has a component in a conditional container, so the counts match the first version of this PR):

Target Files changed Statics, compile Statics, type view
React 4 178 → 190 163 → 175
Preact 5 175 → 188 163 → 175
Hono (server) 3 compile / 4 type view 73 → 84 163 → 175
Solid, Vue, Hono DOM 0 unchanged unchanged

The type view is compile_to_volar_mappings. Inlining every static back into the old and new output gives the same code in each changed file. The one exception is the shared script-body-components.tsrx on Preact and Hono, where the hoisted raw <script> loses its {…} wrapper when inlined back; that is the same element.

Verification

  • packages/tsrx/tests/shared/compile.js, new in expression containers (#81) tests, run for every target:

    • a container beside a bare element: both hoisted, and App() returns the same child objects on each call;
    • children, attribute values, branches, nested containers and fragments: exact output, and the rendered tree equals the tree written inline;
    • a nested container in an @{ … } component body;
    • two consecutive containers: two statics, no merging;
    • the shared runtime fixture: 10 statics.

    On React, Preact and Hono all five fail before the change (15 failures) and pass now. A sixth test (keyed elements, binding reads, bare children, callbacks, arrays: nothing hoisted) passes before and after on every target.

  • Review fixes, also in compile.js and run for every target:

    • conditional containers: only the host elements are hoisted, with exact output;
    • {props.on && <ui.Button x="x" />} with an import whose Button getter throws: the module loads and renders [false];
    • unconditional containers still hoist <ui.Button> and a component in an attribute value.
  • packages/tsrx/tests/shared/source-mappings.js: a new "does not mutate the parsed program" case holds every container shape, in a plain function and an @{ … } component.

    On React, Preact and Hono the conditional, getter and sourceAst tests fail on the first version of this PR (ec6a8ac; 9 failures) and pass now. The unconditional test passes on both.

  • Static names (compile.js, every target): an authored App__static1 at module level and App__static2 in the component give statics App__static3 and App__static4, and the module renders ['div', 'b', 'module', 'local']; $App__static1 in function $App gives $App__static2. Both fail on 62ef3e0 on React, Preact and Hono (6 failures) and pass now. Output for the 78 files is unchanged from 62ef3e0 on every target.

  • New shared runtime fixture (static-containers-components.tsrx and static-containers.js): one component with each container shape, plus keyed elements and elements that read props.

    • React and Preact (tsrx-react-runtime, tsrx-preact-runtime) render it twice from a stateful host and check every static in both instances across five updates that toggle the branches and change a counter.
    • Hono server (packages/vite-plugin-hono/tests/static-containers.test.js) renders two instances through Vite SSR with three sets of props. Each is rendered twice, giving identical HTML, and the same checks run on the parsed HTML.
    • No React warnings.
  • Full npx vitest run: 177 files, 10243 passed, 87 skipped, 0 failed.

  • pnpm typecheck clean, pnpm changeset:check passes, Prettier clean on changed files.

Related

Fixes #81
Related: #1238, #1121, #1187

🤖 Generated with Claude Code


Note

Medium Risk
Touches core JSX lowering with subtle conditional-hoist rules that affect module-load evaluation order and React child shape; mitigated by broad compile/runtime tests.

Overview
React, Preact, and Hono now hoist static JSX inside {…} expression containers to module-level constants, matching behavior for bare static siblings—so reconcilers can reuse element identity in children, props, ?:/&&/||/?? branches, nested containers, and render helpers, without changing Solid/Vue/Hono DOM output.

The JSX transform adds a separate hoist path (hoist_static_jsx, hoist_static_jsx_in_containers, etc.) that walks containers with immutable AST copies for editor mappings. Conditionally rendered branches hoist only host elements (not component/member tags) to avoid reading uninitialized imports at module load. Generated __staticN names skip identifiers already present in source. Keys, binding reads, map callbacks, and similar cases stay unhoisted; static fragments in containers hoist children individually without merging.

Tests cover compile output, conditional safety (getter-on-import), static naming, source-mapping non-mutation, shared runtime fixtures, and Hono SSR HTML.

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

jonkwheeler and others added 15 commits October 26, 2026 15:36
…zed binding

Static JSX is hoisted to a module-level const placed before the
top-level statement it came from, so its tag is read when the module
loads. A tag that reads the class the JSX is in (`<Screen.Tag>` in a
method of `class Screen`), a const or class declared later, or `this`
then threw on import: "Cannot access 'Screen' before initialization".

Hoisting now skips JSX whose tag reads `this` or a name that a
top-level statement binds at or after the one the JSX is in. Function
declarations and imports are initialized before the module body runs,
so tags that read them are still hoisted.

Fixes tsrx-org#204

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since tsrx-org#790 a raw `<script>` keeps its body in `content`, and the walk
lowers it when it reaches the element. An element in an expression
container or attribute value of static JSX is reached only in the
render output the walk returns, which a hoisted static has already
left. So `<div>{<script type="application/json">{"a":1}</script>}</div>`
and a script passed as a prop rendered an empty script on every target.

Hoisting now lowers those scripts with the platform's element
transform, as the walk would.

Fixes tsrx-org#845

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…odule binding

The tsrx-org#204 fix checked tags only against top-level declarations, by
position. It still hoisted `<super.Tag>` (a module that fails to
parse), `<Screen.Tag>` in `const C = class Screen { … }` (`Screen is
not defined`), tags that read a static block's or block's bindings, and
member tags such as `<Menu.Item>` whose property is assigned after the
module loads, along with tags that read a `let` or `var` assigned
later.

Each tag now resolves through the scopes it is written in, and static
JSX is hoisted only when every tag that reads a binding resolves to an
import, a function declaration, or a `const` or class declared in an
earlier statement. A member tag that starts with a function or class
declared in the module is read at render time, as is a tag that reads
`this`, `super`, a local binding, a `let`, `var`, enum, namespace or
import alias, or a name the module does not declare. Intrinsic tags
like `<div>` read no binding, so a later `const div` no longer stops
them from being hoisted.

Related: tsrx-org#204

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ction

A component's statics and helper components go to module level through
the top-level statement that holds it, and `expand_component_helpers`
finds them only through nodes that are not functions. A component
declared inside another function (`function outer() { function App() {
return <div>hi</div> } }`) still hoisted its JSX, so it returned
`App__static1`, which was never declared, and threw a ReferenceError
when it rendered.

Such a component now gets no helper state, so it lowers its statics
and helpers in place, as code outside a component does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A member tag on a `const` declared earlier was still hoisted, so the
compound-component pattern `const Menu = …; <Menu.Item />` with
`Menu.Item = …` (or `Object.assign(Menu, { Item })`) after it read
`Menu.Item` when the module loaded and rendered an undefined type.
A member tag now stays at render time unless it starts with an import
(named, default or namespace). Plain tags keep the earlier rules.

Related: tsrx-org#204

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Giving such a component no helper state, to keep its statics and
helper components in place, also moved its spread temporary to the
fallback name `_tsrx_spread_props_N`, which can shadow or redeclare a
user variable of that name. The component now gets a local helper
state again: it names things as it did on main and keeps its statics
and helper components in place. The spread temporary, in core and in
Solid, also skips any name the module source already uses.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ing JSX

The scope walk that decides whether a static's tags can be read at module
level took each `case` of a `switch` as its own scope, and gave a dotted
namespace's body none of the inner names. A tag that read a `const` from an
earlier case, or `Foo` inside `namespace Outer.Foo`, was hoisted to module
scope, where it named a different binding. Collect every case's
declarations for the whole `switch`, and add each inner name of a dotted
namespace for the code inside it.

Fixes tsrx-org#1160

Co-Authored-By: Claude Opus 5.5 <[email protected]>
In a namespace body a name can read an export of any declaration of the
namespace: in `namespace Outer { export function Foo() {} }` followed by
`namespace Outer { … <Foo /> … }`, TypeScript reads `Foo` as `Outer.Foo`.
Module level can't see that, so a static hoisted out of the second block
read the module's `Foo`. Give a component in a namespace body a local
helper state, as one inside another function gets, so its statics and
helper components stay in place. That covers a dotted namespace's inner
names too, so the scope walk no longer adds them.

Related: tsrx-org#1160

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Solid JSX evaluates to a DOM node, so a static hoisted to a module-level
const was one node that every use of the component moved into place: a
static component rendered twice showed once, and in a three-row @for it
showed only in the last row. babel-preset-solid already hoists the
parsed template (`template('<p class=b>plain')`) and clones it on each
evaluation, so Solid's canHoistStaticNode now returns false, as Hono
DOM's does.

Fixes tsrx-org#1236

Co-Authored-By: Claude Opus 5.5 <[email protected]>
vue-jsx-vapor compiles JSX to Vapor DOM, so a static hoisted to a
module-level const was one node that every use moved into place: a
static component rendered twice showed once, a static @for row showed
once for the whole list, and a static @if branch showed in only one
instance of its component. vue-jsx-vapor already hoists the parsed
template (`_template("<p class=b>plain", true)`) and clones it on each
evaluation, so Vue's canHoistStaticNode now returns false, as Solid's
and Hono DOM's do.

Fixes tsrx-org#1240

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Solid lowers @if and @for bodies itself and never hoisted their static
JSX; these cases match the Vue runtime tests for tsrx-org#1240.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Solid and Vue stopped hoisting static JSX (tsrx-org#1236, tsrx-org#1240), so the comment
no longer describes them as callers. The function stays exported as part
of @tsrx/core's public API.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
React, Preact and Hono hoisted a static element written bare in a
component's output, but not the same element inside an expression
container: `<>{<div> 123 </div>}</>` kept the inner `<div>` in the
render. A static element in a container is now hoisted the same way,
wherever the container sits in the rendered JSX: in an element's or a
fragment's children, in an attribute value other than `key` and `ref`,
in either branch of `?:`, on the right of `&&`, `||` and `??`, and in a
nested container. A static fragment in a container keeps its children,
each hoisted on its own, and consecutive statics stay separate, so the
children React sees keep their shape.

An element with a `key`, JSX that reads a binding, a bare child of a
dynamic element, and JSX inside a callback, an array or any other
expression stay in place. Solid, Vue and Hono DOM don't hoist, so their
output is unchanged.

Fixes tsrx-org#81

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…omponents in place

Two fixes to hoisting static JSX from expression containers (tsrx-org#81):

- The container walk replaced nodes in place, so the parsed tree that
  compile_to_volar_mappings returns as sourceAst, and that editor
  features read, held `App__static1` in place of the authored JSX. It
  now copies each node on the way to a hoisted element.
- A static reads its tags when the module loads, so `{false &&
  <ui.Button />}` read `ui.Button` at import, which throws in a cyclic
  import. In a `?:` branch or on the right of `&&`, `||` or `??`, only
  host elements whose subtree has no component or member tag are
  hoisted now. Unconditional containers still hoist as bare statics do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A static was always named `App__static<n>`, so an authored
`const App__static1` in the component captured the static's references,
and one at module level redeclared it. The name now skips any name the
module source already uses, through the same check as the spread-props
temporaries, which now also escapes a `$` in the name (as tsrx-org#1229 does).

The reassignment of a function tag after the module loads stays a known
limitation, now noted where the hoistable tags are collected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jonkwheeler and others added 2 commits November 12, 2026 11:13
Main already exports source_mentions_identifier, with the same `$` escape this branch adds, so its exported form stays. The shared compile tests keep this branch's readFileSync import beside main's path import.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
The React and Preact runtime tests keep this branch's static-container harness import and call beside main's generated-import-bindings harness (tsrx-org#1175).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@jonkwheeler
jonkwheeler merged commit bbf7092 into tsrx-org:main Nov 12, 2026
8 checks passed
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Nov 12, 2026
main now has tsrx-org#1279's squash commit; merged against the parent commit this branch has so git doesn't redo that work.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
jonkwheeler added a commit that referenced this pull request Nov 18, 2026
…ainers as in children lists (#1367)

* fix(core, solid): print edge whitespace in JSX inside expression containers as in children lists

The spaces and tabs at the edges of an element's children print as
`{' '}` (#1277), but JSX inside a `{…}` container kept its text as
written on React, Preact, Hono and Solid, so
`<><div> 123 </div>{<div> 123 </div>}</>` printed two forms (#1351).

- React, Preact, Hono: a static hoisted out of a container (#1279)
  leaves the walk that applies the rule. lower_static_script_bodies,
  renamed lower_static_jsx, already redoes the walk's `<script>`
  lowering for a static; it now also wraps each children list in the
  static. This covers a container inside a bare static element too.
- Solid: create_element_children returns wrapEdgeWhitespace(...), so
  JSX in a container, an attribute value, a callback, a `const`, a
  module-level declaration or nested in a control-flow body gets the
  rule. Solid's compiler collapses a whitespace run in JSX text but
  keeps a `{'  '}` literal, so `{<b>  1  </b>}` now renders "  1  "
  like a children list instead of " 1 ".
- Vue already printed both forms the same; its output is unchanged.

In those places an element's text with edge whitespace now gets three
children instead of one string, as in a children list, so React's
existing <textarea>/<title> problem with that shape reaches them too.

wrapEdgeWhitespace returns its input when it wraps nothing, and gives
the trimmed text the location of what is left of it: with the whole
text's location, a `@` after a space (`<div> @</div>`) mapped one
character early and got no directive completion, in a children list
and now in a container.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
jonkwheeler added a commit that referenced this pull request Nov 18, 2026
* perf(core): hoist a fully static fragment as one constant

React, Preact and Hono hoisted static JSX element by element (#1279),
so a fragment whose elements were all hoisted was still built again
around their constants on every render.
`<><div> 1 </div>{<><div> 2 </div><div> 3 </div></>}</>` gave three
constants; it now gives one `const B__static1 = <>…</>;` and
`return B__static1;` (#1352).

Hoisting itself is unchanged. Once a component's output is lowered, each
fragment in it that holds at least one static, and otherwise only text,
literals and fragments of these, is replaced by one static that takes in
the statics it held, with the name and place of the first of them. A
static's reference is known by the declaration it points to, not by its
name, and a static is taken in only through its one reference.

Since it reads the output as lowered, the static prints each text as the
fragment in place did, so server markup and text nodes stay the same
wherever the walk wraps or leaves edge whitespace. It takes in no element
#1279 left in place, so every element is built when it was before, and
no component inside the fragment renders less often. A memoized
component that receives the fragment as a prop or as children can skip
a render, as it can for a single hoisted element since #1279. A keyed
`@for` row is a keyed element by then, so it keeps its key and stays in
place.

Solid, Vue and Hono DOM don't hoist, and their output is byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TSRX v2 | static hoisting improvements

1 participant