Repository navigation
fix(core): await generated closures that hold an authored await (#145) - #212
Merged
Merged
Conversation
An async component could not `await` inside `@for`, `@empty`, `@switch`, or `@if` bodies on React, Preact, and the Hono server target: lowering put the await in a generated callback or IIFE that was not async, so builds failed and editors reported TS1308. A final pass now makes those generated closures async and awaits them where they are called. `@for` bodies use a new `map_iterable_async` runtime helper that settles one item before the next. An await in a closure the target calls itself (a `@catch` fallback) is reported at the authored await. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This was referenced Sep 23, 2026
Closed
This was referenced Sep 24, 2026
This was referenced Sep 24, 2026
1 task done
This branch was successfully deployed
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.
Fixes #145.
Problem
In an async component, an
awaitinside a@foror@emptybody compiled to invalid TSX. The loop body became a non-asyncmap_iterablecallback, and@emptybecame a non-async IIFE.@switchcases and@ifbranches with setup statements had the same problem, because they also lower to non-async IIFEs:This affects every target that allows
awaitin components: React, Preact, and the Hono server target. It lives in the shared JSX transform. Solid, Vue, and Hono DOM already rejectawaitin components, and they still do. Editors had the same bug:tsrx-tscreported TS1308 ("'await' expressions are only allowed within async functions") at every suchawait.Fix
A final pass in
createJsxTransformruns once lowering is complete. The parser already rejectsawaitinside authored functions that aren't async. So in the output, any non-async function that directly contains anawaitmust be a closure the compiler generated. The pass handles each one according to how it is called:(() => { … })()becomesawait (async () => { … })().map_iterablecallback (a loop body, or the@emptythunk): the call becomesawait __map_iterable_async(…)and the callback becomes async.map_iterable_asyncis a new helper in@tsrx/runtime/iterable, and every target'sruntime/iterablere-exports it. It has the same contract asmap_iterable(index,is_last, fragment flattening,tail,empty, raw iterators) and finishes one item before it starts the next.@catchfallback. The pass reports it at the authoredawait("does not supportawaithere … Await the value in the component body, or move it into an async child component") and leaves the closure non-async.The inserted
awaitpasses the authored one up to the enclosing closure, so nested cases compose: a@forinside a@for, or@empty, whose thunk wraps an IIFE.Supporting details:
lower_remaining_jsx_code_blocks, so the output imports only the helpers it actually uses.segments.jsskipsAwaitExpressions without a source location, which are the ones this pass adds. Without that,compile_to_volar_mappingscrashed.await.Why loops run sequentially
@for (…) { …await… }reads like afor...ofloop in an async function.@ifand@switchbodies also await inline, in source order.map_iterable_asynckeeps that order: side effects run in source order, and the first rejection stops the walk. The Hono SSR test checks this with items that resolve in reverse order. If an author wants the items fetched concurrently, they canawait Promise.all(…)in setup and render the resolved data.Tests
iterable-runtime.test.js/iterable-types.test.js/runtime-package-boundaries.test.js:map_iterable_asyncordering,is_last, flattening, empty and tail, generators and raw iterators, closing a generator on rejection, inferred types, and re-export identity.rejectsComponentAwaitflag. The cases are@for,@empty, nested@for,@switch, and an@ifwith setup statements. On React, Preact, and Hono server, the output parses, loose Volar compiles report no errors, and every authoredawaitkeyword maps to anawaitin the output. On Solid, Vue, and Hono DOM, each case is rejected. It also checks the@catchdiagnostic and where it points. 21 of these fail onmain.vite-plugin-hono: an end-to-end SSR render through the Vite plugin, which is the issue's reproduction path. It covers@forwithindex,@empty,@switch, and@if, and checks the sequential order.@for,@empty, and@switchbodies passestsrx-tsc, wheremainreports four TS1308 errors. A type error inside the async loop body is still reported at the authored position.pnpm test(5084 passed),pnpm typecheck,pnpm format:check, andpnpm changeset:checkpass.Follow-ups (not in this PR)
createJsxTransformwithforOfIterableHelper: 'octane/tsrx-iterable', and that module re-exports onlymap_iterable. Once Octane picks up this core version, an async@forbody in its editor output would import a missingmap_iterable_async. The fix is to add it to that re-export.@trysemantics: anawaitin a@trybody runs in the parent component. That was already true for a body without setup statements, and bodies with setup statements now behave the same. So@pendingdoesn't show while it waits, and@catchdoesn't catch a rejection. Changing that would mean lowering the body into a generated async child component. That's a separate design question.🤖 Generated with Claude Code
Note
Medium Risk
Changes shared JSX lowering and async component semantics for several targets; loop bodies with await now run sequentially via new runtime behavior.
Overview
Fixes invalid TSX when async components use
awaitinside@for,@empty,@switch, or@ifbodies (React, Preact, Hono server). Those blocks lower to non-async generated callbacks/IIFEs, which broke builds and triggered TS1308 in editors.The JSX transform adds a late
await_generated_closurespass (after remaining code-block lowering, before import injection): generated IIFEs becomeawait (async () => …)(), async@for/@emptybodies useawait map_iterable_async(...), andawaitin non-awaitable render callbacks (e.g.@catch) is diagnosed at the authored keyword. Compiler-insertedawaitnodes without source locations are excluded from source-map mapping.map_iterable_asyncis added to@tsrx/runtime(with types and re-exports), walking iterables one item at a time likefor...ofin an async function; syncmap_iterableshares ato_iterablehelper. Docs, changeset, shared compile/mapping tests, runtime tests, and a Hono Vite SSR test cover the new behavior.Reviewed by Cursor Bugbot for commit b7b62b3. Bugbot is set up for automated code reviews on this repo. Configure here.