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

no warning on contextual-store if declaring it as a parameter / variable #6008

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* In custom elements, call `onMount` functions when connecting and clean up when disconnecting ([#1152](https://github.com/sveltejs/svelte/issues/1152), [#2227](https://github.com/sveltejs/svelte/issues/2227), [#4522](https://github.com/sveltejs/svelte/pull/4522))
* Do not emit `contextual-store` warnings for function parameters or declared variables ([#6008](https://github.com/sveltejs/svelte/pull/6008))

## 3.32.3

Expand Down
16 changes: 9 additions & 7 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ export default class Component {
return this.skip();
}

component.warn_on_undefined_store_value_references(node, parent, scope);
component.warn_on_undefined_store_value_references(node, parent, prop, scope);
},

leave(node: Node) {
Expand Down Expand Up @@ -843,7 +843,7 @@ export default class Component {
});
}

warn_on_undefined_store_value_references(node, parent, scope: Scope) {
warn_on_undefined_store_value_references(node: Node, parent: Node, prop: string, scope: Scope) {
if (
node.type === 'LabeledStatement' &&
node.label.name === '$' &&
Expand All @@ -855,7 +855,7 @@ export default class Component {
});
}

if (is_reference(node as Node, parent as Node)) {
if (is_reference(node, parent)) {
const object = get_object(node);
const { name } = object;

Expand All @@ -865,10 +865,12 @@ export default class Component {
}

if (name[1] !== '$' && scope.has(name.slice(1)) && 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)'
});
if (!((/Function/.test(parent.type) && prop === 'params') || (parent.type === 'VariableDeclarator' && prop === 'id'))) {
this.error(node as any, {
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
25 changes: 25 additions & 0 deletions test/runtime/samples/store-shadow-scope-declaration/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script>
function test(store) {
// allow declaring $store as parameter
// it's not referring to the store value of the
// `store` variable in the upper scope
return derived(store, $store => {

});
}

function test2(store) {
// allow declaring the `$store` variable
// it is not referring to the store value of the `store` variable
let $store;
}
</script>

<div
on:test={(store) => {
derived(store, $store => {});
}}
on:test2={(store) => {
let $store;
}}
/>