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

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

* fix: avoid calling checks if we are in runes mode
pull/14260/head
Paolo Ricciuti 5 months ago committed by GitHub
parent d207666ec3
commit 7bc94b9984
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: do not treat reassigned synthetic binds as state in runes mode

@ -276,6 +276,8 @@ export function analyze_component(root, source, options) {
/** @type {Template} */ /** @type {Template} */
const template = { ast: root.fragment, scope, scopes }; const template = { ast: root.fragment, scope, scopes };
let synthetic_stores_legacy_check = [];
// create synthetic bindings for store subscriptions // create synthetic bindings for store subscriptions
for (const [name, references] of module.scope.references) { for (const [name, references] of module.scope.references) {
if (name[0] !== '$' || RESERVED.includes(name)) continue; if (name[0] !== '$' || RESERVED.includes(name)) continue;
@ -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 // we push to the array because at this moment in time we can't be sure if we are in legacy
// the declaration as state in case it's reassigned // mode yet because we are still changing the module scope
if ( synthetic_stores_legacy_check.push(() => {
declaration !== null && // if we are creating a synthetic binding for a let declaration we should also declare
declaration.kind === 'normal' && // the declaration as state in case it's reassigned and we are not in runes mode (the function will
declaration.declaration_kind === 'let' && // not be called if we are not in runes mode, that's why there's no !runes check here)
declaration.reassigned if (
) { declaration !== null &&
declaration.kind = 'state'; declaration.kind === 'normal' &&
} declaration.declaration_kind === 'let' &&
declaration.reassigned
) {
declaration.kind = 'state';
}
});
const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic'); const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
binding.references = references; binding.references = references;
@ -373,6 +380,12 @@ export function analyze_component(root, source, options) {
const runes = options.runes ?? Array.from(module.scope.references.keys()).some(is_rune); 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) { if (runes && root.module) {
const context = root.module.attributes.find((attribute) => attribute.name === 'context'); const context = root.module.attributes.find((attribute) => attribute.name === 'context');
if (context) { if (context) {

@ -0,0 +1,7 @@
import { test } from '../../test';
export default test({
async test({ logs, assert }) {
assert.deepEqual(logs, ['world']);
}
});

@ -0,0 +1,13 @@
<script>
import { writable } from 'svelte/store';
let style = writable('world');
$effect(() => {
console.log($style);
});
function init(){
style = writable('svelte');
}
</script>
Loading…
Cancel
Save