Skip to content

Commit 0ce79c2

Browse files
committed
fix: allow await in {@const } in more blocks
1 parent 97f263c commit 0ce79c2

File tree

6 files changed

+47
-9
lines changed

6 files changed

+47
-9
lines changed

.changeset/red-phones-brake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: allow `await` in `{@const }` in more blocks

packages/svelte/src/compiler/phases/3-transform/client/visitors/AwaitBlock.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ export function AwaitBlock(node, context) {
3737
const declarations = argument?.declarations ?? [];
3838
const block = /** @type {BlockStatement} */ (then_context.visit(node.then, then_context.state));
3939

40-
then_block = b.arrow(args, b.block([...declarations, ...block.body]));
40+
then_block = b.arrow(
41+
args,
42+
b.block([...declarations, ...block.body]),
43+
node.then.metadata.has_await
44+
);
4145
}
4246

4347
if (node.catch) {
@@ -53,7 +57,11 @@ export function AwaitBlock(node, context) {
5357
catch_context.visit(node.catch, catch_context.state)
5458
);
5559

56-
catch_block = b.arrow(args, b.block([...declarations, ...block.body]));
60+
catch_block = b.arrow(
61+
args,
62+
b.block([...declarations, ...block.body]),
63+
node.catch.metadata.has_await
64+
);
5765
}
5866

5967
context.state.init.push(
@@ -63,7 +71,11 @@ export function AwaitBlock(node, context) {
6371
context.state.node,
6472
expression,
6573
node.pending
66-
? b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.pending)))
74+
? b.arrow(
75+
[b.id('$$anchor')],
76+
/** @type {BlockStatement} */ (context.visit(node.pending)),
77+
node.pending.metadata.has_await
78+
)
6779
: b.null,
6880
then_block,
6981
catch_block

packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,16 @@ export function EachBlock(node, context) {
326326
b.literal(flags),
327327
thunk,
328328
key_function,
329-
b.arrow(render_args, b.block(declarations.concat(block.body)))
329+
b.arrow(render_args, b.block(declarations.concat(block.body)), node.body.metadata.has_await)
330330
];
331331

332332
if (node.fallback) {
333333
args.push(
334-
b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.fallback)))
334+
b.arrow(
335+
[b.id('$$anchor')],
336+
/** @type {BlockStatement} */ (context.visit(node.fallback)),
337+
node.fallback.metadata.has_await
338+
)
335339
);
336340
}
337341

packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ export function IfBlock(node, context) {
1515
const consequent = /** @type {BlockStatement} */ (context.visit(node.consequent));
1616
const consequent_id = b.id(context.state.scope.generate('consequent'));
1717

18-
statements.push(b.var(consequent_id, b.arrow([b.id('$$anchor')], consequent)));
18+
statements.push(
19+
b.var(
20+
consequent_id,
21+
b.arrow([b.id('$$anchor')], consequent, node.consequent.metadata.has_await)
22+
)
23+
);
1924

2025
let alternate_id;
2126

2227
if (node.alternate) {
2328
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
2429
alternate_id = b.id(context.state.scope.generate('alternate'));
25-
statements.push(b.var(alternate_id, b.arrow([b.id('$$anchor')], alternate)));
30+
statements.push(
31+
b.var(alternate_id, b.arrow([b.id('$$anchor')], alternate, node.alternate.metadata.has_await))
32+
);
2633
}
2734

2835
const { has_await } = node.metadata.expression;

packages/svelte/src/compiler/phases/3-transform/client/visitors/KeyBlock.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export function KeyBlock(node, context) {
1818
const body = /** @type {Expression} */ (context.visit(node.fragment));
1919

2020
let statement = add_svelte_meta(
21-
b.call('$.key', context.state.node, key, b.arrow([b.id('$$anchor')], body)),
21+
b.call(
22+
'$.key',
23+
context.state.node,
24+
key,
25+
b.arrow([b.id('$$anchor')], body, node.fragment.metadata.has_await)
26+
),
2227
node,
2328
'key'
2429
);

packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ export function SvelteBoundary(node, context) {
8686
block.body.unshift(...const_tags);
8787

8888
const boundary = b.stmt(
89-
b.call('$.boundary', context.state.node, props, b.arrow([b.id('$$anchor')], block))
89+
b.call(
90+
'$.boundary',
91+
context.state.node,
92+
props,
93+
b.arrow([b.id('$$anchor')], block, node.fragment.metadata.has_await)
94+
)
9095
);
9196

9297
context.state.template.push_comment();

0 commit comments

Comments
 (0)