Skip to content

Commit cca67bb

Browse files
authored
fix: support $state.snapshot as part of variable declarations (#11235)
fixes #11234
1 parent 307f15d commit cca67bb

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

.changeset/sixty-numbers-hope.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: support `$state.snapshot` as part of variable declarations

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ export const javascript_visitors_runes = {
197197
for (const declarator of node.declarations) {
198198
const init = declarator.init;
199199
const rune = get_rune(init, state.scope);
200-
if (!rune || rune === '$effect.active' || rune === '$effect.root' || rune === '$inspect') {
200+
if (
201+
!rune ||
202+
rune === '$effect.active' ||
203+
rune === '$effect.root' ||
204+
rune === '$inspect' ||
205+
rune === '$state.snapshot'
206+
) {
201207
if (init != null && is_hoistable_function(init)) {
202208
const hoistable_function = visit(init);
203209
state.hoisted.push(
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { test } from '../../test';
22

33
export default test({
4-
html: `<button>[{"a":0}]</button>`,
4+
html: `[{"a":0}] <button>[{"a":0}]</button>`,
55

66
async test({ assert, target }) {
77
const btn = target.querySelector('button');
88

99
await btn?.click();
10-
assert.htmlEqual(target.innerHTML, `<button>[{"a":0},{"a":1}]</button>`);
10+
assert.htmlEqual(target.innerHTML, `[{"a":0}] <button>[{"a":0},{"a":1}]</button>`);
1111
}
1212
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script>
22
let items = $state([{a: 0}]);
3+
let start = $state.snapshot(items);
34
</script>
45

6+
{JSON.stringify(start)}
57
<button on:click={() => items.push({a: items.length})}>{JSON.stringify(structuredClone($state.snapshot(items)))}</button>

0 commit comments

Comments
 (0)