fix: throw error when auto-subscribed store variable shadow by local variable (#11170)

pull/11162/head
Tan Li Hau 3 months ago committed by GitHub
parent f90639d89c
commit 9b67ee18eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: throw error when auto-subscribed store variable shadow by local variable

@ -302,8 +302,8 @@ export function analyze_component(root, source, options) {
declaration.initial.source.value === 'svelte/store'
))
) {
let is_nested_store_subscription = false;
for (const reference of references) {
let is_nested_store_subscription_node = undefined;
search: for (const reference of references) {
for (let i = reference.path.length - 1; i >= 0; i--) {
const scope =
scopes.get(reference.path[i]) ||
@ -311,14 +311,17 @@ export function analyze_component(root, source, options) {
instance.scopes.get(reference.path[i]);
if (scope) {
const owner = scope?.owner(store_name);
is_nested_store_subscription =
!!owner && owner !== module.scope && owner !== instance.scope;
if (!!owner && owner !== module.scope && owner !== instance.scope) {
is_nested_store_subscription_node = reference.node;
break search;
}
break;
}
}
}
if (is_nested_store_subscription) {
error(references[0].node, 'illegal-store-subscription');
if (is_nested_store_subscription_node) {
error(is_nested_store_subscription_node, 'illegal-store-subscription');
}
if (options.runes !== false) {

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'illegal-store-subscription',
message: 'Cannot subscribe to stores that are not declared at the top level of the component',
position: [142, 147]
}
});

@ -0,0 +1,8 @@
<script>
import { writable } from 'svelte/store';
const user = writable(0);
const users = [];
$: selected = users.find(user => user.id == $user);
$: console.log($user);
</script>

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
error: {
code: 'illegal-store-subscription',
message: 'Cannot subscribe to stores that are not declared at the top level of the component',
position: [167, 172]
}
});

@ -0,0 +1,8 @@
<script>
import { writable } from 'svelte/store';
const user = writable(0);
const users = [];
$: console.log($user);
$: selected = users.find(user => user.id == $user);
</script>
Loading…
Cancel
Save