Skip to content

Commit c394aa7

Browse files
authoredOct 17, 2017
Merge pull request #889 from jacobmischka/destructure-each
Add array destructuring for each contexts
2 parents d01d7ee + a60a7e6 commit c394aa7

File tree

8 files changed

+123
-2
lines changed

8 files changed

+123
-2
lines changed
 

‎src/generators/dom/Block.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class Block {
2929
name: string;
3030
expression: Node;
3131
context: string;
32+
destructuredContexts?: string[];
3233
comment?: string;
3334

3435
key: string;
@@ -75,6 +76,7 @@ export default class Block {
7576
this.name = options.name;
7677
this.expression = options.expression;
7778
this.context = options.context;
79+
this.destructuredContexts = options.destructuredContexts;
7880
this.comment = options.comment;
7981

8082
// for keyed each blocks

‎src/generators/dom/preprocess.ts

+6
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ const preprocessors = {
229229
const contexts = new Map(block.contexts);
230230
contexts.set(node.context, context);
231231

232+
if (node.destructuredContexts) {
233+
for (const i = 0; i < node.destructuredContexts.length; i++) {
234+
contexts.set(node.destructuredContexts[i], `${context}[${i}]`);
235+
}
236+
}
237+
232238
const indexes = new Map(block.indexes);
233239
if (node.index) indexes.set(node.index, node.context);
234240

‎src/generators/server-side-rendering/visitors/EachBlock.ts

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export default function visitEachBlock(
1818
const contexts = new Map(block.contexts);
1919
contexts.set(node.context, node.context);
2020

21+
if (node.destructuredContexts) {
22+
for (const i = 0; i < node.destructuredContexts.length; i++) {
23+
contexts.set(node.destructuredContexts[i], `${node.context}[${i}]`);
24+
}
25+
}
26+
2127
const indexes = new Map(block.indexes);
2228
if (node.index) indexes.set(node.index, node.context);
2329

‎src/parse/state/mustache.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,29 @@ export default function mustache(parser: Parser) {
161161
parser.eat('as', true);
162162
parser.requireWhitespace();
163163

164-
block.context = parser.read(validIdentifier); // TODO check it's not a keyword
165-
if (!block.context) parser.error(`Expected name`);
164+
if (parser.eat('[')) {
165+
parser.allowWhitespace();
166+
167+
block.destructuredContexts = [];
168+
169+
do {
170+
parser.allowWhitespace();
171+
const destructuredContext = parser.read(validIdentifier);
172+
if (!destructuredContext) parser.error(`Expected name`);
173+
block.destructuredContexts.push(destructuredContext);
174+
parser.allowWhitespace();
175+
} while (parser.eat(','));
176+
177+
if (!block.destructuredContexts.length) parser.error(`Expected name`);
178+
block.context = block.destructuredContexts.join('_');
179+
180+
parser.allowWhitespace();
181+
parser.eat(']', true);
182+
} else {
183+
block.context = parser.read(validIdentifier); // TODO check it's not a keyword
184+
185+
if (!block.context) parser.error(`Expected name`);
186+
}
166187

167188
parser.allowWhitespace();
168189

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{#each animals as [key, value]}}
2+
<p>{{key}}: {{value}}</p>
3+
{{/each}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"hash": 2621498076,
3+
"html": {
4+
"start": 0,
5+
"end": 70,
6+
"type": "Fragment",
7+
"children": [
8+
{
9+
"start": 0,
10+
"end": 70,
11+
"type": "EachBlock",
12+
"expression": {
13+
"type": "Identifier",
14+
"start": 8,
15+
"end": 15,
16+
"name": "animals"
17+
},
18+
"children": [
19+
{
20+
"start": 35,
21+
"end": 60,
22+
"type": "Element",
23+
"name": "p",
24+
"attributes": [],
25+
"children": [
26+
{
27+
"start": 38,
28+
"end": 45,
29+
"type": "MustacheTag",
30+
"expression": {
31+
"type": "Identifier",
32+
"start": 40,
33+
"end": 43,
34+
"name": "key"
35+
}
36+
},
37+
{
38+
"start": 45,
39+
"end": 47,
40+
"type": "Text",
41+
"data": ": "
42+
},
43+
{
44+
"start": 47,
45+
"end": 56,
46+
"type": "MustacheTag",
47+
"expression": {
48+
"type": "Identifier",
49+
"start": 49,
50+
"end": 54,
51+
"name": "value"
52+
}
53+
}
54+
]
55+
}
56+
],
57+
"destructuredContexts": [
58+
"key",
59+
"value"
60+
],
61+
"context": "key_value"
62+
}
63+
]
64+
},
65+
"css": null,
66+
"js": null
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
data: {
3+
animalPawsEntries: [
4+
['raccoon', 'hands'],
5+
['eagle', 'wings']
6+
]
7+
},
8+
9+
html: `
10+
<p>raccoon: hands</p>
11+
<p>eagle: wings</p>
12+
`
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{#each animalPawsEntries as [animal, pawType]}}
2+
<p>{{animal}}: {{pawType}}</p>
3+
{{/each}}

0 commit comments

Comments
 (0)
Please sign in to comment.