Skip to content

Commit 7952bd0

Browse files
committed
fix: consider variables with synthetic store sub as state
1 parent 44a833f commit 7952bd0

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

.changeset/giant-waves-act.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: consider variables with synthetic store sub as state

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ export function analyze_component(root, source, options) {
351351
}
352352
}
353353

354+
// if we are creating a synthetic binding for a let declaration we should also declare
355+
// the declaration as state in case it's reassigned
356+
if (
357+
declaration !== null &&
358+
declaration.kind === 'normal' &&
359+
declaration.declaration_kind === 'let'
360+
)
361+
declaration.kind = 'state';
362+
354363
const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
355364
binding.references = references;
356365
instance.scope.references.set(name, references);

packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
import { run as run_1 } from 'svelte/legacy';
33
4-
let count = 0;
4+
let count = $state(0);
55
let run = true;
66
run_1(() => {
77
console.log(count);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
export let store;
3+
4+
let currentStore;
5+
function update(){
6+
currentStore = store
7+
}
8+
</script>
9+
10+
<button on:click={update}></button>
11+
<p>{$currentStore}</p>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { flushSync } from 'svelte';
2+
import { ok, test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target, window }) {
6+
const [btn1, btn2] = target.querySelectorAll('button');
7+
const p = target.querySelector('p');
8+
9+
assert.equal(p?.innerHTML, '');
10+
11+
flushSync(() => {
12+
btn2.click();
13+
});
14+
15+
assert.equal(p?.innerHTML, '1');
16+
17+
flushSync(() => {
18+
btn1.click();
19+
});
20+
21+
assert.equal(p?.innerHTML, '1');
22+
23+
flushSync(() => {
24+
btn2.click();
25+
});
26+
27+
assert.equal(p?.innerHTML, '2');
28+
}
29+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import { writable } from 'svelte/store'
3+
import Test from './Test.svelte'
4+
let counter = 1
5+
let store = writable(counter)
6+
</script>
7+
8+
<button on:click={() => store = writable(++counter)}></button>
9+
<Test {store} />

0 commit comments

Comments
 (0)