File tree Expand file tree Collapse file tree 7 files changed +45
-8
lines changed
store-template-expression-scope Expand file tree Collapse file tree 7 files changed +45
-8
lines changed Original file line number Diff line number Diff line change 9
9
* Do not display ` a11y-missing-content ` warning on elements with ` contenteditable ` bindings ([ #5020 ] ( https://github.com/sveltejs/svelte/issues/5020 ) )
10
10
* Fix handling of ` this ` in inline function expressions in the template ([ #5033 ] ( https://github.com/sveltejs/svelte/issues/5033 ) )
11
11
* Fix collapsing HTML with static content ([ #5040 ] ( https://github.com/sveltejs/svelte/issues/5040 ) )
12
+ * Prevent use of ` $store ` at compile time when top-level ` store ` has been shadowed ([ #5048 ] ( https://github.com/sveltejs/svelte/issues/5048 ) )
12
13
* Update ` <select> ` with one-way ` value ` binding when the available ` <option> ` s change ([ #5051 ] ( https://github.com/sveltejs/svelte/issues/5051 ) )
13
14
* Fix published ` tweened ` types so the ` .set() ` and ` .update() ` options are optional ([ #5062 ] ( https://github.com/sveltejs/svelte/issues/5062 ) )
14
15
* Fix contextual ` bind:this ` inside ` {#each} ` block ([ #5067 ] ( https://github.com/sveltejs/svelte/issues/5067 ) )
Original file line number Diff line number Diff line change @@ -836,7 +836,7 @@ export default class Component {
836
836
} ) ;
837
837
}
838
838
839
- warn_on_undefined_store_value_references ( node , parent , scope ) {
839
+ warn_on_undefined_store_value_references ( node , parent , scope : Scope ) {
840
840
if (
841
841
node . type === 'LabeledStatement' &&
842
842
node . label . name === '$' &&
@@ -852,8 +852,17 @@ export default class Component {
852
852
const object = get_object ( node ) ;
853
853
const { name } = object ;
854
854
855
- if ( name [ 0 ] === '$' && ! scope . has ( name ) ) {
856
- this . warn_if_undefined ( name , object , null ) ;
855
+ if ( name [ 0 ] === '$' ) {
856
+ if ( ! scope . has ( name ) ) {
857
+ this . warn_if_undefined ( name , object , null ) ;
858
+ }
859
+
860
+ if ( name [ 1 ] !== '$' && scope . has ( name . slice ( 1 ) ) && scope . find_owner ( name . slice ( 1 ) ) !== this . instance_scope ) {
861
+ this . error ( node , {
862
+ code : `contextual-store` ,
863
+ message : `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
864
+ } ) ;
865
+ }
857
866
}
858
867
}
859
868
}
Original file line number Diff line number Diff line change @@ -78,11 +78,14 @@ export default class Expression {
78
78
79
79
if ( scope . has ( name ) ) return ;
80
80
81
- if ( name [ 0 ] === '$' && template_scope . names . has ( name . slice ( 1 ) ) ) {
82
- component . error ( node , {
83
- code : `contextual-store` ,
84
- message : `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
85
- } ) ;
81
+ if ( name [ 0 ] === '$' ) {
82
+ const store_name = name . slice ( 1 ) ;
83
+ if ( template_scope . names . has ( store_name ) || scope . has ( store_name ) ) {
84
+ component . error ( node , {
85
+ code : `contextual-store` ,
86
+ message : `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
87
+ } ) ;
88
+ }
86
89
}
87
90
88
91
if ( template_scope . is_let ( name ) ) {
Original file line number Diff line number Diff line change
1
+ export default {
2
+ error : `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
3
+ } ;
Original file line number Diff line number Diff line change
1
+ <script >
2
+ import { writable } from ' svelte/store' ;
3
+ const store = writable ();
4
+
5
+ function foo () {
6
+ let store = 1 ;
7
+ $store = 2 ;
8
+ }
9
+ </script >
Original file line number Diff line number Diff line change
1
+ export default {
2
+ error : `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
3
+ } ;
Original file line number Diff line number Diff line change
1
+ <script >
2
+ import { writable } from ' svelte/store' ;
3
+ const store = writable ();
4
+ </script >
5
+
6
+ <button
7
+ on:click ={(store ) => {
8
+ $store = Math .random ();
9
+ }} />
You can’t perform that action at this time.
0 commit comments