Repository navigation
fix(vue): lower non-VaporFor @for loops through map_iterable - #1079
Conversation
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
|
hey @leonidaz, any chance you could take a look at this when you get a sec? happy to adjust anything. |
|
Thanks for this! I checked it against current main, and it fixes #1068:
Using the same It needs a rebase and a few changes:
One behavior change to be aware of (not blocking):
Optional:
|
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.
|
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. |
… 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
Fixes #1068
On Vue, a
@forthat wasn't a single-element body got lowered to array methods on the source:source.length === 0 ? ... : source.map(...)for@emptyand statement bodies, andsource.flatMap(...)when the body can skip a row. So aSet,Mapor generator threwprops.items.map is not a functionat render.What changed:
forOfIterableHelperlike the other targets:@tsrx/vue/runtime/iterable(and@tsrx/vue-runtime/iterablefor direct runtime imports), both just re-exporting@tsrx/runtime/iterableinject_vue_importsadds themap_iterableimport when it's used, since Vue has its own import hookmap_iterableinstead of.flatMap. It already flattens a returned array, so[]/[<li/>]behaves the same<VaporFor>, no change thereforOfIterableHelperdoc comment injsx-platform.d.ts, which said Vue leaves it unsetMAP_ITERABLE_INTERNAL_NAMEfrom@tsrx/coreso Vue uses the same local name the shared lowering doesTests: new cases in
tsrx-vue/tests/basic.test.jscovering@empty, skip-a-row and statement bodies over aSet, 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 fullpnpm testexcept 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 aSetin node. Prettier andpnpm changeset:checkare clean.Didn't touch the Solid side (#1070), that one is separate.
Note
Medium Risk
Changes Vue compile output for common
@forpatterns and adds runtime import paths; behavior shift is localized to list rendering but affects many templates.Overview
Fixes Vue
@forlowering so loops that are not handled by<VaporFor>(for example@empty, bodies that can skip rows, or statement bodies) usemap_iterable/map_iterable_asyncinstead of.map,.flatMap, or.length, soSet,Map, generators, and other iterables render without runtime errors.The Vue compiler wires in a
forOfIterableHelperplatform path (@tsrx/vue/runtime/iterableand@tsrx/vue-runtime/iterablefor direct runtime imports), both re-exporting@tsrx/runtime/iterable.inject_vue_importsadds the helper imports when needed. Simple single-expression bodies still compile to<VaporFor>unchanged.MAP_ITERABLE_*internal names are exported from@tsrx/corefor consistent naming. Tests cover compile output, direct runtime imports, async@for, and Vite runtime cases overSet; shared iterable source tests skip cases whereVaporForstill 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