Skip to content

fix(vue): lower non-VaporFor @for loops through map_iterable - #1079

Merged
jonkwheeler merged 5 commits into
tsrx-org:mainfrom
cestercian:fix/vue-for-iterable
Nov 18, 2026
Merged

jonkwheeler merged 5 commits into
tsrx-org:mainfrom
cestercian:fix/vue-for-iterable

Conversation

@cestercian

@cestercian cestercian commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1068

On Vue, a @for that wasn't a single-element body got lowered to array methods on the source: source.length === 0 ? ... : source.map(...) for @empty and statement bodies, and source.flatMap(...) when the body can skip a row. So a Set, Map or generator threw props.items.map is not a function at render.

What changed:

  • Vue now has a forOfIterableHelper like the other targets: @tsrx/vue/runtime/iterable (and @tsrx/vue-runtime/iterable for direct runtime imports), both just re-exporting @tsrx/runtime/iterable
  • inject_vue_imports adds the map_iterable import when it's used, since Vue has its own import hook
  • the skip-a-row path calls map_iterable instead of .flatMap. It already flattens a returned array, so [] / [<li/>] behaves the same
  • single-element bodies still use <VaporFor>, no change there
  • updated the forOfIterableHelper doc comment in jsx-platform.d.ts, which said Vue leaves it unset
  • exported MAP_ITERABLE_INTERNAL_NAME from @tsrx/core so Vue uses the same local name the shared lowering does
  • patch changeset for core, vue and vue-runtime

Tests: new cases in tsrx-vue/tests/basic.test.js covering @empty, skip-a-row and statement bodies over a Set, VaporFor staying for the simple case, and the direct runtime import path. One existing assertion now expects __map_iterable(items, ...) instead of .map(...). The tsrx-vue suite passes, and so does full pnpm test except the eslint-plugin suites, which fail the same way on main for me (they can't resolve @tsrx/eslint-parser). I also checked that both new export paths resolve and map a Set in node. Prettier and pnpm changeset:check are clean.

Didn't touch the Solid side (#1070), that one is separate.


Note

Medium Risk
Changes Vue compile output for common @for patterns and adds runtime import paths; behavior shift is localized to list rendering but affects many templates.

Overview
Fixes Vue @for lowering so loops that are not handled by <VaporFor> (for example @empty, bodies that can skip rows, or statement bodies) use map_iterable / map_iterable_async instead of .map, .flatMap, or .length, so Set, Map, generators, and other iterables render without runtime errors.

The Vue compiler wires in a forOfIterableHelper platform path (@tsrx/vue/runtime/iterable and @tsrx/vue-runtime/iterable for direct runtime imports), both re-exporting @tsrx/runtime/iterable. inject_vue_imports adds the helper imports when needed. Simple single-expression bodies still compile to <VaporFor> unchanged.

MAP_ITERABLE_* internal names are exported from @tsrx/core for consistent naming. Tests cover compile output, direct runtime imports, async @for, and Vite runtime cases over Set; shared iterable source tests skip cases where VaporFor still cannot honor custom iterators.

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

Fixes #863

Vue had no forOfIterableHelper, so a @for with @empty or a statement body was
lowered to source.length / source.map, and a body that can skip a row to
source.flatMap. A Set, Map, generator or other iterable has none of those, so
the component threw on render.

Give Vue the map_iterable helper the other targets use, exported as
@tsrx/vue/runtime/iterable and @tsrx/vue-runtime/iterable, and use it for the
skip-a-row lowering too (it flattens a returned array like flatMap). A
single-element body still renders through VaporFor.

Fixes tsrx-org#1068
@cestercian

Copy link
Copy Markdown
Contributor Author

hey @leonidaz, any chance you could take a look at this when you get a sec? happy to adjust anything.

@jonkwheeler

Copy link
Copy Markdown
Contributor

Thanks for this! I checked it against current main, and it fixes #1068:

  • A Set, a Map iterator, a generator and a string all render through @empty, a body that skips a row, and a body with statements.
  • That holds in jsdom, in SSR, and in the editor types.
  • A ref(new Set()) updates on add and clear.

Using the same map_iterable path as the other targets is the right call, and #1147 can build on it directly.

It needs a rebase and a few changes:

  1. Rebase on main.

    • Conflicts: packages/tsrx-vue/src/transform.js, packages/tsrx/src/index.js and packages/tsrx/types/jsx-platform.d.ts conflict, all on adjacent lines. Keep both sides of the import and export lists.
    • Solid comment: in the forOfIterableHelper comment, keep main's Solid sentence. Solid sets this option now (fix(solid): render @for over any iterable and let its key read the index #1134).
    • One test needs updating: after the rebase, keeps VaporFor for a single-element body fails, because main imports VaporFor as __VaporFor (fix(solid,vue): import generated control-flow components under private names #1222). Expect '<__VaporFor in={props.items}>'.
    • The rebase also fixes an import bug. It picks up main's ensure_named_import, which stops map_iterable from being added to an existing import type or import * as from the same module. The current branch does that.
  2. Import map_iterable_async too.

    • What changes: once Vue sets forOfIterableHelper, the shared pass turns a @for body that contains await into await __map_iterable_async(...). But inject_vue_imports only imports map_iterable.
    • Components are fine: they still reject await.
    • Plain async functions are now broken. Main reports TSRX2021 for this; with this branch it compiles with no errors and throws ReferenceError: __map_iterable_async is not defined at runtime:
      async function rows(items: Set<string>) {
        const out = @{
          @for (const item of items) {
            const v = await Promise.resolve(item.toUpperCase());
            <li>{v}</li>
          }
        };
        return out;
      }
      
    • The fix: import map_iterable_async when needs_for_of_iterable_async is set, the way inject_try_imports does, and add a compile test. Alternatively, keep reporting TSRX2021 here if Vue shouldn't support this.
  3. Add a runtime test. The new tests only check the compiled string.

  4. This also fixes [Bug]: Vue @for with @empty evaluates its source twice and renders the wrong batch #863. @empty used to read the source twice; now it's one __map_iterable(source, …) call.

    • Before and after: with @for (const item of nextBatch()), main renders the second batch. This branch renders the first and calls nextBatch once.
    • Please add Fixes #863 and a test like that.
  5. README: add @tsrx/vue-runtime/iterable to the "Available subpaths" list in packages/tsrx-vue-runtime/README.md, like the React and Solid runtime READMEs.

One behavior change to be aware of (not blocking):

  • Why it happens: a Vue reactive array has its own Symbol.iterator, so it now takes map_iterable's read-ahead path instead of .map. That path pulls item i+1 before it runs the body for item i, so it knows which item is last.
  • What changes: if the body writes to a later item while rendering, the loop shows the old value. With items = reactive([1, 2]) and a body that sets items[1] = 3 on the first item, main and for...of give [1, 3], but this branch gives [1, 2].
  • How much it matters: that's a side effect in render, so it should be rare. If you want to match for...of exactly, one option is the indexed path for any Array.isArray source, since Vue's proxy tracks index reads too.

Optional:

  • render_for_of_as_flat_map doesn't call flatMap any more, so a new name might read better.

Keep both sides of the import and export lists, and keep main's Solid
sentence on forOfIterableHelper. Expect __VaporFor after the import rename.
A plain async function that awaits inside @for compiles to
await __map_iterable_async, so the Vue import hook has to pull that
helper in the same way it pulls map_iterable. ensure_named_import keeps
it off an existing import type or import * as from the same module.
…read

Render a Set through @empty, a skipped row, and a statement body, and
update a ref(Set) on add and clear. @for (const item of nextBatch())
renders the first batch and calls nextBatch once. The shared for-of
fixture runs on Vue, with the three VaporFor cases left for tsrx-org#1147.
@cestercian

Copy link
Copy Markdown
Contributor Author

thanks for the super detailed review @jonkwheeler! merged main in (kept both sides of the imports, Solid comment from main, __VaporFor test updated), map_iterable_async is imported now with a compile test, added runtime tests incl the ref(Set) case and the #863 one, and the README subpath. also renamed render_for_of_as_flat_map since it no longer calls flatMap. left reactive arrays on the read-ahead path, the shared helper still walks a custom iterator on an array.

@jonkwheeler
jonkwheeler merged commit f50dd3e into tsrx-org:main Nov 18, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 18, 2026
jonkwheeler added a commit that referenced this pull request Nov 19, 2026
… as TSRX2021 (#1377)

* fix(vue, solid): report an await in a plain async function's template as TSRX2021

In a plain async function (not a component), an `await` in an element or
fragment of a `@{ … }` template compiled without an error on Vue and
Solid, and the build then failed with `await` is only allowed within
async functions: vue-jsx-vapor and babel-preset-solid move template
values into effects, getters and immediately called arrows, which aren't
async. Since #1079, an awaiting `@for` inside an element reached the same
failure on Vue (#1370).

Core now runs a target's new `findDeferredTemplateAwaits` hook on the
final output, next to `findDeferredTemplateYields` (#259), and reports
each `await` it returns as TSRX2021 at the authored `await`, in source
order. An `await` core adds for a generated closure it made async (an
`@if` or `@switch` body, or `map_iterable_async` for an awaiting `@for`)
now carries that closure in `metadata.generated_await`, so it is reported
at the authored `await`s inside. An `await` that already has an error
(a component's, or one in a callback the target calls) isn't reported
again.

- Vue: any `await` in an element or fragment whose nearest function is
  async. An awaiting `@for` that is the template's only output still
  compiles and renders, since `map_iterable_async` is awaited in the
  function itself. A host spread that awaits is evaluated in its setup
  declaration before the element, as one that yields is (#1108), so it
  builds, in the editor and the build alike.
- Solid: babel-preset-solid compiles an `await` as it compiles a
  `yield`, so the Solid finder reads `await`s with the TSRX2034 rules,
  for the DOM output and, with `ssr: true`, the SSR output too.
  `<p>{await text}</p>` stays in place and still compiles.

React, Preact and Hono don't set the hook and are unchanged.

Fixes #1370

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46


* fix(vue, solid): give a template await its own TSRX2021 message and model Solid's control-flow components

Follow-up to the review of the previous commit.

- Solid: `is_component_name` counted a tag as a component only when it
  started with a capital, but dom-expressions' `isComponent` also counts
  one that starts with something other than a letter, which includes
  TSRX's generated `__Show`, `__Switch`/`__Match`, `__ErrorBoundary` and
  `__Loading`. Their bodies were modelled as host elements: a static
  value in an `@if`, `@switch`, `@try` or `@pending` body, which
  babel-preset-solid passes in a children getter, went unreported (for
  `yield` on main, TSRX2034, and for `await`), and a static value of one
  of them in a fragment, such as `<>@if (await t) { <p>a</p> }</>`, was
  reported though it builds.
- A template `await` now has its own TSRX2021 message, worded like
  TSRX2034's: the callback message told the user to move the `await`
  into an async child component, which doesn't apply in a plain async
  function and isn't possible on Vue.
- A module's top level counts as async for both finders, so an `await`
  in JSX there (`export const el = <p>{await load()}</p>;`) is reported
  where the target's compiler moves it.

The new message gets a content-mapper error example and a line in the
specification's code list. Docs: the Solid plugins' `ssr`/`solid` option
docs name TSRX2021 beside TSRX2034, and llms.txt names Vue's awaiting
host spread.

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

2 participants