File tree 8 files changed +123
-2
lines changed
server-side-rendering/visitors
parser/samples/each-block-destructured
runtime/samples/each-block-destructured-array
8 files changed +123
-2
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ export default class Block {
29
29
name : string ;
30
30
expression : Node ;
31
31
context : string ;
32
+ destructuredContexts ?: string [ ] ;
32
33
comment ?: string ;
33
34
34
35
key : string ;
@@ -75,6 +76,7 @@ export default class Block {
75
76
this . name = options . name ;
76
77
this . expression = options . expression ;
77
78
this . context = options . context ;
79
+ this . destructuredContexts = options . destructuredContexts ;
78
80
this . comment = options . comment ;
79
81
80
82
// for keyed each blocks
Original file line number Diff line number Diff line change @@ -229,6 +229,12 @@ const preprocessors = {
229
229
const contexts = new Map ( block . contexts ) ;
230
230
contexts . set ( node . context , context ) ;
231
231
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
+
232
238
const indexes = new Map ( block . indexes ) ;
233
239
if ( node . index ) indexes . set ( node . index , node . context ) ;
234
240
Original file line number Diff line number Diff line change @@ -18,6 +18,12 @@ export default function visitEachBlock(
18
18
const contexts = new Map ( block . contexts ) ;
19
19
contexts . set ( node . context , node . context ) ;
20
20
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
+
21
27
const indexes = new Map ( block . indexes ) ;
22
28
if ( node . index ) indexes . set ( node . index , node . context ) ;
23
29
Original file line number Diff line number Diff line change @@ -161,8 +161,29 @@ export default function mustache(parser: Parser) {
161
161
parser . eat ( 'as' , true ) ;
162
162
parser . requireWhitespace ( ) ;
163
163
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
+ }
166
187
167
188
parser . allowWhitespace ( ) ;
168
189
Original file line number Diff line number Diff line change
1
+ {{#each animals as [key, value]}}
2
+ < p > {{key}}: {{value}}</ p >
3
+ {{/each}}
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ {{#each animalPawsEntries as [animal, pawType]}}
2
+ < p > {{animal}}: {{pawType}}</ p >
3
+ {{/each}}
You can’t perform that action at this time.
0 commit comments