Skip to content

Commit

Permalink
fix: throw error when auto-subscribed store variable shadow by local …
Browse files Browse the repository at this point in the history
…variable (#11170)
  • Loading branch information
tanhauhau authored Apr 14, 2024
1 parent f90639d commit 9b67ee1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-kids-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: throw error when auto-subscribed store variable shadow by local variable
15 changes: 9 additions & 6 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,23 +302,26 @@ 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]) ||
module.scopes.get(reference.path[i]) ||
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
}
});
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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]
}
});
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 9b67ee1

Please sign in to comment.