Skip to content

Commit

Permalink
feat: shadowed store
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Jun 30, 2020
1 parent 2e0566e commit 4773b0f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ export default class Component {
});
}

warn_on_undefined_store_value_references(node, parent, scope) {
warn_on_undefined_store_value_references(node, parent, scope: Scope) {
if (
node.type === 'LabeledStatement' &&
node.label.name === '$' &&
Expand All @@ -852,8 +852,17 @@ export default class Component {
const object = get_object(node);
const { name } = object;

if (name[0] === '$' && !scope.has(name)) {
this.warn_if_undefined(name, object, null);
if (name[0] === '$') {
if (!scope.has(name)) {
this.warn_if_undefined(name, object, null);
}

if (scope.find_owner(name.slice(1)) !== this.instance_scope) {
this.error(node, {
code: `contextual-store`,
message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
});
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/runtime/samples/store-shadow-scope/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
error: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`,
solo: true,
};
9 changes: 9 additions & 0 deletions test/runtime/samples/store-shadow-scope/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import { writable } from 'svelte/store';
const store = writable();
function foo() {
let store = 1;
$store = 2;
}
</script>

0 comments on commit 4773b0f

Please sign in to comment.