fix: consider variables with synthetic store sub as state (#14195)

Fixes #14183
pull/14208/head
Paolo Ricciuti 1 week ago committed by GitHub
parent 1eed645919
commit ea0d80e195
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: consider variables with synthetic store sub as state

@ -351,6 +351,17 @@ export function analyze_component(root, source, options) {
}
}
// if we are creating a synthetic binding for a let declaration we should also declare
// the declaration as state in case it's reassigned
if (
declaration !== null &&
declaration.kind === 'normal' &&
declaration.declaration_kind === 'let' &&
declaration.reassigned
) {
declaration.kind = 'state';
}
const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
binding.references = references;
instance.scope.references.set(name, references);

@ -0,0 +1,11 @@
<script>
export let store;
let currentStore;
function update(){
currentStore = store
}
</script>
<button on:click={update}></button>
<p>{$currentStore}</p>

@ -0,0 +1,29 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';
export default test({
async test({ assert, target, window }) {
const [btn1, btn2] = target.querySelectorAll('button');
const p = target.querySelector('p');
assert.equal(p?.innerHTML, '');
flushSync(() => {
btn2.click();
});
assert.equal(p?.innerHTML, '1');
flushSync(() => {
btn1.click();
});
assert.equal(p?.innerHTML, '1');
flushSync(() => {
btn2.click();
});
assert.equal(p?.innerHTML, '2');
}
});

@ -0,0 +1,9 @@
<script>
import { writable } from 'svelte/store'
import Test from './Test.svelte'
let counter = 1
let store = writable(counter)
</script>
<button on:click={() => store = writable(++counter)}></button>
<Test {store} />
Loading…
Cancel
Save