Skip to content

Commit 812d66c

Browse files
committed
fix: do not treat reassigned synthetic binds as state in runes mode
1 parent 4bcd01b commit 812d66c

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed
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: 21 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((/** @type {boolean} */ runes) => {
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
361+
if (
362+
declaration !== null &&
363+
declaration.kind === 'normal' &&
364+
declaration.declaration_kind === 'let' &&
365+
declaration.reassigned &&
366+
!runes
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,10 @@ export function analyze_component(root, source, options) {
373380

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

383+
for (let check of synthetic_stores_legacy_check) {
384+
check(runes);
385+
}
386+
376387
if (runes && root.module) {
377388
const context = root.module.attributes.find((attribute) => attribute.name === 'context');
378389
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)