Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-waves-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: consider variables with synthetic store sub as state
11 changes: 11 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
export let store;

let currentStore;
function update(){
currentStore = store
}
</script>

<button on:click={update}></button>
<p>{$currentStore}</p>
Original file line number Diff line number Diff line change
@@ -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');
}
});
Original file line number Diff line number Diff line change
@@ -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