Repository navigation
fix(compiler): declare the host ref/spread binding in every element position - #758
Merged
trueadm merged 1 commit intoAug 15, 2026
Conversation
…osition
A host element carrying `ref` plus a JSX spread lowers to a generated
`let X = __normalize_spread_props_for_ref_attr(bag)` that rides on the
element's metadata for a later pass to hoist. Only the render-block
statement builder and the native-directive path hoist it, so the same
element in plain-JS expression position — a ternary arm, a concise arrow
body, a declarator init, a callback body, an attribute value, an array
element, or plain JS nested in a native template — kept the references and
lost the declaration. Editors and tsrx-tsc then reported TS2304 on source
the runtime compiles and runs correctly.
Such an element also reached both lowering sites, and the lowering is not
idempotent: the second pass read the composed `ref={[authored, X.ref]}`
array as an authored ref and composed it again, emitting a second binding
and a ref nested one level deeper than the runtime ever produces.
Patch @tsrx/core to wrap residual declarations in the IIFE the
native-directive path already uses, and to mark an element whose host
ref/spread has been lowered so the dispatch does not lower it twice. Both
are scoped to the type-only path, so runtime emit for the other platforms
sharing the transform is unchanged.
Cover the position matrix in the volar suite, and add a repo-wide scan so
no .tsrx file can reference a generated name it never declares. Packages
typechecked through tsgo cannot catch this class: tsgo does not read .tsrx.
The regenerated lockfile hash in the parity audits and the evals manifest
follows from the new patch hash.
trueadm
approved these changes
Aug 14, 2026
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
A host element carrying
refplus a JSX spread lowers, in the type-only path, to a generatedlet X = __normalize_spread_props_for_ref_attr(bag)with the attributes rewritten to readX. The declaration rides on the element's metadata for a later pass to hoist, and only two consumers hoist it: the render-block statement builder and the native-directive path. An element anywhere else kept the references and lost the declaration, so editors andtsrx-tscreported TS2304 on source that compiles and runs correctly.Such an element also reached both lowering sites — the walker's
JSXOpeningElementvisitor andtransform_element_attributes_dispatch— and the lowering is not idempotent: the second pass read the composedref={[authored, X.ref]}array as an authored ref and composed it again, emitting a second binding and a ref nested one level deeper than the runtime ever produces.merge_duplicate_refsrebuilds the mergedrefwithout thesynthetic_refmarker, so the attribute list could no longer answer "already lowered".Beyond the positions in the issue, the same defect covers an attribute value, an array-literal element, an element with two spreads,
<text {...bag} ref={bag.ref} />, and plain JS nested inside a native@{ … }template — the issue notes the native directive path works, but only the directive does.Closes #737.
Why
refplus a spread on a host element is ordinary authoring. Nothing indocs/differences-from-react.mdor.rulesync/rules/tsrx-authoring.mdrestricts where such an element may appear, and the authoring rule documentsrefas a plain prop with no positional constraint. The runtime compiles every position correctly, so the divergence was confined to the type-only print reached throughoctane/compiler/volar.Rewriting the three affected visx components to hoist the element into a
returnwould hide a compiler defect behind app changes and leave the position broken for every consumer.Changes
patches/@[email protected]— mark an opening element whose host ref/spread has been lowered and have the attribute dispatch honour that marker; wrap residual setup declarations at the walker'sJSXElementpath in the samewrap_jsx_setup_declarationsIIFE the native-directive path already emits. Both changes are scoped totransform_context.typeOnly, so runtime emit for the other platforms sharing this transform is unchanged.pnpm-workspace.yaml— patch rationale, following the existing convention in that block including the "remove once upstream ships it" note.packages/octane/tests/compiler/volar.test.ts— the position matrix, one test asserting no TS2304 from a realtsprogram over the generated TSX, and one asserting the composedrefis a flat array.scripts/check-virtual-tsx-declarations.mjs+virtual-tsx:check+ a CI step — scans every.tsrxin the repo for a generated name referenced without a declaration. Packages typechecked throughtsgocannot catch this class, becausetsgodoes not read.tsrxat all.lockfileSha256in 73audit/react-parity.jsonfiles and the evals manifest follows from the new patch hash. That line is their only change.Validation
Both new tests were confirmed to fail without the fix: 25 TS2304 diagnostics and a nested ref array respectively. The guard names the three visx files and exits 1 with the fix inert.
pnpm format:check— viapnpm format:files:checkon the changed filespnpm typecheck— not run in full.pnpm typecheck:filesreports 4 pre-existing errors involar.test.ts(TS7016 on theoctane/compiler/volarimport, three TS7006); the identical set is present on the stashed HEAD version, so this diff adds none.pnpm test— run twice; fails both times, confined topackages/octane/tests/browser/native-change/native-change.test.ts(12 tests,page.waitForFunctiontimeouts rooted in a Vite504 (Outdated Optimize Dep)). The same file fails on cleanorigin/mainwith this patch absent, so it is pre-existing and not caused by this change. Failure count varied 14 / 12 / 1 across identical runs, and clearing the Vite optimizer caches did not change it. Everything else passed: 2192 files, 21590 tests.vitest --project octane(441 files, 7118 tests),volar.test.ts(17),virtual-tsx:check(2139 files),tsrx-tsc -p packages/visx(TS2304 7 → 0), visxcompile:check(257 components, client + server) andtypes:check,ci:workflow:test(108),changeset:checkNo changeset:
packages/octane/srcis untouched, and a pnpm patch never ships in the tarball, so consumer-visible package behavior is unchanged.Risk / follow-ups
@tsrx/coreships the equivalent fix. Thepnpm-workspace.yamlnote records that.@tsrx/react's runtime emit, which is why thetypeOnlyscoping matters here and why this is worth fixing upstream. Its transform on the concise-arrow position produces<text {...A__spread_props2} ref={__mergeRefs(A__spread_props2.ref, __mergeRefs(p.r, A__spread_props1.ref))} />, where neither name is declared — aReferenceErrorwhen that module runs, not just an editor diagnostic. Filing upstream is a maintainer call, not done here..tsrxfiles: 8.06 / 8.06 / 8.09s with it against 7.77 / 7.91 / 7.92s without — roughly 2%, and nothing on runtime compile.ref={[authored, X.ref]}reads.refoff the normalized bag unconditionally. When the spread bag's type declares norefthat is TS2339; when it declaresrefas optional the array element isT | undefinedandOctane.Ref<T>admits noundefined, so it is TS2322. Both fire in every position, including the ones that always worked. The repo's bindings spread wide React-style attribute bags and do not trip it; a narrow bag like{ x?: number }trips it every time.tsrx-tscerrors (was 31 with 7 TS2304, now 31 with 0). Fixing this un-masked two the undeclaredanyhad been hiding:Projection.tsrx:187TS2345 andText.tsrx:37TS2551. None of it is gated — rootpnpm typechecksends visx throughtsgo, which reads no.tsrx, so that package's type surface is unchecked in CI. Moving it totsrx-tscneeds those errors resolved first and is deliberately out of scope here.Provenance
agent-authored)Note
Medium Risk
Touches shared compiler transform via pnpm patch (type-only path); wrong scoping could affect IDE typings or double-lowering behavior, though runtime emit is explicitly unchanged.
Overview
Fixes virtual TSX diagnostics where host elements with
refplus a spread compile at runtime but editors/tsrx-tscreport TS2304 because the type-only lowering dropped the generated spread/ref binding outside render-block and native-directive hoisting paths.The
@tsrx/corepnpm patch (type-only scoped) marks elements after host ref/spread lowering, skips double-lowering in attribute dispatch, and wraps residual setup declarations in the samewrap_jsx_setup_declarationsIIFE used elsewhere so bindings are declared in ternaries, concise arrows, callbacks, native templates, etc.Adds
scripts/check-virtual-tsx-declarations.mjs,pnpm virtual-tsx:check, and a CI step that scans all.tsrxvirtual TSX for referenced-but-undeclared generated names.volar.test.tsgains a position matrix with TS2304 and single-level ref-array assertions. Lockfile hash updates in parity audit JSON and evals manifest follow the patch hash change.Reviewed by Cursor Bugbot for commit cca0bde. Bugbot is set up for automated code reviews on this repo. Configure here.