Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading