Skip to content

Commit

Permalink
fix: do not treat reassigned synthetic binds as state in runes mode (#…
Browse files Browse the repository at this point in the history
…14236)

* fix: do not treat reassigned synthetic binds as state in runes mode

* fix: avoid calling checks if we are in runes mode
  • Loading branch information
paoloricciuti authored Nov 10, 2024
1 parent d207666 commit 7bc94b9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-papayas-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: do not treat reassigned synthetic binds as state in runes mode
33 changes: 23 additions & 10 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ export function analyze_component(root, source, options) {
/** @type {Template} */
const template = { ast: root.fragment, scope, scopes };

let synthetic_stores_legacy_check = [];

// create synthetic bindings for store subscriptions
for (const [name, references] of module.scope.references) {
if (name[0] !== '$' || RESERVED.includes(name)) continue;
Expand Down Expand Up @@ -351,16 +353,21 @@ 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';
}
// we push to the array because at this moment in time we can't be sure if we are in legacy
// mode yet because we are still changing the module scope
synthetic_stores_legacy_check.push(() => {
// if we are creating a synthetic binding for a let declaration we should also declare
// the declaration as state in case it's reassigned and we are not in runes mode (the function will
// not be called if we are not in runes mode, that's why there's no !runes check here)
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;
Expand All @@ -373,6 +380,12 @@ export function analyze_component(root, source, options) {

const runes = options.runes ?? Array.from(module.scope.references.keys()).some(is_rune);

if (!runes) {
for (let check of synthetic_stores_legacy_check) {
check();
}
}

if (runes && root.module) {
const context = root.module.attributes.find((attribute) => attribute.name === 'context');
if (context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
async test({ logs, assert }) {
assert.deepEqual(logs, ['world']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { writable } from 'svelte/store';
let style = writable('world');
$effect(() => {
console.log($style);
});
function init(){
style = writable('svelte');
}
</script>

0 comments on commit 7bc94b9

Please sign in to comment.