get destructuring working

pull/1173/head
Rich Harris 8 years ago
parent a0aeb98685
commit c3a08788e6

@ -83,6 +83,18 @@ export default class EachBlock extends Node {
listNames listNames
}); });
this.contextProps = [
`${context}: ${listName}[#i]`,
`${indexName}: #i`
];
if (this.destructuredContexts) {
for (let i = 0; i < this.destructuredContexts.length; i += 1) {
contexts.set(this.destructuredContexts[i], `${context}[${i}]`);
this.contextProps.push(`${this.destructuredContexts[i]}: ${listName}[#i][${i}]`);
}
}
this.generator.blocks.push(this.block); this.generator.blocks.push(this.block);
this.initChildren(this.block, stripWhitespace, nextSibling); this.initChildren(this.block, stripWhitespace, nextSibling);
block.addDependencies(this.block.dependencies); block.addDependencies(this.block.dependencies);
@ -272,8 +284,7 @@ export default class EachBlock extends Node {
for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) {
var ${key} = ${each_block_value}[#i].${this.key}; var ${key} = ${each_block_value}[#i].${this.key};
var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, { var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, {
${this.context}: ${each_block_value}[#i], ${this.contextProps.join(',\n')}
${this.block.indexName}: #i
})); }));
if (${last}) ${last}.next = ${iteration}; if (${last}) ${last}.next = ${iteration};
@ -379,8 +390,7 @@ export default class EachBlock extends Node {
var ${iteration} = ${lookup}[${key}]; var ${iteration} = ${lookup}[${key}];
var ${this.each_context} = @assign({}, state, { var ${this.each_context} = @assign({}, state, {
${this.context}: ${each_block_value}[#i], ${this.contextProps.join(',\n')}
${this.block.indexName}: #i
}); });
${dynamic && ${dynamic &&
@ -477,8 +487,7 @@ export default class EachBlock extends Node {
for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) { for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) {
${iterations}[#i] = ${create_each_block}(#component, @assign({}, state, { ${iterations}[#i] = ${create_each_block}(#component, @assign({}, state, {
${this.context}: ${each_block_value}[#i], ${this.contextProps.join(',\n')}
${this.block.indexName}: #i
})); }));
} }
`); `);
@ -576,8 +585,7 @@ export default class EachBlock extends Node {
if (${condition}) { if (${condition}) {
for (var #i = ${start}; #i < ${each_block_value}.${length}; #i += 1) { for (var #i = ${start}; #i < ${each_block_value}.${length}; #i += 1) {
var ${this.each_context} = @assign({}, state, { var ${this.each_context} = @assign({}, state, {
${this.context}: ${each_block_value}[#i], ${this.contextProps.join(',\n')}
${this.block.indexName}: #i
}); });
${forLoopBody} ${forLoopBody}

@ -11,7 +11,9 @@ export default function visitEachBlock(
block.contextualise(node.expression); block.contextualise(node.expression);
const { dependencies, snippet } = node.metadata; const { dependencies, snippet } = node.metadata;
const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${node.context}, ${node.index})` : node.context} => \``; const context = node.destructuredContexts ? `[${node.destructuredContexts.join(', ')}]` : node.context;
const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${context}, ${node.index})` : `(${context})`} => \``;
generator.append(open); generator.append(open);
// TODO should this be the generator's job? It's duplicated between // TODO should this be the generator's job? It's duplicated between

Loading…
Cancel
Save