Repository navigation
fix(core): hoist static JSX only when its tags are initialized, and keep raw script bodies - #1121
Merged
Merged
Conversation
…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]>
This was referenced Oct 26, 2026
…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
marked this pull request as ready for review
October 27, 2026 12:33
This was referenced Oct 27, 2026
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]>
This was referenced Oct 28, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.constor class declared in an earlier statement.<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 aconst.this,super, a binding local to a function, class, static block or block, alet,var, enum, namespace orimport X = …, or a name the module does not declare.<Screen.Tag>in a method ofclass ScreenthrewCannot access 'Screen' before initializationon import. Inconst C = class Screen { … }it threwScreen is not defined.<super.Tag>made the module fail to parse, and aletreassigned later was captured with its first value.<div>,<my-element>) are strings that read no binding, so they are hoisted even when a later binding shares their name.expand_component_helpersnever reaches, sofunction outer() { function App() { return <div>hi</div> } }returned an undeclaredApp__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.ref(App__spread_props1, or_tsrx_spread_props_1outside 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.<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 fromcontentin every target (#708) #790 the body lives incontent, 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 (transformElementhook orto_jsx_element).Verification
packages/tsrx/tests/shared/compile.jscompiles every case on all five targets and runs the output with a stubh, checking the tag value each element gets:this,super, a laterconst, a static-block binding, a latervar, a reassignedlet, a property assigned to a function declaration, a class or aconst, a property thatObject.assigngives aconst, and a later import alias.const, a member of a namespace or default import, a later function declaration, an earlier class, and a re-exported import._tsrx_spread_props_1orApp__spread_props1.const div.script-body-components.tsrx) has a container case that fails onmainin the React, Vue and Solid client runtimes and in the server-HTML suite.review/1121/cases.mjs) now matches plain TSX on all five targets..tsrxfiles, 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>inpackages/vite-plugin-hono/tests/runtime.test.tsrx, a member tag on aconst.tsrx-utils,tsrx-analyze, the five target compiler and runtime projects and the fivevite-plugin-*projects pass (79 files, 6,056 tests).pnpm typecheck, Prettier andpnpm changeset:checkpass.npx vitest runonce, 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 onetsrx-vuetype-only test), while ten agents were running suites on this machine. With--testTimeout=120000, those four projects pass: 47 files, 1,474 tests.pnpm typecheckandpnpm changeset:checkpass.Related
Fixes #204
Fixes #845
🤖 Generated with Claude Code