get destructuring working

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

@ -83,6 +83,18 @@ export default class EachBlock extends Node {
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.initChildren(this.block, stripWhitespace, nextSibling);
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) {
var ${key} = ${each_block_value}[#i].${this.key};
var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, {
${this.context}: ${each_block_value}[#i],
${this.block.indexName}: #i
${this.contextProps.join(',\n')}
}));
if (${last}) ${last}.next = ${iteration};
@ -379,8 +390,7 @@ export default class EachBlock extends Node {
var ${iteration} = ${lookup}[${key}];
var ${this.each_context} = @assign({}, state, {
${this.context}: ${each_block_value}[#i],
${this.block.indexName}: #i
${this.contextProps.join(',\n')}
});
${dynamic &&
@ -477,8 +487,7 @@ export default class EachBlock extends Node {
for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) {
${iterations}[#i] = ${create_each_block}(#component, @assign({}, state, {
${this.context}: ${each_block_value}[#i],
${this.block.indexName}: #i
${this.contextProps.join(',\n')}
}));
}
`);
@ -576,8 +585,7 @@ export default class EachBlock extends Node {
if (${condition}) {
for (var #i = ${start}; #i < ${each_block_value}.${length}; #i += 1) {
var ${this.each_context} = @assign({}, state, {
${this.context}: ${each_block_value}[#i],
${this.block.indexName}: #i
${this.contextProps.join(',\n')}
});
${forLoopBody}

@ -11,7 +11,9 @@ export default function visitEachBlock(
block.contextualise(node.expression);
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);
// TODO should this be the generator's job? It's duplicated between

Loading…
Cancel
Save