Skip to content

fix(core): see switch-case and dotted-namespace bindings before hoisting JSX - #1187

Merged
jonkwheeler merged 9 commits into
tsrx-org:mainfrom
jonkwheeler:fix/hoist-switch-namespace-bindings
Nov 12, 2026
Merged

jonkwheeler merged 9 commits into
tsrx-org:mainfrom
jonkwheeler:fix/hoist-switch-namespace-bindings

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. A declaration in an earlier case of the same switch. All cases share one block scope, but each case was walked as its own scope.

    const Tag: any = 'outer';
    function make(k: number): any {
      switch (k) {
        case 1:
          const Tag: any = inner;
        case 2:
          return <Tag>ok</Tag>;
      }
    }

    Before, <Tag>ok</Tag> was hoisted to const make__static1, so make(1).type was 'outer'. Now the element stays in place and make(1).type is inner, as in plain TSX.

  2. Any component in a namespace body. Inside a namespace, a name can read an export of any declaration of that namespace. TypeScript resolves Foo in both of these to Outer.Foo:

    const Foo = "outer";
    namespace Outer { export function Foo() { return null; } }
    namespace Outer.Foo { export function App() { return <Foo>ok</Foo>; } }  // Foo is Outer.Foo
    namespace Outer { export function App2() { return <Foo>ok</Foo>; } }     // emitted as <Outer.Foo>

    Before, both elements were hoisted to module scope, where Foo is "outer". Following the decision to put correctness ahead of the hoisting optimization, a component in a namespace/module body 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 from inside_plain_function to in_local_helper_scope, since it now also covers namespace bodies.

Hoisted counts in the repo don't move. Compiling all 78 tracked .tsrx files gives the same number of __static declarations before and after this change: React 150, Preact 147, Solid 79, Vue 131, Hono 73. None of them declares a namespace.

Verification

  • Four new rows in the shared leaves JSX whose tag reads %s in place table, which compiles with each target, runs the output and compares the element type: an earlier switch case's const, the inner name of namespace Outer.Foo, a middle name of namespace Outer.Foo.Bar, and an export of another namespace Outer declaration. 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.
  • After the namespace change: tsrx-utils plus the five target projects pass (39 files, 5176 tests).
  • Full npx vitest run --testTimeout=120000 before 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 typecheck clean, pnpm changeset:check passes, 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 __static constants, so tags resolve the same at render time as in plain TSX.

Hoisting runs only when collect_hoistable_tags says every component tag is safe at module init (imports, earlier const/class, function declarations; member tags only when the root is an import). Tags that use this/super, locals, let/var, enums/namespaces, late bindings, or <Screen.Tag> inside the same class stay inline—fixing TDZ and late-assigned Menu.Item cases (#204). scope_names treats all switch cases as one block, so a tag in a later case is not hoisted against an outer binding (#1160).

Components inside another function or a namespace body get a local helper 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 outer Foo.

Hoisted trees run lower_static_script_bodies so raw <script> in expression containers or props keeps JSON/text (#845). Generated spread_props temporaries 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.

jonkwheeler and others added 8 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]>
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
jonkwheeler merged commit 964899b 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#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
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-core: static JSX hoisting misses bindings from an earlier switch case and a dotted namespace's own name

1 participant