fix: generate correct code for `each` blocks with async body (#16923)

* fix: generate correct code for `each` blocks with async body

* fix: else branch of `each` block

* chore: add expected html
pull/16607/merge
Paolo Ricciuti 2 days ago committed by GitHub
parent 7f8e60fd8a
commit 297afd0578
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: generate correct code for `each` blocks with async body

@ -32,7 +32,9 @@ export function EachBlock(node, context) {
each.push(b.let(node.index, index));
}
each.push(.../** @type {BlockStatement} */ (context.visit(node.body)).body);
const new_body = /** @type {BlockStatement} */ (context.visit(node.body)).body;
each.push(...(node.body.metadata.has_await ? [create_async_block(b.block(new_body))] : new_body));
const for_loop = b.for(
b.declaration('let', [
@ -55,7 +57,7 @@ export function EachBlock(node, context) {
b.if(
b.binary('!==', b.member(array_id, 'length'), b.literal(0)),
b.block([open, for_loop]),
fallback
node.fallback.metadata.has_await ? create_async_block(fallback) : fallback
)
);
} else {

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
mode: ['async']
});

@ -0,0 +1 @@
<!--[--><!--[--><!--[--><!---->each<!--]--><!--]--> <!--[--><!--[!--><!---->else<!--]--><!--]--><!--]-->

@ -0,0 +1,11 @@
{#each { length: 1 }}
{@const data = await Promise.resolve("each")}
{data}
{/each}
{#each { length: 0 }}
should not see this
{:else}
{@const data = await Promise.resolve("else")}
{data}
{/each}
Loading…
Cancel
Save