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

pull/6026/head
Tan Li Hau 4 years ago committed by GitHub
parent 8ea4fb1106
commit b764374b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -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) {
@ -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 === '$' &&
@ -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;
@ -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)'
});
}
}
}
}

@ -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;
}}
/>
Loading…
Cancel
Save