Repository navigation
fix(vue): destructure keyed @for array patterns and leave types alone in keyed slots - #1135
Conversation
A keyed `@for` renders through `<VaporFor>`, whose keyed slot receives the item as a shallow ref, so the slot read each pattern name as a member of `__vapor_item.value`. For an array pattern that meant `item[0]` and `item[1]`: an inner `Set` gave `undefined` and `'😀x'` gave surrogate halves (tsrx-org#938). A default anywhere in the pattern made the loop fall back to unkeyed array output instead. When the item pattern contains an array pattern, the slot destructures the item with the authored pattern inside a `computed`, which runs once per item value and keeps the row reactive, and reads each name from it. Object patterns keep their property reads. Fixes tsrx-org#938 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The keyed `<VaporFor>` slot rewrites each loop param reference to read `.value`, and it treated every identifier spelled like a loop param as a reference, types included: `function <item>(x: item)` became `function <item.value>(x: item.value)`, which compiles without an error but fails the Vue build (tsrx-org#922). Skip TypeScript type syntax in that rewrite. A `typeof item` in a type still reads the param, so it keeps reading its value. Fixes tsrx-org#922 Co-Authored-By: Claude Opus 5.5 <[email protected]>
| // A default in the pattern reads the index through its ref too. | ||
| b.const( | ||
| rewrite_vapor_for_keyed_slot_refs(loop_params[0], loop_params.slice(1)), | ||
| create_value_member_expression(item_ref), |
There was a problem hiding this comment.
Keyed slot mutates shared item pattern
Medium Severity
rewrite_vapor_for_keyed_slot_refs walks loop_params[0] in place, so a default that reads the index is rewritten to i.value on the original pattern. getKey later clones that same pattern, but it still receives a plain number, so a missing element can yield undefined as the key and collapse distinct rows.
Reviewed by Cursor Bugbot for commit 1ab555f. Configure here.
The keyed slot that destructures an array pattern asked for `computed`
through the helper that adds a specifier to the first `from 'vue'`
import. After an authored `import type { Ref } from 'vue'` the specifier
was erased with the type import and the build called an unbound
`__vapor_computed`; after `import * as Vue from 'vue'` the output was a
syntax error. It now gets an import declaration of its own.
The generated `computed` and bindings names use the compiler's reserved
`_$_` prefix, so an authored name can't collide with them. The changeset
no longer claims single-use items work: the key callback destructures
the item as well, so a generator item can't be keyed by a name its
pattern binds.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The keyed slot's rewrite skipped type annotations, parameters, and references, but not the declarations: in the slot body, `type item = string` and an interface that mentions `item` had their names rewritten to `item.value`, and `typeof Types.item` rewrote its member name. Type aliases and interfaces are now type syntax for the rewrite, and the right side of a qualified name is never a reference to a loop param. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every keyed slot that destructures an array pattern named its computed
`_$_vapor_bindings`, so a name an outer loop rewrote inside an inner
loop's slot read the inner computed (`${k}-${a}-${b}` rendered
`undefined-x-X`). Each slot's computed now has a name of its own.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d import
The keyed slot's `computed` import goes first, and `ensure_named_import`
looked only at the first declaration from a source. With an authored
`import { Suspense } from 'vue'`, it added `Suspense` to the `computed`
declaration as well, and the module declared it twice ("Identifier
'Suspense' has already been declared"). It now returns when any
declaration from the source already imports the name as a value, and
otherwise adds it to the first one as before, so an authored
`import type` or `import * as` from 'vue' still never takes it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit c3a85cf. Configure here.
… import
`ensure_named_import` skipped type-only specifiers when it looked for an
existing binding, but still picked the first declaration from the source
to add one to. With an authored `import type { Ref } from 'vue'` and no
keyed slot `computed` import, `Suspense` went into the type-only import
and was erased; `import * as Vue from 'vue'` printed
`import * as Vue{ Suspense }`; and `import type { Suspense } from 'vue'`
became `import type { Suspense, Suspense }`. Type-only and namespace
declarations are now skipped entirely, so the binding joins a named value
import or gets an import of its own.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
…ests This branch imports Solid's Show, For, Switch, Match, Errored and Loading and Vue's VaporFor under `__` names. Tests main added since (the static @if fallbacks from tsrx-org#1129, the @{ } block bodies from tsrx-org#1128, the memoized @switch, the untracked @Try body, and the keyed-array @for from tsrx-org#1135) matched the public names; they now match the private ones. The memoized @switch also imports `createMemo` from solid-js, so the generated import no longer fits on one line and the printer wraps it. The shared generated-import check and the @switch memo test now read an import across lines. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
…e names (#1222) * fix(solid,vue): import generated control-flow components privately Solid imported Show, For, Switch, Match, Errored and Loading from solid-js under their public names, and Vue imported VaporFor the same way. An authored binding with one of those names either collided with the generated import (another module's import, a type-only import) or replaced the generated element (a module or setup local, or a `@for (const Match of rows)` loop variable). Import them as __Show, __For, __Switch, __Match, __Errored, __Loading and __VaporFor, the way #1175 imports Suspense, Fragment and TsrxErrorBoundary. Solid's compiler treats the aliased tags as plain components, and vue-jsx-vapor compiles <__VaporFor> exactly like <VaporFor> in DOM, SSR, HMR and interop output. defineVaporComponent keeps its public name: vue-jsx-vapor's macros, HMR registration, SSR registration and interop compile all find components by that callee name. * fix(vue): report authored defineVaporComponent bindings (TSRX2032) vue-jsx-vapor finds components by calls to `defineVaporComponent`, so the compiler's import of it can't take a private name the way VaporFor now does. An authored binding of that name either collided with the generated import (an import from another module) or replaced it (a module function or const), and vue-jsx-vapor's macros treat every call of the name as a component definition anyway. Report each such binding with a new code, TSRX2032: an import from anywhere but `vue-jsx-vapor`, a module function, const or class, and a local or parameter in any scope. In collect and editor mode a module-level binding keeps the name, and the compiler adds no import that would declare it again. A type-only import of `defineVaporComponent` from `vue-jsx-vapor` itself becomes the value import the components need. The error has an entry in the error tables, a row in the specification and an example the content mapper checks through TypeScript 7, which now covers Vue as well as React and Solid. * fix(core): treat any tag not starting with a-z as a component The previous commit named Solid's generated control-flow components `__Show`, `__Switch`, `__Loading` and so on. The compiler counted a tag as a component only when it started with A-Z, so those looked like host tags. Solid vetoes hoisting a subtree that renders a component, and without the veto a static `@if`, `@switch` or `@pending` subtree went to one module-level node: a component rendered twice showed one copy. Decide as JSX does instead: a tag is a host element only when its first character is a lowercase ASCII letter. Compiled on every .tsrx file in the repo, React, Preact, Hono and Vue output is byte-identical before and after, and Solid output matches #1175's apart from the private names. * fix(vue): cover the TSRX2032 edge cases - `declare function defineVaporComponent` declares the name, so it is reported too. - After any TSRX2032 the compiler adds no `defineVaporComponent` import. A `var` in a top-level block hoists to module scope, so the import redeclared it (TS2440) even though the binding is not at module level. - A namespace of types only binds no value, so it is not reported. - A quoted specifier, `{ "defineVaporComponent" as defineVaporComponent }` from `vue-jsx-vapor`, is the compiler's own import: it is not reported, and the compiler reuses it instead of adding the name again. * docs(changeset): note the component-name rule * test: expect the private Solid and Vue control-flow names in main's tests This branch imports Solid's Show, For, Switch, Match, Errored and Loading and Vue's VaporFor under `__` names. Tests main added since (the static @if fallbacks from #1129, the @{ } block bodies from #1128, the memoized @switch, the untracked @Try body, and the keyed-array @for from #1135) matched the public names; they now match the private ones. The memoized @switch also imports `createMemo` from solid-js, so the generated import no longer fits on one line and the printer wraps it. The shared generated-import check and the @switch memo test now read an import across lines. * test(vue): import defineVaporComponent from vue-jsx-vapor in the props params test Main's #1231 added a runtime test that imports `defineVaporComponent` from `vue` and calls it directly, so vue-jsx-vapor's props macro sees the patterns as written. This branch reports every binding of that name other than an import from `vue-jsx-vapor` (TSRX2032), because the compiler imports it from there for each component it wraps and a second binding would collide. Import it from `vue-jsx-vapor`, as the compiler does; the macro still finds the calls by name. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46


Summary
Part of the
@forsemantics series (spec in #1132). This PR fixes the keyed<VaporFor>slot on Vue for rule 2: loop bindings followfor...ofrules and stay scope-correct.A keyed slot receives the item as a shallow ref, so the compiler rewrites each loop binding in the slot body to read
.value:@for (const [first, second] of rows; key first)read__vapor_item.value[0]and[1]. An innerSetgaveundefined, and'😀x'split into surrogate halves. A default anywhere in the pattern made the loop fall back to unkeyed array output, so rows lost their state on reorder. After: when the item pattern contains an array pattern, the slot destructures the item with the authored pattern inside acomputedand reads each name from it. Thecomputedruns once per item value and keeps the row reactive, so in-place updates still reach the row. Object patterns keep their property reads, and their output is unchanged.computedcomes from an import declaration of its own (import { computed as _$_vapor_computed } from 'vue'), so an authoredimport type { Ref } from 'vue'orimport * as Vue from 'vue'stays valid. A name the compiler adds to avueimport, such asSuspensefor@pending, reuses an authored value import of it from any declaration. The generated names use the compiler's reserved_$_prefix, and each slot's computed is numbered (_$_vapor_bindings_N), so an outer loop's names read inside a nested keyed loop resolve to the outer slot's computed.Exception to spec rule 2 (bindings follow
for...ofrules):getKeydestructures the item as well as the slot, so a keyed array pattern destructures each item twice. Re-iterable items (arrays, Sets, strings) are unaffected, but an item that can be iterated only once, such as a generator, can't be keyed by a name its pattern binds, and a default with side effects runs twice. The changeset says so.function <item>(x: item) {}in the body becamefunction <item.value>(x: item.value). That compiled without errors and then failed the Vue build. After: the rewrite skips TypeScript type syntax, including type alias and interface declarations (type item = string,interface Row { label: item }), and never rewrites the member name in a qualified name (typeof Types.item). Atypeof itemin a type still reads the param, as before.This PR does not touch the non-
VaporForfallback that #1079 changes.Verification
tsrx-vuecompile tests for an array pattern with a default ([first, second = 'none']), for thecomputedimport besideimport type,import * as, and mixed type/value imports fromvue, for aSuspenseimport beside thecomputedimport and four authoredvueimports, and for type parameters, type annotations,astypes,typeof, type aliases, interfaces, andtypeof Types.itemin a keyed slot. Each fails before its fix.tsrx-vue-runtime(Vue vapor in jsdom): a keyed loop over[new Set(['a', 'Alpha']), '😀x', ['b']]with[id, label = 'none']rendersAlpha,x, andnone. Row state survives a reverse, and renaming an item in place updates its row. Onmainthe row state is lost after the reverse.tsrx-vue-runtime: nested keyed loops that destructure ([k, list]around[a, b]) renderp-x-X; before the per-slot names they renderedundefined-x-X.tsrx-tscon Vue: the destructured names keep their types, and a planted mistake is reported at its source position.npx vitest run --testTimeout=60000with 10012 passed, 82 skipped, 0 failed. After the first review fixes: the compiler, runtime, and vite-plugin projects for all five targets, plustsrx-utilsandcontent-mapper(--testTimeout=60000, 92 files): 6175 passed, 65 skipped, 0 failed. After the second round:tsrx-utilsand the Solid and Vue compiler, runtime, vite, bun, and rspack plugin suites (52 files): 3156 passed, 31 skipped, 0 failed. After c3a85cf: the Vue projects, 799 passed, 15 skipped;tscfor tsrx-vue and Prettier are clean.pnpm typecheck: clean.prettier --checkon the changed files: clean.pnpm changeset:check: passes.Related
Fixes #938
Fixes #922
Cursor Bugbot
rewrite_vapor_for_keyed_slot_refsmutating the item pattern isn't a bug: zimmerframe'swalkcopies each node it changes, sogetKeykeeps the authored pattern. With[a = () => i, b]andkey a(),getKeycompiles to([a = () => i, b], i) => a()and the slot's computed to() => i.value, and the rows render0:x,1:y,2:zwith keys0,1,2. A default that reads the index directly,[a = 'r' + i], throwsCannot access 'i' before initializationbecause the index is a later parameter, but main's.mapcallback throws the same way on Vue and React, so that isn't this PR's.computedimport was right: with an authoredimport { Suspense } from 'vue', a keyed array pattern and a@try … @pending,ensure_named_importaddedSuspenseto the newcomputeddeclaration too, and the module failed in Vite withIdentifier 'Suspense' has already been declared. It now looks through every import fromvuefor the name before adding it, so the authored import is reused; an authoredimport typeorimport * asfromvuestill never takes it (c3a85cf).ensure_named_importadding a value import to a type-only declaration was right for loops without a keyed slot: with an authoredimport type { Ref } from 'vue',@try … @pendingputSuspenseinto that import, where the build erased it, andimport * as Vue from 'vue'printedimport * as Vue{ Suspense }, as on main. It now skips type-only and namespace imports, as fix(core): join a dynamic class with scope classes as each target renders it #1232 does, soSuspensejoins a named value import or gets one of its own (45d8eb5), and this PR now merges with fix(core): join a dynamic class with scope classes as each target renders it #1232 without a conflict. An authored import that already binds the nameSuspense(import type { Suspense },import Suspense from 'vue') still collides with the generated import, as it does on main; that needs the generated import renamed.🤖 Generated with Claude Code
Note
Medium Risk
Changes core Vue
@forlowering and AST rewriting for keyed slots; incorrect ref/type rewriting or import merging could break compiles or runtime keyed lists, though coverage is extensive.Overview
Keyed Vue
@forloops that use an array destructuring pattern (e.g.[first, second]) now follow realfor...ofsemantics instead of indexing__vapor_item.value[0]. The compiler wraps pattern bindings in a per-slot Vuecomputed(_$_vapor_bindings_N), emits a dedicatedimport { computed as _$_vapor_computed } from 'vue', and keeps nested keyed loops scoped with separate binding computeds so Sets, strings, and defaults behave correctly while<VaporFor>row state is preserved.The loop-variable
.valuerewrite in keyed slots skips TypeScript type syntax (type params, aliases, interfaces, qualified type members), fixing build breaks when a loop name collides with type names (#922).Import injection is tightened: helper imports are not merged into
import typeorimport * asfromvue, and existing value imports (e.g. Suspense) are reused instead of duplicated.Reviewed by Cursor Bugbot for commit 45d8eb5. Bugbot is set up for automated code reviews on this repo. Configure here.