Skip to content

fix(core): hoist static JSX only when its tags are initialized, and keep raw script bodies - #1121

Merged
jonkwheeler merged 6 commits into
tsrx-org:mainfrom
jonkwheeler:fix/static-jsx-hoisting
Nov 12, 2026
Merged

jonkwheeler merged 6 commits into
tsrx-org:mainfrom
jonkwheeler:fix/static-jsx-hoisting

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes to hoisting static JSX into module-level consts, which React, Preact, Hono (server), Solid and Vue share. A static runs at module level before the top-level statement it came from, so it must only read what is ready by then and cannot change afterwards.

  • [Bug]: React static JSX hoisting reads a class before initialization #204 and the cases a review found: each tag now resolves through the scopes it is written in. Static JSX is hoisted only when every tag that reads a binding resolves to an import (re-exported or not), a function declaration, or a const or class declared in an earlier statement.
    • A member tag (<Menu.Item>) also reads a property, which the module can assign after it loads (Menu.Item = …, Object.assign(Menu, { Item })). So a member tag is hoisted only when it starts with an import (named, default or namespace), and every other member tag is read at render time, including ones rooted at a const.
    • Never hoisted: tags that read this, super, a binding local to a function, class, static block or block, a let, var, enum, namespace or import X = …, or a name the module does not declare.
    • Before, <Screen.Tag> in a method of class Screen threw Cannot access 'Screen' before initialization on import. In const C = class Screen { … } it threw Screen is not defined. <super.Tag> made the module fail to parse, and a let reassigned later was captured with its first value.
    • Intrinsic tags (<div>, <my-element>) are strings that read no binding, so they are hoisted even when a later binding shares their name.
  • Components inside another function (existing bug on main): their statics went to helper metadata that expand_component_helpers never reaches, so function outer() { function App() { return <div>hi</div> } } returned an undeclared App__static1. Such a component now gets a local helper state, which keeps its static JSX and any helper components in place and names its temporaries as main does.
  • Spread temporaries: the temporary that holds a host element's spread props beside a ref (App__spread_props1, or _tsrx_spread_props_1 outside a component) now skips any name the module source already uses, in core and in Solid, so it can't shadow or redeclare a binding the author wrote.
  • [Bug]: Static JSX expression containers drop raw script bodies after #790 #845: a raw <script> in an expression container or prop of static JSX keeps its body. Since fix(core): read a <script> body as raw text and print it from content in every target (#708) #790 the body lives in content, and the walk lowers it only in the render output it returns, which a hoisted static has already left. So <div>{<script type="application/json">{"a":1}</script>}</div> rendered an empty script on all five targets. Hoisting now lowers those scripts with the platform's element transform (transformElement hook or to_jsx_element).

Verification

  • packages/tsrx/tests/shared/compile.js compiles every case on all five targets and runs the output with a stub h, checking the tag value each element gets:
    • 13 cases that must stay in place: the class or class expression the JSX is in, this, super, a later const, a static-block binding, a later var, a reassigned let, a property assigned to a function declaration, a class or a const, a property that Object.assign gives a const, and a later import alias.
    • The nested component.
    • 6 cases that must still hoist (on React/Preact/Hono): an earlier const, a member of a namespace or default import, a later function declaration, an earlier class, and a re-exported import.
    • A spread temporary in a component inside a method, next to a user variable named _tsrx_spread_props_1 or App__spread_props1.
    • The intrinsic tag with a later const div.
    • On the previous head, 10 of these fail on React and the intrinsic and nested cases fail on Solid.
  • The shared script fixture (script-body-components.tsrx) has a container case that fails on main in the React, Vue and Solid client runtimes and in the server-HTML suite.
  • The reviewer's 25-case matrix (review/1121/cases.mjs) now matches plain TSX on all five targets.
  • Hoisted statics across the repo's 78 .tsrx files, compiled on main and on this branch: React 151 → 150, Preact 148 → 147, Hono 74 → 73, Solid 79 → 79, Vue 131 → 131. The one static that moves is <ThemeContext.Provider> in packages/vite-plugin-hono/tests/runtime.test.tsrx, a member tag on a const.
  • After the second review round: tsrx-utils, tsrx-analyze, the five target compiler and runtime projects and the five vite-plugin-* projects pass (79 files, 6,056 tests). pnpm typecheck, Prettier and pnpm changeset:check pass.
  • I ran the full npx vitest run once, on a local merge of all three of my PRs in this batch (fix(core): scan the code before a distant comment once, not per statement #1119, fix(core): run generated closures in the enclosing function's context #1120, fix(core): hoist static JSX only when its tags are initialized, and keep raw script bodies #1121): 10,043 passed, 88 skipped, 12 failed. All 12 failures were 5 s timeouts in tests that start TypeScript programs (content-mapper, typescript-plugin, language-server, and one tsrx-vue type-only test), while ten agents were running suites on this machine. With --testTimeout=120000, those four projects pass: 47 files, 1,474 tests. pnpm typecheck and pnpm changeset:check pass.

Related

Fixes #204
Fixes #845

🤖 Generated with Claude Code

jonkwheeler and others added 2 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]>
jonkwheeler and others added 4 commits October 27, 2026 07:12
…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]>
@jonkwheeler
jonkwheeler marked this pull request as ready for review October 27, 2026 12:33
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Oct 27, 2026
A generated helper named `App__StatementBodyHook1` could still collide
with, or shadow, an authored binding of that exact name, at module level
and, since the previous commit, in a component inside a function or a
namespace. Its name now skips any identifier the module source already
uses, the way the spread temporary does since tsrx-org#1121.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Oct 28, 2026
`source_mentions_identifier` put the name into a regular expression as
written. A component named `App$` gives helper names like
`App$__StatementBodyHook1`, where `$` reads as an end anchor, so the
scan never matched and an authored binding of that name was declared a
second time. The spread temporary of tsrx-org#1121 uses the same scan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jonkwheeler
jonkwheeler merged commit 1589310 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#1121'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 12, 2026
* fix(core): prefix a component's local helper with its name

Vue renders a `@catch` body under `@pending` through a generated helper
component. At module level it is named `App__StatementBodyHook1`. A
component inside another function or a namespace has a local helper
state, and the helper was then declared in place under the bare name
`StatementBodyHook1`: a setup local of that name failed the build with
a duplicate declaration, and a binding of that name in the enclosing
function was shadowed, so the fallback rendered the helper itself.

The local helper now takes the component prefix as well, and its
`typeof` aliases are named after it.

* fix(core): skip helper names the module source already uses

A generated helper named `App__StatementBodyHook1` could still collide
with, or shadow, an authored binding of that exact name, at module level
and, since the previous commit, in a component inside a function or a
namespace. Its name now skips any identifier the module source already
uses, the way the spread temporary does since #1121.

* fix(core): escape $ when scanning the source for a generated name

`source_mentions_identifier` put the name into a regular expression as
written. A component named `App$` gives helper names like
`App$__StatementBodyHook1`, where `$` reads as an end anchor, so the
scan never matched and an authored binding of that name was declared a
second time. The spread temporary of #1121 uses the same scan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]: Static JSX expression containers drop raw script bodies after #790 [Bug]: React static JSX hoisting reads a class before initialization

1 participant