Skip to content

Commit 619cf57

Browse files
committed
add more test case, supporting deep destructuring and array destructuring
1 parent 2562687 commit 619cf57

File tree

8 files changed

+66
-20
lines changed

8 files changed

+66
-20
lines changed

src/compiler/compile/nodes/AwaitBlock.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default class AwaitBlock extends Node {
2323
then: ThenBlock;
2424
catch: CatchBlock;
2525

26+
context_rest_properties: Map<string, ESTreeNode> = new Map();
27+
2628
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
2729
super(component, parent, scope, info);
2830

@@ -33,12 +35,12 @@ export default class AwaitBlock extends Node {
3335

3436
if (this.then_node) {
3537
this.then_contexts = [];
36-
unpack_destructuring({ contexts: this.then_contexts, node: info.value, scope, component });
38+
unpack_destructuring({ contexts: this.then_contexts, node: info.value, scope, component, context_rest_properties: this.context_rest_properties });
3739
}
3840

3941
if (this.catch_node) {
4042
this.catch_contexts = [];
41-
unpack_destructuring({ contexts: this.catch_contexts, node: info.error, scope, component });
43+
unpack_destructuring({ contexts: this.catch_contexts, node: info.error, scope, component, context_rest_properties: this.context_rest_properties });
4244
}
4345

4446
this.pending = new PendingBlock(component, this, scope, info.pending);

src/compiler/compile/nodes/ConstTag.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { extract_identifiers } from 'periscopic';
1010
import is_reference, { NodeWithPropertyDefinition } from 'is-reference';
1111
import get_object from '../utils/get_object';
1212
import compiler_errors from '../compiler_errors';
13+
import { Node as ESTreeNode } from 'estree';
1314

1415
const allowed_parents = new Set(['EachBlock', 'CatchBlock', 'ThenBlock', 'InlineComponent', 'SlotTemplate', 'IfBlock', 'ElseBlock']);
1516

@@ -19,6 +20,7 @@ export default class ConstTag extends Node {
1920
contexts: Context[] = [];
2021
node: ConstTagType;
2122
scope: TemplateScope;
23+
context_rest_properties: Map<string, ESTreeNode> = new Map();
2224

2325
assignees: Set<string> = new Set();
2426
dependencies: Set<string> = new Set();
@@ -58,7 +60,8 @@ export default class ConstTag extends Node {
5860
contexts: this.contexts,
5961
node: this.node.expression.left,
6062
scope: this.scope,
61-
component: this.component
63+
component: this.component,
64+
context_rest_properties: this.context_rest_properties
6265
});
6366
this.expression = new Expression(this.component, this, this.scope, this.node.expression.right);
6467
this.contexts.forEach(context => {

src/compiler/compile/nodes/EachBlock.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import AbstractBlock from './shared/AbstractBlock';
55
import Element from './Element';
66
import ConstTag from './ConstTag';
77
import { Context, unpack_destructuring } from './shared/Context';
8-
import { Node, Identifier } from 'estree';
8+
import { Node } from 'estree';
99
import Component from '../Component';
1010
import { TemplateNode } from '../../interfaces';
1111
import compiler_errors from '../compiler_errors';
@@ -42,15 +42,8 @@ export default class EachBlock extends AbstractBlock {
4242
this.scope = scope.child();
4343
this.context_rest_properties = new Map();
4444
this.contexts = [];
45-
unpack_destructuring({ contexts: this.contexts, node: info.context, scope, component });
46-
47-
if (this.context_node.type === 'ObjectPattern') {
48-
this.context_node.properties.forEach(i => {
49-
if (i.type === 'RestElement') {
50-
this.context_rest_properties.set((i.argument as Identifier).name, i);
51-
}
52-
});
53-
}
45+
unpack_destructuring({ contexts: this.contexts, node: info.context, scope, component, context_rest_properties: this.context_rest_properties });
46+
5447
this.contexts.forEach(context => {
5548
this.scope.add(context.key.name, this.expression.dependencies, this);
5649
});

src/compiler/compile/nodes/shared/Context.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ export function unpack_destructuring({
2020
modifier = (node) => node,
2121
default_modifier = (node) => node,
2222
scope,
23-
component
23+
component,
24+
context_rest_properties
2425
}: {
2526
contexts: Context[];
2627
node: Node;
2728
modifier?: Context['modifier'];
2829
default_modifier?: Context['default_modifier'];
2930
scope: TemplateScope;
3031
component: Component;
32+
context_rest_properties: Map<string, Node>;
3133
}) {
3234
if (!node) return;
3335

@@ -43,6 +45,7 @@ export function unpack_destructuring({
4345
modifier,
4446
default_modifier
4547
});
48+
context_rest_properties.set((node.argument as Identifier).name, node);
4649
} else if (node.type === 'ArrayPattern') {
4750
node.elements.forEach((element, i) => {
4851
if (element && element.type === 'RestElement') {
@@ -52,8 +55,10 @@ export function unpack_destructuring({
5255
modifier: (node) => x`${modifier(node)}.slice(${i})` as Node,
5356
default_modifier,
5457
scope,
55-
component
58+
component,
59+
context_rest_properties
5660
});
61+
context_rest_properties.set((element.argument as Identifier).name, node);
5762
} else if (element && element.type === 'AssignmentPattern') {
5863
const n = contexts.length;
5964
mark_referenced(element.right, scope, component);
@@ -70,7 +75,8 @@ export function unpack_destructuring({
7075
to_ctx
7176
)}` as Node,
7277
scope,
73-
component
78+
component,
79+
context_rest_properties
7480
});
7581
} else {
7682
unpack_destructuring({
@@ -79,7 +85,8 @@ export function unpack_destructuring({
7985
modifier: (node) => x`${modifier(node)}[${i}]` as Node,
8086
default_modifier,
8187
scope,
82-
component
88+
component,
89+
context_rest_properties
8390
});
8491
}
8592
});
@@ -97,8 +104,10 @@ export function unpack_destructuring({
97104
)}, [${used_properties}])` as Node,
98105
default_modifier,
99106
scope,
100-
component
107+
component,
108+
context_rest_properties
101109
});
110+
context_rest_properties.set((property.argument as Identifier).name, node);
102111
} else {
103112
const key = property.key as Identifier;
104113
const value = property.value;
@@ -121,7 +130,8 @@ export function unpack_destructuring({
121130
to_ctx
122131
)}` as Node,
123132
scope,
124-
component
133+
component,
134+
context_rest_properties
125135
});
126136
} else {
127137
unpack_destructuring({
@@ -130,7 +140,8 @@ export function unpack_destructuring({
130140
modifier: (node) => x`${modifier(node)}.${key.name}` as Node,
131141
default_modifier,
132142
scope,
133-
component
143+
component,
144+
context_rest_properties
134145
});
135146
}
136147
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
let objArray = [
3+
[1, 2, 3, "4"],
4+
[5, 6, 7, "8"],
5+
];
6+
</script>
7+
8+
{#each objArray as [id, ...rest] (id)}
9+
<input bind:value={rest[0]} type="text" placeholder={rest[2]} />
10+
<br />
11+
{/each}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"code": "invalid-rest-eachblock-binding",
4+
"message": "...rest operator will create a new object and binding propogation with original object will not work",
5+
"pos": 102,
6+
"start": { "line": 8, "column": 24, "character": 102 },
7+
"end": { "line": 8, "column": 31, "character": 109 }
8+
}
9+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
let objArray = [{ bar: {foo: '1', id: 0, innerValue: "test"} }, { bar: {foo: '2', id:1, innerValue: "Somethin"} }]
3+
</script>
4+
5+
{#each objArray as { bar: { id, ...rest } } (id)}
6+
<input bind:value={rest.innerValue} type="text" placeholder={rest.foo}/>
7+
<br/>
8+
{/each}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"code": "invalid-rest-eachblock-binding",
4+
"message": "...rest operator will create a new object and binding propogation with original object will not work",
5+
"pos": 162,
6+
"start": { "line": 5, "column": 26, "character": 162 },
7+
"end": { "line": 5, "column": 41, "character": 177 }
8+
}
9+
]

0 commit comments

Comments
 (0)