Skip to content

Commit

Permalink
fix: consider variables with synthetic store sub as state (#14195)
Browse files Browse the repository at this point in the history
Fixes #14183
  • Loading branch information
paoloricciuti authored Nov 7, 2024
1 parent 1eed645 commit ea0d80e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
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} />

0 comments on commit ea0d80e

Please sign in to comment.