Repository navigation
fix(core): keep var bindings from inactive platform branches (#146) - #199
Merged
Merged
Conversation
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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Merged
This was referenced Sep 23, 2026
This branch was successfully deployed
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.
Fixes #146.
Problem
Platform specialization replaces
if (import.meta.env.platform.<name>)with the selected branch and drops the other branch. That also dropped anyvarbindings the discarded branch hoists to the enclosing function or module. Compiled forios:became
export { value };, which fails withExport 'value' is not defined. Inside a function, readingvaluethrew aReferenceError, or silently read an outer binding with the same name. This affected all five targets because they sharespecializePlatform. In the editor, it showed up asCannot find name 'value'.Fix
The discarded branch's code is still dropped, but it now leaves behind a
vardeclaration with no initializer and no source location for each name it hoists:This matches plain JavaScript with the flag replaced by
false: the binding exists and readsundefined. Details:vardeclarations in blocks,if/else,for/for…in/for…ofheads and bodies,while,do, labels,try/catch/finally, andswitchcases. Destructured names are included.let/constare not collected.if (web) { var x = 1 } else { var x = 2 }compiles unchanged apart from the dropped branch.{ var hoisted; <selected> }. The declaration is kept out of the selected block, becauseif (web) { let v } else { var v }is valid JS, but{ let v; var v; }is not.vardeclaration takes the place of the oldEmptyStatement. This is valid in single-statement slots (label: var x;,while (c) var x;).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-anyerror (TS7005). Before this change it gotCannot find name.varis accepted in.tsrxfiles: 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 nestedifare included. Functions, classes, arrows, andlet/constare excluded. Also checks that the synthesized declaration has no initializers and no locations.packages/tsrx/tests/shared/compile.js, run for every target:All 8 new tests fail on
mainand pass with the fix. Also ran:tsrx-utils,tsrx-analyze,tsrx-{react,preact,solid,vue,hono},turbopack-plugin-react,vite-plugin-hono, andbun-plugin-honotest projects (3575 passed)pnpm typecheck,pnpm format:check, andpnpm changeset:checkios,valueandread()areundefined; onweb, they are'web'and1🤖 Generated with Claude Code
Note
Medium Risk
Changes shared AST platform specialization used by all compile targets; incorrect
varcollection 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 removesvarnames that branch would hoist to the module or enclosing function.specialize_nodenow collectsvaridentifiers from the discarded arm (blocks, loops,try/switch, destructuring, etc., but not nested functions/classes orlet/const), skips names the kept branch already declares, and emitsvar 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 runtimeReferenceErrorwhen only the inactive platform arm declaredvalue, and clears editorCannot find namefor 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.