From 7952bd021b33470abf0e927f0109125446337ac1 Mon Sep 17 00:00:00 2001 From: paoloricciuti Date: Wed, 6 Nov 2024 22:43:29 +0100 Subject: [PATCH 1/3] fix: consider variables with synthetic store sub as state --- .changeset/giant-waves-act.md | 5 ++++ .../src/compiler/phases/2-analyze/index.js | 9 ++++++ .../effects-with-alias-run/output.svelte | 2 +- .../variable-assigned-store-state/Test.svelte | 11 +++++++ .../variable-assigned-store-state/_config.js | 29 +++++++++++++++++++ .../variable-assigned-store-state/main.svelte | 9 ++++++ 6 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .changeset/giant-waves-act.md create mode 100644 packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/Test.svelte create mode 100644 packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/_config.js create mode 100644 packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/main.svelte diff --git a/.changeset/giant-waves-act.md b/.changeset/giant-waves-act.md new file mode 100644 index 000000000000..591f0f25ba1e --- /dev/null +++ b/.changeset/giant-waves-act.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: consider variables with synthetic store sub as state diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index f5a31d09a779..c8223dbcc5eb 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -351,6 +351,15 @@ 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.kind = 'state'; + const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic'); binding.references = references; instance.scope.references.set(name, references); diff --git a/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte b/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte index ac26da45e5a6..f240c389971b 100644 --- a/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte +++ b/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte @@ -1,7 +1,7 @@ + + +

{$currentStore}

\ No newline at end of file diff --git a/packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/_config.js b/packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/_config.js new file mode 100644 index 000000000000..eecdcb246295 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/_config.js @@ -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'); + } +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/main.svelte b/packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/main.svelte new file mode 100644 index 000000000000..5033fbc52676 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/variable-assigned-store-state/main.svelte @@ -0,0 +1,9 @@ + + + + \ No newline at end of file From 22b6058ad3ba8c0c4191b50ab003db9ac52a0e7d Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Thu, 7 Nov 2024 10:07:13 +0100 Subject: [PATCH 2/3] fix: add `reassigned` check and curlies Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --- packages/svelte/src/compiler/phases/2-analyze/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index c8223dbcc5eb..36d8145145c7 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -356,9 +356,11 @@ export function analyze_component(root, source, options) { if ( declaration !== null && declaration.kind === 'normal' && - declaration.declaration_kind === 'let' - ) + declaration.declaration_kind === 'let' && + declaration.reassigned + ) { declaration.kind = 'state'; + } const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic'); binding.references = references; From 4f30961b699a4ad998b477b4d24bd383888db6b6 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Thu, 7 Nov 2024 10:10:20 +0100 Subject: [PATCH 3/3] fix: revert migrate test changing --- .../tests/migrate/samples/effects-with-alias-run/output.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte b/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte index f240c389971b..ac26da45e5a6 100644 --- a/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte +++ b/packages/svelte/tests/migrate/samples/effects-with-alias-run/output.svelte @@ -1,7 +1,7 @@