Skip to content

Commit 4773b0f

Browse files
committed
feat: shadowed store
1 parent 2e0566e commit 4773b0f

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/compiler/compile/Component.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ export default class Component {
836836
});
837837
}
838838

839-
warn_on_undefined_store_value_references(node, parent, scope) {
839+
warn_on_undefined_store_value_references(node, parent, scope: Scope) {
840840
if (
841841
node.type === 'LabeledStatement' &&
842842
node.label.name === '$' &&
@@ -852,8 +852,17 @@ export default class Component {
852852
const object = get_object(node);
853853
const { name } = object;
854854

855-
if (name[0] === '$' && !scope.has(name)) {
856-
this.warn_if_undefined(name, object, null);
855+
if (name[0] === '$') {
856+
if (!scope.has(name)) {
857+
this.warn_if_undefined(name, object, null);
858+
}
859+
860+
if (scope.find_owner(name.slice(1)) !== this.instance_scope) {
861+
this.error(node, {
862+
code: `contextual-store`,
863+
message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
864+
});
865+
}
857866
}
858867
}
859868
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
solo: true,
4+
};
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+
5+
function foo() {
6+
let store = 1;
7+
$store = 2;
8+
}
9+
</script>

0 commit comments

Comments
 (0)