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

pull/11162/head
Tan Li Hau 9 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' declaration.initial.source.value === 'svelte/store'
)) ))
) { ) {
let is_nested_store_subscription = false; let is_nested_store_subscription_node = undefined;
for (const reference of references) { search: for (const reference of references) {
for (let i = reference.path.length - 1; i >= 0; i--) { for (let i = reference.path.length - 1; i >= 0; i--) {
const scope = const scope =
scopes.get(reference.path[i]) || scopes.get(reference.path[i]) ||
@ -311,14 +311,17 @@ export function analyze_component(root, source, options) {
instance.scopes.get(reference.path[i]); instance.scopes.get(reference.path[i]);
if (scope) { if (scope) {
const owner = scope?.owner(store_name); const owner = scope?.owner(store_name);
is_nested_store_subscription = if (!!owner && owner !== module.scope && owner !== instance.scope) {
!!owner && owner !== module.scope && owner !== instance.scope; is_nested_store_subscription_node = reference.node;
break search;
}
break; 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) { 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