Skip to content

Commit 0ad8bf9

Browse files
authored
Merge pull request #2103 from sveltejs/gh-2016
error on contextual stores, for now
2 parents 47ab23c + 6f394e5 commit 0ad8bf9

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/compile/nodes/shared/Expression.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,16 @@ export default class Expression {
122122
const { name, nodes } = flattenReference(node);
123123

124124
if (scope.has(name)) return;
125+
125126
if (globalWhitelist.has(name) && !component.var_lookup.has(name)) return;
126127

128+
if (name[0] === '$' && template_scope.names.has(name.slice(1))) {
129+
component.error(node, {
130+
code: `contextual-store`,
131+
message: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
132+
});
133+
}
134+
127135
if (template_scope.is_let(name)) {
128136
if (!function_expression) {
129137
dependencies.add(name);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { writable } from '../../../../store.js';
2+
3+
const todos = [
4+
writable({ done: false, text: 'write docs' }),
5+
writable({ done: false, text: 'implement contextual stores' }),
6+
writable({ done: false, text: 'go outside' })
7+
];
8+
9+
export default {
10+
error: `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`,
11+
12+
props: {
13+
todos
14+
},
15+
16+
html: `
17+
<label>
18+
<input type=checkbox>
19+
[todo] write docs
20+
</label>
21+
22+
<label>
23+
<input type=checkbox>
24+
[todo] implement contextual stores
25+
</label>
26+
27+
<label>
28+
<input type=checkbox>
29+
[todo] go outside
30+
</label>
31+
`,
32+
33+
async test({ assert, component, target, window }) {
34+
const inputs = target.querySelectorAll('input');
35+
const change = new window.MouseEvent('change');
36+
37+
inputs[1].checked = true;
38+
await inputs[1].dispatchEvent(change);
39+
40+
assert.htmlEqual(target.innerHTML, `
41+
<label>
42+
<input type=checkbox>
43+
[todo] write docs
44+
</label>
45+
46+
<label>
47+
<input type=checkbox>
48+
[done] implement contextual stores
49+
</label>
50+
51+
<label>
52+
<input type=checkbox>
53+
[todo] go outside
54+
</label>
55+
`);
56+
57+
await todos[0].update(todo => ({ done: !todo.done, text: todo.text }));
58+
59+
assert.htmlEqual(target.innerHTML, `
60+
<label>
61+
<input type=checkbox>
62+
[done] write docs
63+
</label>
64+
65+
<label>
66+
<input type=checkbox>
67+
[done] implement contextual stores
68+
</label>
69+
70+
<label>
71+
<input type=checkbox>
72+
[todo] go outside
73+
</label>
74+
`);
75+
}
76+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{#each todos as todo}
2+
<label>
3+
<input type=checkbox on:change={e => todo.update(t => ({ done: e.target.checked, text: t.text }))}>
4+
{$todo.done ? '[done]' : '[todo]'} {$todo.text}
5+
</label>
6+
{/each}

0 commit comments

Comments
 (0)