Skip to content

Commit 2b95e0c

Browse files
committed
fix: ensure await block scope transforms are isolated
1 parent 48f0bfc commit 2b95e0c

File tree

8 files changed

+85
-7
lines changed

8 files changed

+85
-7
lines changed

.changeset/tough-pans-boil.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: ensure await block scope transforms are isolated

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,34 @@ export function AwaitBlock(node, context) {
1818
let catch_block;
1919

2020
if (node.then) {
21-
const argument = node.value && create_derived_block_argument(node.value, context);
21+
const then_context = {
22+
...context,
23+
state: { ...context.state, transform: { ...context.state.transform } }
24+
};
25+
const argument = node.value && create_derived_block_argument(node.value, then_context);
2226

2327
/** @type {Pattern[]} */
2428
const args = [b.id('$$anchor')];
2529
if (argument) args.push(argument.id);
2630

2731
const declarations = argument?.declarations ?? [];
28-
const block = /** @type {BlockStatement} */ (context.visit(node.then));
32+
const block = /** @type {BlockStatement} */ (then_context.visit(node.then, then_context.state));
2933

3034
then_block = b.arrow(args, b.block([...declarations, ...block.body]));
3135
}
3236

3337
if (node.catch) {
34-
const argument = node.error && create_derived_block_argument(node.error, context);
38+
const catch_context = { ...context, state: { ...context.state } };
39+
const argument = node.error && create_derived_block_argument(node.error, catch_context);
3540

3641
/** @type {Pattern[]} */
3742
const args = [b.id('$$anchor')];
3843
if (argument) args.push(argument.id);
3944

4045
const declarations = argument?.declarations ?? [];
41-
const block = /** @type {BlockStatement} */ (context.visit(node.catch));
46+
const block = /** @type {BlockStatement} */ (
47+
catch_context.visit(node.catch, catch_context.state)
48+
);
4249

4350
catch_block = b.arrow(args, b.block([...declarations, ...block.body]));
4451
}

packages/svelte/tests/migrate/samples/self-closing-elements/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
<div title="preserve"></div>
33
<input type="text" />
44
<hr />
5-
<f:table></f:table>
5+
<f:table></f:table>

packages/svelte/tests/migrate/samples/svelte-self-name-conflict/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
child
2020
</Output_1>
2121
<Output_1></Output_1>
22-
{/if}
22+
{/if}

packages/svelte/tests/migrate/samples/svelte-self/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
child
1919
</Output>
2020
<Output></Output>
21-
{/if}
21+
{/if}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import "svelte/internal/disclose-version";
2+
import * as $ from "svelte/internal/client";
3+
4+
function increment(_, counter) {
5+
counter.count += 1;
6+
}
7+
8+
var root = $.template(`<button> </button> <!> `, 1);
9+
10+
export default function Await_block_scope($$anchor) {
11+
let counter = $.proxy({ count: 0 });
12+
const promise = $.derived(() => Promise.resolve(counter));
13+
var fragment = root();
14+
var button = $.first_child(fragment);
15+
16+
button.__click = [increment, counter];
17+
18+
var text = $.child(button);
19+
20+
$.reset(button);
21+
22+
var node = $.sibling(button, 2);
23+
24+
$.await(node, () => $.get(promise), null, ($$anchor, counter) => {});
25+
26+
var text_1 = $.sibling(node);
27+
28+
$.template_effect(() => {
29+
$.set_text(text, `clicks: ${counter.count ?? ""}`);
30+
$.set_text(text_1, ` ${counter.count ?? ""}`);
31+
});
32+
33+
$.append($$anchor, fragment);
34+
}
35+
36+
$.delegate(["click"]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as $ from "svelte/internal/server";
2+
3+
export default function Await_block_scope($$payload) {
4+
let counter = { count: 0 };
5+
const promise = Promise.resolve(counter);
6+
7+
function increment() {
8+
counter.count += 1;
9+
}
10+
11+
$$payload.out += `<button>clicks: ${$.escape(counter.count)}</button> <!---->`;
12+
$.await(promise, () => {}, (counter) => {}, () => {});
13+
$$payload.out += `<!----> ${$.escape(counter.count)}`;
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
let counter = $state({ count: 0 });
3+
const promise = $derived(Promise.resolve(counter))
4+
5+
function increment() {
6+
counter.count += 1;
7+
}
8+
</script>
9+
10+
<button onclick={increment}>
11+
clicks: {counter.count}
12+
</button>
13+
14+
{#await promise then counter}{/await}
15+
16+
{counter.count}

0 commit comments

Comments
 (0)