Repository navigation
fix(core): see switch-case and dotted-namespace bindings before hoisting JSX - #1187
Merged
jonkwheeler merged 9 commits intoNov 12, 2026
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]>
…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]>
jonkwheeler
marked this pull request as ready for review
October 27, 2026 17:33
This was referenced Oct 27, 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
to jonkwheeler/tsrx
that referenced
this pull request
Nov 12, 2026
main now has tsrx-org#1187'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
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.
Stacked on #1121; review the commits after 0414840; merge after #1121.
Summary
Static JSX hoisting no longer moves an element to module scope when its tag names a local binding that the scope walk missed. There were two cases:
A declaration in an earlier
caseof the sameswitch. All cases share one block scope, but eachcasewas walked as its own scope.Before,
<Tag>ok</Tag>was hoisted toconst make__static1, somake(1).typewas'outer'. Now the element stays in place andmake(1).typeisinner, as in plain TSX.Any component in a namespace body. Inside a namespace, a name can read an export of any declaration of that namespace. TypeScript resolves
Fooin both of these toOuter.Foo:Before, both elements were hoisted to module scope, where
Foois"outer". Following the decision to put correctness ahead of the hoisting optimization, a component in anamespace/modulebody now keeps all its static JSX in place, intrinsic-only statics included. It gets a local helper state, the same one fix(core): hoist static JSX only when its tags are initialized, and keep raw script bodies #1121 gives a component inside another function, so its helper components stay in place too. They could read namespace exports the same way.The switch fix is in
scope_names, the scope walk #1121 adds to decide whether a static's tags can be read at module level. A static has no other identifier references (is_hoist_safe_jsx_node), so that walk is what decides it. The namespace rule replaces the inner-name tracking this PR's first commit added, so the second commit removes that code. The scope-state flag is renamed frominside_plain_functiontoin_local_helper_scope, since it now also covers namespace bodies.Hoisted counts in the repo don't move. Compiling all 78 tracked
.tsrxfiles gives the same number of__staticdeclarations before and after this change: React 150, Preact 147, Solid 79, Vue 131, Hono 73. None of them declares a namespace.Verification
leaves JSX whose tag reads %s in placetable, which compiles with each target, runs the output and compares the element type: an earlier switch case'sconst, the inner name ofnamespace Outer.Foo, a middle name ofnamespace Outer.Foo.Bar, and an export of anothernamespace Outerdeclaration. There is also a new test that an intrinsic-only static in a namespace stays in place. All of them failed before their fix (the output had__static) and pass for React, Preact, Solid, Vue and Hono.npx vitest run --testTimeout=120000before the namespace change: 176 files passed, 10154 tests passed, 82 skipped. A default-timeout run on this branch timed out in the TypeScript-backed tooling tests while the machine's load average was over 90. Those projects pass with the longer timeout, and none of them touch hoisting.pnpm typecheckclean,pnpm changeset:checkpasses, Prettier clean on changed files.Related
Fixes #1160
Related: #1121, #204
🤖 Generated with Claude Code
Note
Medium Risk
Changes JSX compile output and hoisting rules across all targets; risk is bounded to static-hoist edge cases but incorrect analysis could still change runtime element types.
Overview
Static JSX hoisting is now conservative about when elements can move to module-level
__staticconstants, so tags resolve the same at render time as in plain TSX.Hoisting runs only when
collect_hoistable_tagssays every component tag is safe at module init (imports, earlierconst/class, function declarations; member tags only when the root is an import). Tags that usethis/super, locals,let/var, enums/namespaces, late bindings, or<Screen.Tag>inside the same class stay inline—fixing TDZ and late-assignedMenu.Itemcases (#204).scope_namestreats allswitchcases as one block, so a tag in a latercaseis not hoisted against an outer binding (#1160).Components inside another function or a
namespacebody get alocalhelper state (in_local_helper_scope), so static JSX and lifted helpers stay in that scope instead of module level—needed because namespace names can mean exports, not the outerFoo.Hoisted trees run
lower_static_script_bodiesso raw<script>in expression containers or props keeps JSON/text (#845). Generatedspread_propstemporaries are renamed until they cannot collide with identifiers already in the source (core + Solid).Reviewed by Cursor Bugbot for commit e1aeccf. Bugbot is set up for automated code reviews on this repo. Configure here.