From b764374b6299d079db13418bbbddc58707bef59c Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Thu, 25 Feb 2021 23:53:23 +0800 Subject: [PATCH] no warning on contextual-store if declaring it as a parameter / variable (#6008) --- CHANGELOG.md | 1 + src/compiler/compile/Component.ts | 16 ++++++------ .../store-shadow-scope-declaration/_config.js | 1 + .../main.svelte | 25 +++++++++++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 test/runtime/samples/store-shadow-scope-declaration/_config.js create mode 100644 test/runtime/samples/store-shadow-scope-declaration/main.svelte diff --git a/CHANGELOG.md b/CHANGELOG.md index c44ce287c0..bd57c193de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 8aab2b4898..64e658c049 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -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)' + }); + } } } } diff --git a/test/runtime/samples/store-shadow-scope-declaration/_config.js b/test/runtime/samples/store-shadow-scope-declaration/_config.js new file mode 100644 index 0000000000..ff8b4c5632 --- /dev/null +++ b/test/runtime/samples/store-shadow-scope-declaration/_config.js @@ -0,0 +1 @@ +export default {}; diff --git a/test/runtime/samples/store-shadow-scope-declaration/main.svelte b/test/runtime/samples/store-shadow-scope-declaration/main.svelte new file mode 100644 index 0000000000..fa76d33df8 --- /dev/null +++ b/test/runtime/samples/store-shadow-scope-declaration/main.svelte @@ -0,0 +1,25 @@ + + +
{ + derived(store, $store => {}); + }} + on:test2={(store) => { + let $store; + }} +/>