Skip to content

fix(runtime): walk @for sources the way for...of does - #1132

Merged
jonkwheeler merged 3 commits into
tsrx-org:mainfrom
jonkwheeler:fix/for-map-iterable
Nov 12, 2026
Merged

jonkwheeler merged 3 commits into
tsrx-org:mainfrom
jonkwheeler:fix/for-map-iterable

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On React, Preact, and Hono, @for now reads its source the way for...of does. One difference stays as before: the loop pulls the next item before it renders the current one, so it knows which item is last.

Changes: map_iterable and map_iterable_async read Symbol.iterator once, take the indexed array path only while the iterator is Array.prototype[Symbol.iterator], and walk the iterator that one call returned. Their declarations infer the source type as a whole and derive the item type with IterationValue.

@for semantics across targets

This is the first of several PRs that bring @for to the same rules on every target. The spec they follow:

  1. @for accepts any iterable. It evaluates the source once and reads Symbol.iterator once.
  2. Loop bindings follow for...of rules.
  3. Only valid head forms are accepted. Any other head gets a diagnostic.
  4. The key's scope and placement are the same on every lowering path.
  5. @empty renders when the loop yielded zero items.

This PR covers rule 1 (and rule 5 for custom iterators) on the map_iterable targets. It also adds a shared runtime fixture (@tsrx/core/test-harness/runtime/for-of-sources) that renders @for over arrays, Sets, Map iterators, generators, custom array iterators, a changing iterator getter, and empty sources, with and without @empty. React, Preact, and Hono run it here. #1134, stacked on this one, runs it on Solid. Vue fails some cases today: VaporFor ignores custom array iterators and getters, and its @empty path is #1068 / #1079. It can join once #1079 lands.

Related PRs in this series: #1133 (loop-head diagnostics: #267, #453, #224), #1134 (Solid, stacked on this PR: #1070, #933), #1135 (Vue keyed VaporFor slots: #938, #922), and #1136 (#181, #649).

Verification

  • New unit tests in packages/tsrx/tests/utils/iterable-runtime.test.js (custom array iterators, array subclass, empty array with a yielding iterator, single-read getter, a non-iterable iterator returned by Symbol.iterator; sync and async) and iterable-types.test.js (union sources). Each failed on main for the reported reason before the fix.
  • The shared fixture fails 6 cases per target on main (custom iterators and the getter) and passes after the fix on React, Preact, and Hono.
  • The [Bug]: tsrx-tsc rejects @for over a union of arrays with different element types #507 repro run through tsrx-tsc (Preact): exit 2 with TS2345 on main, exit 0 after. React, Solid, and Vue also pass the same union source.
  • Full npx vitest run --testTimeout=60000: 10065 passed, 82 skipped, 4 failed. All 4 are load-related on a machine running 10 suites in parallel: three vite-plugin-react typecheck fixtures hit their own 30 s limit, and one vscode-plugin grammar test threw in a TextMate tokenizer. Rerun alone, all 41 tests in those files pass. The same process-spawning tests also time out on main under this load. The stacked Solid branch contains this PR's commits and passed the full suite with 0 failures.
  • pnpm typecheck: clean. prettier --check on the changed files: clean. pnpm changeset:check: passes.

Related

Fixes #506
Fixes #273
Fixes #507

🤖 Generated with Claude Code

jonkwheeler and others added 2 commits October 26, 2026 14:26
`map_iterable` and `map_iterable_async` took the indexed array path for
any array, so an array whose own `Symbol.iterator` filters or replaces
its items rendered the stored items, and an empty array whose iterator
yields items rendered `@empty` (tsrx-org#506). For other iterables they read
`Symbol.iterator` once to pick a path and the `for...of` walk read it
again, so a getter-backed iterator rendered its second snapshot or threw
(tsrx-org#273).

Read `Symbol.iterator` once, take the indexed path only while it is the
built-in array iterator, and walk the iterator that single call returned.

A shared runtime fixture renders `@for` over arrays, Sets, Map
iterators, generators, custom array iterators, and a changing iterator
getter, with and without `@empty`, on React, Preact, and Hono.

Fixes tsrx-org#506
Fixes tsrx-org#273

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`map_iterable` inferred one item type `T` from `Iterable<T> | Iterator<T>`,
so TypeScript took the first member of a union source and rejected the
rest: `@for` over `string[] | number[]` failed with TS2345 under
`tsrx-tsc`. Infer the source type whole and derive the item type with
`IterationValue`, which distributes over the union and yields
`string | number`, as `for...of` does.

Fixes tsrx-org#507

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The loop pulls the next item before it renders the current one, to
know which item is last, so the changeset no longer says it walks the
source exactly the way for...of does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jonkwheeler
jonkwheeler merged commit bf6d334 into tsrx-org:main Nov 12, 2026
8 checks passed
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