@@ -6,6 +6,7 @@ import createDebuggingComment from '../../utils/createDebuggingComment';
6
6
import Expression from './shared/Expression' ;
7
7
import mapChildren from './shared/mapChildren' ;
8
8
import TemplateScope from './shared/TemplateScope' ;
9
+ import unpackDestructuring from '../../utils/unpackDestructuring' ;
9
10
10
11
export default class EachBlock extends Node {
11
12
type : 'EachBlock' ;
@@ -18,7 +19,7 @@ export default class EachBlock extends Node {
18
19
context : string ;
19
20
key : Expression ;
20
21
scope : TemplateScope ;
21
- destructuredContexts : string [ ] ;
22
+ contexts : Array < { name : string , tail : string } > ;
22
23
23
24
children : Node [ ] ;
24
25
else ?: ElseBlock ;
@@ -27,7 +28,7 @@ export default class EachBlock extends Node {
27
28
super ( compiler , parent , scope , info ) ;
28
29
29
30
this . expression = new Expression ( compiler , this , scope , info . expression ) ;
30
- this . context = info . context ;
31
+ this . context = info . context . name || 'each' ; // TODO this is used to facilitate binding; currently fails with destructuring
31
32
this . index = info . index ;
32
33
33
34
this . key = info . key
@@ -36,20 +37,19 @@ export default class EachBlock extends Node {
36
37
37
38
this . scope = scope . child ( ) ;
38
39
39
- this . scope . add ( this . context , this . expression . dependencies ) ;
40
+ this . contexts = [ ] ;
41
+ unpackDestructuring ( this . contexts , info . context , '' ) ;
42
+
43
+ this . contexts . forEach ( context => {
44
+ this . scope . add ( context . key . name , this . expression . dependencies ) ;
45
+ } ) ;
40
46
41
47
if ( this . index ) {
42
48
// index can only change if this is a keyed each block
43
49
const dependencies = this . key ? this . expression . dependencies : [ ] ;
44
50
this . scope . add ( this . index , dependencies ) ;
45
51
}
46
52
47
- // TODO more general approach to destructuring
48
- this . destructuredContexts = info . destructuredContexts || [ ] ;
49
- this . destructuredContexts . forEach ( name => {
50
- this . scope . add ( name , this . expression . dependencies ) ;
51
- } ) ;
52
-
53
53
this . children = mapChildren ( compiler , this , this . scope , info . children ) ;
54
54
55
55
this . else = info . else
@@ -90,17 +90,13 @@ export default class EachBlock extends Node {
90
90
this . block . getUniqueName ( this . index ) ; // this prevents name collisions (#1254)
91
91
}
92
92
93
- this . contextProps = [
93
+ this . contextProps = this . contexts . map ( prop => `${ prop . key . name } : list[i]${ prop . tail } ` ) ;
94
+
95
+ // TODO only add these if necessary
96
+ this . contextProps . push (
94
97
`${ listName } : list` ,
95
- `${ this . context } : list[i]` ,
96
98
`${ indexName } : i`
97
- ] ;
98
-
99
- if ( this . destructuredContexts ) {
100
- for ( let i = 0 ; i < this . destructuredContexts . length ; i += 1 ) {
101
- this . contextProps . push ( `${ this . destructuredContexts [ i ] } : list[i][${ i } ]` ) ;
102
- }
103
- }
99
+ ) ;
104
100
105
101
this . compiler . target . blocks . push ( this . block ) ;
106
102
this . initChildren ( this . block , stripWhitespace , nextSibling ) ;
@@ -481,8 +477,7 @@ export default class EachBlock extends Node {
481
477
const { compiler } = this ;
482
478
const { snippet } = this . expression ;
483
479
484
- const props = [ `${ this . context } : item` ]
485
- . concat ( this . destructuredContexts . map ( ( name , i ) => `${ name } : item[${ i } ]` ) ) ;
480
+ const props = this . contexts . map ( prop => `${ prop . key . name } : item${ prop . tail } ` ) ;
486
481
487
482
const getContext = this . index
488
483
? `(item, i) => Object.assign({}, ctx, { ${ props . join ( ', ' ) } , ${ this . index } : i })`
0 commit comments