Skip to content

Commit ea0d80e

Browse files
fix: consider variables with synthetic store sub as state (#14195)
Fixes #14183
1 parent 1eed645 commit ea0d80e

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,17 @@ 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+
declaration.reassigned
361+
) {
362+
declaration.kind = 'state';
363+
}
364+
354365
const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
355366
binding.references = references;
356367
instance.scope.references.set(name, references);
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)