Skip to content

fix(core): await generated closures that hold an authored await (#145) - #212

Merged
leonidaz merged 1 commit into
mainfrom
fix/async-for-body-145
Sep 23, 2026
Merged

leonidaz merged 1 commit into
mainfrom
fix/async-for-body-145

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #145.

Problem

In an async component, an await inside a @for or @empty body compiled to invalid TSX. The loop body became a non-async map_iterable callback, and @empty became a non-async IIFE. @switch cases and @if branches with setup statements had the same problem, because they also lower to non-async IIFEs:

export async function App() {
  return __map_iterable([1], (item) => {
    return <p>{await Promise.resolve(item)}</p>; // PARSE_ERROR
  });
}

This affects every target that allows await in components: React, Preact, and the Hono server target. It lives in the shared JSX transform. Solid, Vue, and Hono DOM already reject await in components, and they still do. Editors had the same bug: tsrx-tsc reported TS1308 ("'await' expressions are only allowed within async functions") at every such await.

Fix

A final pass in createJsxTransform runs once lowering is complete. The parser already rejects await inside authored functions that aren't async. So in the output, any non-async function that directly contains an await must be a closure the compiler generated. The pass handles each one according to how it is called:

  • IIFE: (() => { … })() becomes await (async () => { … })().
  • map_iterable callback (a loop body, or the @empty thunk): the call becomes await __map_iterable_async(…) and the callback becomes async. map_iterable_async is a new helper in @tsrx/runtime/iterable, and every target's runtime/iterable re-exports it. It has the same contract as map_iterable (index, is_last, fragment flattening, tail, empty, raw iterators) and finishes one item before it starts the next.
  • Anything else: a closure the target calls during rendering, where the result can't be awaited. The only one I found is the @catch fallback. The pass reports it at the authored await ("does not support await here … Await the value in the component body, or move it into an async child component") and leaves the closure non-async.

The inserted await passes the authored one up to the enclosing closure, so nested cases compose: a @for inside a @for, or @empty, whose thunk wraps an IIFE.

Supporting details:

  • Imports are now injected after this pass and after lower_remaining_jsx_code_blocks, so the output imports only the helpers it actually uses.
  • segments.js skips AwaitExpressions without a source location, which are the ones this pass adds. Without that, compile_to_volar_mappings crashed.
  • The pass returns early when the source contains no await.

Why loops run sequentially

@for (…) { …await… } reads like a for...of loop in an async function. @if and @switch bodies also await inline, in source order. map_iterable_async keeps 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 can await 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_async ordering, is_last, flattening, empty and tail, generators and raw iterators, closing a generator on rejection, inferred types, and re-export identity.
  • Shared source-mapping harness, which revives the unused rejectsComponentAwait flag. The cases are @for, @empty, nested @for, @switch, and an @if with setup statements. On React, Preact, and Hono server, the output parses, loose Volar compiles report no errors, and every authored await keyword maps to an await in the output. On Solid, Vue, and Hono DOM, each case is rejected. It also checks the @catch diagnostic and where it points. 21 of these fail on main.
  • vite-plugin-hono: an end-to-end SSR render through the Vite plugin, which is the issue's reproduction path. It covers @for with index, @empty, @switch, and @if, and checks the sequential order.
  • Checked by hand: a Preact workspace with async @for, @empty, and @switch bodies passes tsrx-tsc, where main reports 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, and pnpm changeset:check pass.

Follow-ups (not in this PR)

  • Octane: its Volar transform uses createJsxTransform with forOfIterableHelper: 'octane/tsrx-iterable', and that module re-exports only map_iterable. Once Octane picks up this core version, an async @for body in its editor output would import a missing map_iterable_async. The fix is to add it to that re-export.
  • @try semantics: an await in a @try body 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 @pending doesn't show while it waits, and @catch doesn'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 await inside @for, @empty, @switch, or @if bodies (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_closures pass (after remaining code-block lowering, before import injection): generated IIFEs become await (async () => …)(), async @for / @empty bodies use await map_iterable_async(...), and await in non-awaitable render callbacks (e.g. @catch) is diagnosed at the authored keyword. Compiler-inserted await nodes without source locations are excluded from source-map mapping.

map_iterable_async is added to @tsrx/runtime (with types and re-exports), walking iterables one item at a time like for...of in an async function; sync map_iterable shares a to_iterable helper. 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.

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]>
@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 23, 2026 10:39pm UTC
tsrx-website-mcp Ready Ready Preview Sep 23, 2026 10:39pm UTC

Request Review

This branch was successfully deployed

2 active deployments
Preview – tsrx-website — b7b62b35 Deployed Sep 23, 2026 by vercel[bot]
Preview – tsrx-website-mcp — b7b62b35 Deployed Sep 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Await in Hono server @for and @empty bodies produces invalid TSX

1 participant