Skip to content

fix(vue): destructure keyed @for array patterns and leave types alone in keyed slots - #1135

Merged
jonkwheeler merged 7 commits into
tsrx-org:mainfrom
jonkwheeler:fix/for-vue-vapor-for
Nov 12, 2026
Merged

jonkwheeler merged 7 commits into
tsrx-org:mainfrom
jonkwheeler:fix/for-vue-vapor-for

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part of the @for semantics series (spec in #1132). This PR fixes the keyed <VaporFor> slot on Vue for rule 2: loop bindings follow for...of rules 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:

  • Array patterns ([Bug]: Vue keyed @for treats array destructuring as indexed access and renders incorrect values #938). Before: @for (const [first, second] of rows; key first) read __vapor_item.value[0] and [1]. An inner Set gave undefined, 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 a computed and reads each name from it. The computed runs 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. computed comes from an import declaration of its own (import { computed as _$_vapor_computed } from 'vue'), so an authored import type { Ref } from 'vue' or import * as Vue from 'vue' stays valid. A name the compiler adds to a vue import, such as Suspense for @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...of rules): getKey destructures 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.

  • Type names ([Bug]: Vue keyed @for rewrites generic type parameters into item.value and breaks builds #922). Before: the rewrite treated every identifier spelled like a loop param as a reference, so function <item>(x: item) {} in the body became function <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). A typeof item in a type still reads the param, as before.

This PR does not touch the non-VaporFor fallback that #1079 changes.

Verification

  • tsrx-vue compile tests for an array pattern with a default ([first, second = 'none']), for the computed import beside import type, import * as, and mixed type/value imports from vue, for a Suspense import beside the computed import and four authored vue imports, and for type parameters, type annotations, as types, typeof, type aliases, interfaces, and typeof Types.item in 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'] renders Alpha, x, and none. Row state survives a reverse, and renaming an item in place updates its row. On main the row state is lost after the reverse.
  • tsrx-vue-runtime: nested keyed loops that destructure ([k, list] around [a, b]) render p-x-X; before the per-slot names they rendered undefined-x-X.
  • tsrx-tsc on Vue: the destructured names keep their types, and a planted mistake is reported at its source position.
  • First round: full npx vitest run --testTimeout=60000 with 10012 passed, 82 skipped, 0 failed. After the first review fixes: the compiler, runtime, and vite-plugin projects for all five targets, plus tsrx-utils and content-mapper (--testTimeout=60000, 92 files): 6175 passed, 65 skipped, 0 failed. After the second round: tsrx-utils and 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; tsc for tsrx-vue and Prettier are clean.
  • pnpm typecheck: clean. prettier --check on the changed files: clean. pnpm changeset:check: passes.

Related

Fixes #938
Fixes #922

Cursor Bugbot

  • Bugbot's comment on rewrite_vapor_for_keyed_slot_refs mutating the item pattern isn't a bug: zimmerframe's walk copies each node it changes, so getKey keeps the authored pattern. With [a = () => i, b] and key a(), getKey compiles to ([a = () => i, b], i) => a() and the slot's computed to () => i.value, and the rows render 0:x, 1:y, 2:z with keys 0, 1, 2. A default that reads the index directly, [a = 'r' + i], throws Cannot access 'i' before initialization because the index is a later parameter, but main's .map callback throws the same way on Vue and React, so that isn't this PR's.
  • Bugbot's comment on the computed import was right: with an authored import { Suspense } from 'vue', a keyed array pattern and a @try … @pending, ensure_named_import added Suspense to the new computed declaration too, and the module failed in Vite with Identifier 'Suspense' has already been declared. It now looks through every import from vue for the name before adding it, so the authored import is reused; an authored import type or import * as from vue still never takes it (c3a85cf).
  • Its later comment on ensure_named_import adding a value import to a type-only declaration was right for loops without a keyed slot: with an authored import type { Ref } from 'vue', @try … @pending put Suspense into that import, where the build erased it, and import * as Vue from 'vue' printed import * 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, so Suspense joins 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 name Suspense (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 @for lowering 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 @for loops that use an array destructuring pattern (e.g. [first, second]) now follow real for...of semantics instead of indexing __vapor_item.value[0]. The compiler wraps pattern bindings in a per-slot Vue computed (_$_vapor_bindings_N), emits a dedicated import { 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 .value rewrite 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 type or import * as from vue, 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.

jonkwheeler and others added 2 commits October 26, 2026 15:23
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]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

// 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),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 1ab555f. Configure here.

jonkwheeler and others added 2 commits October 27, 2026 08:08
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]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/tsrx-vue/src/transform.js Resolved
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]>
@jonkwheeler
jonkwheeler marked this pull request as ready for review October 27, 2026 12:57
…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

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Fix All in Cursor

❌ 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.

Comment thread packages/tsrx-vue/src/transform.js Resolved
… 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
@jonkwheeler
jonkwheeler merged commit 3603935 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
…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
jonkwheeler added a commit that referenced this pull request Nov 12, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant