Skip to content

Commit 2e0566e

Browse files
committed
error on expression scope store
1 parent 2450dd1 commit 2e0566e

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/compiler/compile/nodes/shared/Expression.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ export default class Expression {
7878

7979
if (scope.has(name)) return;
8080

81-
if (name[0] === '$' && template_scope.names.has(name.slice(1))) {
82-
component.error(node, {
83-
code: `contextual-store`,
84-
message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
85-
});
81+
if (name[0] === '$') {
82+
const store_name = name.slice(1);
83+
if (template_scope.names.has(store_name) || scope.has(store_name)) {
84+
component.error(node, {
85+
code: `contextual-store`,
86+
message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
87+
});
88+
}
8689
}
8790

8891
if (template_scope.is_let(name)) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
error: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
3+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
const store = writable();
4+
</script>
5+
6+
<button
7+
on:click={(store) => {
8+
$store = Math.random();
9+
}} />

0 commit comments

Comments
 (0)