Skip to content

fix(core): keep var bindings from inactive platform branches (#146) - #199

Merged
leonidaz merged 1 commit into
mainfrom
fix/platform-var-hoist-146
Sep 23, 2026
Merged

leonidaz merged 1 commit into
mainfrom
fix/platform-var-hoist-146

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #146.

Problem

Platform specialization replaces if (import.meta.env.platform.<name>) with the selected branch and drops the other branch. That also dropped any var bindings the discarded branch hoists to the enclosing function or module. Compiled for ios:

if (import.meta.env.platform.web) { var value = 'web'; }
export { value };

became export { value };, which fails with Export 'value' is not defined. Inside a function, reading value threw a ReferenceError, or silently read an outer binding with the same name. This affected all five targets because they share specializePlatform. In the editor, it showed up as Cannot find name 'value'.

Fix

The discarded branch's code is still dropped, but it now leaves behind a var declaration with no initializer and no source location for each name it hoists:

var value;

export { value };

This matches plain JavaScript with the flag replaced by false: the binding exists and reads undefined. Details:

  • Names come from var declarations in blocks, if/else, for/for…in/for…of heads and bodies, while, do, labels, try/catch/finally, and switch cases. Destructured names are included.
  • Nested functions, arrows, classes (including static blocks), and let/const are not collected.
  • Names the selected branch already declares are skipped, so if (web) { var x = 1 } else { var x = 2 } compiles unchanged apart from the dropped branch.
  • If there is both a selected branch and something to hoist, the replacement is a synthesized block: { var hoisted; <selected> }. The declaration is kept out of the selected block, because if (web) { let v } else { var v } is valid JS, but { let v; var v; } is not.
  • If there is no selected branch, the var declaration takes the place of the old EmptyStatement. This is valid in single-statement slots (label: var x;, while (c) var x;).
  • The declaration has no type annotation. In the editor, TypeScript then types the variable by control flow as undefined, which is its value on the configured platform. Copying an annotation from the discarded branch would instead produce "used before being assigned" errors, and could name types that only existed in that branch. One remaining gap: a closure that reads such a variable still gets TypeScript's implicit-any error (TS7005). Before this change it got Cannot find name.

var is accepted in .tsrx files: at module level, in functions, and in @{ } component bodies. The scope analyzer already hoists it through block scopes.

Tests

  • packages/tsrx/tests/utils/platform.test.js: which names are collected, with an exact expected list. Loop heads, destructuring, try/switch/labels, and nested if are included. Functions, classes, arrows, and let/const are excluded. Also checks that the synthesized declaration has no initializers and no locations.
  • packages/tsrx/tests/shared/compile.js, run for every target:
    • module-level and function-level bindings are kept on the inactive platform
    • only the names the selected branch lacks are declared
    • the editor's virtual TSX for the issue's example has no TypeScript errors

All 8 new tests fail on main and pass with the fix. Also ran:

  • the tsrx-utils, tsrx-analyze, tsrx-{react,preact,solid,vue,hono}, turbopack-plugin-react, vite-plugin-hono, and bun-plugin-hono test projects (3575 passed)
  • pnpm typecheck, pnpm format:check, and pnpm changeset:check
  • the issue's runtime check on all five targets: on ios, value and read() are undefined; on web, they are 'web' and 1

🤖 Generated with Claude Code


Note

Medium Risk
Changes shared AST platform specialization used by all compile targets; incorrect var collection or ordering could introduce duplicate declarations or subtle scope bugs.

Overview
When compile-time platform specialization drops an inactive if (import.meta.env.platform.*) branch, it no longer removes var names that branch would hoist to the module or enclosing function.

specialize_node now collects var identifiers from the discarded arm (blocks, loops, try/switch, destructuring, etc., but not nested functions/classes or let/const), skips names the kept branch already declares, and emits var name; without initializers or source locations. If both hoisting and a kept statement apply, it wraps them as { var …; <selected> } so lexical rules stay valid.

This fixes export { value } and runtime ReferenceError when only the inactive platform arm declared value, and clears editor Cannot find name for the same pattern. New unit and cross-target compile tests cover hoisting breadth, deduplication, and virtual TSX typing.

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

Platform specialization replaced an exact `if (import.meta.env.platform.*)`
with its selected branch and dropped the other one, along with the `var`
bindings that branch hoists to the enclosing function or module. Compiling
for another platform left `export { value }` pointing at an undeclared name
and made reads throw a ReferenceError or resolve to an outer binding.

The discarded branch now contributes a location-free `var` declaration,
without initializers, for each name it hoists that the selected branch does
not already declare. Nested functions, classes, and lexical declarations are
not collected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 23, 2026 9:46pm UTC
tsrx-website-mcp Ready Ready Preview Sep 23, 2026 9:46pm UTC

Request Review

This branch was successfully deployed

2 active deployments
Preview – tsrx-website — 29e6d8d1 Deployed Sep 23, 2026 by vercel[bot]
Preview – tsrx-website-mcp — 29e6d8d1 Deployed Sep 23, 2026 by vercel[bot]
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]: Inactive platform guards drop hoisted var bindings and break exports

1 participant