Repository navigation
fix(runtime): walk @for sources the way for...of does - #1132
Merged
Merged
Conversation
`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]>
This was referenced Oct 26, 2026
jonkwheeler
marked this pull request as ready for review
October 27, 2026 11:41
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]>
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
On React, Preact, and Hono,
@fornow reads its source the wayfor...ofdoes. One difference stays as before: the loop pulls the next item before it renders the current one, so it knows which item is last.Symbol.iteratorrendered its indexed items, and an empty array whose iterator yields items rendered@empty. After: the loop renders what the iterator yields.Symbol.iteratorgetters ([Bug]: @for reads Symbol.iterator getters twice, changing rows or throwing #273). Before:map_iterableread the getter twice, so a getter-backed iterable rendered its second snapshot, or threwsource is not iterableif the getter worked only once. After: one read, likefor...of.@foroverstring[] | number[]failedtsrx-tscwith TS2345, becausemap_iterableinferred one item type from the first union member. After: the loop type-checks anditemisstring | number.Changes:
map_iterableandmap_iterable_asyncreadSymbol.iteratoronce, take the indexed array path only while the iterator isArray.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 withIterationValue.@forsemantics across targetsThis is the first of several PRs that bring
@forto the same rules on every target. The spec they follow:@foraccepts any iterable. It evaluates the source once and readsSymbol.iteratoronce.for...ofrules.@emptyrenders when the loop yielded zero items.This PR covers rule 1 (and rule 5 for custom iterators) on the
map_iterabletargets. It also adds a shared runtime fixture (@tsrx/core/test-harness/runtime/for-of-sources) that renders@forover 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@emptypath 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
VaporForslots: #938, #922), and #1136 (#181, #649).Verification
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 bySymbol.iterator; sync and async) anditerable-types.test.js(union sources). Each failed onmainfor the reported reason before the fix.main(custom iterators and the getter) and passes after the fix on React, Preact, and Hono.tsrx-tsc(Preact): exit 2 with TS2345 onmain, exit 0 after. React, Solid, and Vue also pass the same union source.npx vitest run --testTimeout=60000: 10065 passed, 82 skipped, 4 failed. All 4 are load-related on a machine running 10 suites in parallel: threevite-plugin-reacttypecheck fixtures hit their own 30 s limit, and onevscode-plugingrammar test threw in a TextMate tokenizer. Rerun alone, all 41 tests in those files pass. The same process-spawning tests also time out onmainunder this load. The stacked Solid branch contains this PR's commits and passed the full suite with 0 failures.pnpm typecheck: clean.prettier --checkon the changed files: clean.pnpm changeset:check: passes.Related
Fixes #506
Fixes #273
Fixes #507
🤖 Generated with Claude Code