Skip to content

Commit 7bc94b9

Browse files
fix: do not treat reassigned synthetic binds as state in runes mode (#14236)
* fix: do not treat reassigned synthetic binds as state in runes mode * fix: avoid calling checks if we are in runes mode
1 parent d207666 commit 7bc94b9

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

.changeset/bright-papayas-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: do not treat reassigned synthetic binds as state in runes mode

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ export function analyze_component(root, source, options) {
276276
/** @type {Template} */
277277
const template = { ast: root.fragment, scope, scopes };
278278

279+
let synthetic_stores_legacy_check = [];
280+
279281
// create synthetic bindings for store subscriptions
280282
for (const [name, references] of module.scope.references) {
281283
if (name[0] !== '$' || RESERVED.includes(name)) continue;
@@ -351,16 +353,21 @@ export function analyze_component(root, source, options) {
351353
}
352354
}
353355

354-
// if we are creating a synthetic binding for a let declaration we should also declare
355-
// the declaration as state in case it's reassigned
356-
if (
357-
declaration !== null &&
358-
declaration.kind === 'normal' &&
359-
declaration.declaration_kind === 'let' &&
360-
declaration.reassigned
361-
) {
362-
declaration.kind = 'state';
363-
}
356+
// we push to the array because at this moment in time we can't be sure if we are in legacy
357+
// mode yet because we are still changing the module scope
358+
synthetic_stores_legacy_check.push(() => {
359+
// if we are creating a synthetic binding for a let declaration we should also declare
360+
// the declaration as state in case it's reassigned and we are not in runes mode (the function will
361+
// not be called if we are not in runes mode, that's why there's no !runes check here)
362+
if (
363+
declaration !== null &&
364+
declaration.kind === 'normal' &&
365+
declaration.declaration_kind === 'let' &&
366+
declaration.reassigned
367+
) {
368+
declaration.kind = 'state';
369+
}
370+
});
364371

365372
const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
366373
binding.references = references;
@@ -373,6 +380,12 @@ export function analyze_component(root, source, options) {
373380

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

383+
if (!runes) {
384+
for (let check of synthetic_stores_legacy_check) {
385+
check();
386+
}
387+
}
388+
376389
if (runes && root.module) {
377390
const context = root.module.attributes.find((attribute) => attribute.name === 'context');
378391
if (context) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
async test({ logs, assert }) {
5+
assert.deepEqual(logs, ['world']);
6+
}
7+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
4+
let style = writable('world');
5+
6+
$effect(() => {
7+
console.log($style);
8+
});
9+
10+
function init(){
11+
style = writable('svelte');
12+
}
13+
</script>

0 commit comments

Comments
 (0)