destructuring

pull/1367/head
Rich Harris 7 years ago
parent 77a47020c4
commit 57324507dd

@ -36,6 +36,12 @@ export default class EachBlock extends Node {
// TODO handle indexes and destructuring
this.scope.add(this.context, this.expression.dependencies);
// TODO more general approach to destructuring
this.destructuredContexts = info.destructuredContexts || [];
this.destructuredContexts.forEach(name => {
this.scope.add(name, this.expression.dependencies);
});
this.children = mapChildren(compiler, this, this.scope, info.children);
this.else = info.else

@ -10,9 +10,12 @@ export default function visitEachBlock(
) {
const { snippet } = node.expression;
const props = [`${node.context}: item`]
.concat(node.destructuredContexts.map((name, i) => `${name}: item[${i}]`));
const getContext = node.index
? `(item, i) => Object.assign({}, ctx, { ${node.context}: item, ${node.index}: i })`
: `item => Object.assign({}, ctx, { ${node.context}: item })`;
? `(item, i) => Object.assign({}, ctx, { ${props.join(', ')}, ${node.index}: i })`
: `item => Object.assign({}, ctx, { ${props.join(', ')} })`;
const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}__each(${snippet}, ${getContext}, ctx => \``;
generator.append(open);

Loading…
Cancel
Save