From c2f6ea3321e46e077083f5048754e0d0d02c62b5 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sat, 20 Feb 2021 09:33:27 +0800 Subject: [PATCH] no warning on contextual-store if declaring it as a parameter / variable --- src/compiler/compile/Component.ts | 16 ++++++------ .../store-shadow-scope-declaration/_config.js | 1 + .../main.svelte | 25 +++++++++++++++++++ 3 files changed, 35 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/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; + }} +/>