Skip to content

Commit 1ce6ac5

Browse files
authored
fix destructuring to get multiple stores (#5390)
1 parent 338cf87 commit 1ce6ac5

File tree

10 files changed

+71
-22
lines changed

10 files changed

+71
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Fix specificity of certain styles involving a child selector ([#4795](https://github.com/sveltejs/svelte/issues/4795))
66
* Fix transitions that are parameterised with stores ([#5244](https://github.com/sveltejs/svelte/issues/5244))
77
* Fix scoping of styles involving child selector and `*` ([#5370](https://github.com/sveltejs/svelte/issues/5370))
8+
* Fix destructuring which reassigns stores ([#5388](https://github.com/sveltejs/svelte/issues/5388))
89

910
## 3.25.0
1011

src/compiler/compile/Component.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -947,12 +947,6 @@ export default class Component {
947947
const variable = component.var_lookup.get(name);
948948

949949
if (variable.export_name && variable.writable) {
950-
const insert = variable.subscribable
951-
? get_insert(variable)
952-
: null;
953-
954-
parent[key].splice(index + 1, 0, insert);
955-
956950
declarator.id = {
957951
type: 'ObjectPattern',
958952
properties: [{
@@ -973,7 +967,9 @@ export default class Component {
973967
};
974968

975969
declarator.init = x`$$props`;
976-
} else if (variable.subscribable) {
970+
}
971+
972+
if (variable.subscribable && declarator.init) {
977973
const insert = get_insert(variable);
978974
parent[key].splice(index + 1, 0, ...insert);
979975
}

src/compiler/compile/render_dom/Renderer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,14 @@ export default class Renderer {
166166
return member;
167167
}
168168

169-
invalidate(name: string, value?) {
169+
invalidate(name: string, value?, main_execution_context: boolean = false) {
170170
const variable = this.component.var_lookup.get(name);
171171
const member = this.context_lookup.get(name);
172172

173173
if (variable && (variable.subscribable && (variable.reassigned || variable.export_name))) {
174-
return x`${`$$subscribe_${name}`}($$invalidate(${member.index}, ${value || name}))`;
174+
return main_execution_context
175+
? x`${`$$subscribe_${name}`}(${value || name})`
176+
: x`${`$$subscribe_${name}`}($$invalidate(${member.index}, ${value || name}))`;
175177
}
176178

177179
if (name[0] === '$' && name[1] !== '$') {

src/compiler/compile/render_dom/invalidate.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
3131

3232
function get_invalidated(variable: Var, node?: Expression) {
3333
if (main_execution_context && !variable.subscribable && variable.name[0] !== '$') {
34-
return node || x`${variable.name}`;
34+
return node;
3535
}
36-
37-
return renderer.invalidate(variable.name);
36+
return renderer.invalidate(variable.name, undefined, main_execution_context);
3837
}
3938

4039
if (head) {
@@ -44,12 +43,15 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
4443
return get_invalidated(head, node);
4544
} else {
4645
const is_store_value = head.name[0] === '$' && head.name[1] !== '$';
47-
const extra_args = tail.map(variable => get_invalidated(variable));
46+
const extra_args = tail.map(variable => get_invalidated(variable)).filter(Boolean);
4847

4948
const pass_value = (
50-
extra_args.length > 0 ||
51-
(node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
52-
(node.type === 'UpdateExpression' && (!node.prefix || node.argument.type !== 'Identifier'))
49+
!main_execution_context &&
50+
(
51+
extra_args.length > 0 ||
52+
(node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
53+
(node.type === 'UpdateExpression' && (!node.prefix || node.argument.type !== 'Identifier'))
54+
)
5355
);
5456

5557
if (pass_value) {
@@ -63,7 +65,9 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
6365
? x`@set_store_value(${head.name.slice(1)}, ${node}, ${extra_args})`
6466
: !main_execution_context
6567
? x`$$invalidate(${renderer.context_lookup.get(head.name).index}, ${node}, ${extra_args})`
66-
: node;
68+
: extra_args.length
69+
? [node, ...extra_args]
70+
: node;
6771

6872
if (head.subscribable && head.reassigned) {
6973
const subscribe = `$$subscribe_${head.name}`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
html: `<h1>2 2 xxx 5 6</h1>`
3+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
4+
let eid = writable(1);
5+
let foo;
6+
let u;
7+
let v;
8+
let w;
9+
[u, v, w] = [
10+
{id: eid = writable(foo = 2), name: 'xxx'},
11+
5,
12+
writable(6)
13+
];
14+
</script>
15+
16+
<h1>{foo} {$eid} {u.name} {v} {$w}</h1>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<script>
22
let foo;
33
let bar = (foo = 1);
4+
function a() {
5+
bar = (foo = 1);
6+
}
47
</script>
58

69
<h1>{foo} {bar}</h1>
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<script>
2-
let foo1;
3-
let foo2;
4-
for (let bar = (foo1 = 0); bar < 5; bar += 1) {
5-
foo2 = foo1;
6-
}
2+
let foo1;
3+
let foo2;
4+
for (let bar = (foo1 = 0); bar < 5; bar += 1) {
5+
foo2 = foo1;
6+
}
7+
function a() {
8+
for (let bar = (foo1 = 0); bar < 5; bar += 1) {
9+
foo2 = foo1;
10+
}
11+
}
712
</script>
813

914
<h1>{foo1} {foo2}</h1>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
html: `31 42`
3+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
const context = {
4+
store1: writable(31),
5+
store2: writable(42)
6+
};
7+
let store1;
8+
let store2;
9+
({
10+
store1,
11+
store2
12+
} = context);
13+
</script>
14+
15+
{$store1}
16+
{$store2}

0 commit comments

Comments
 (0)