Skip to content

Commit

Permalink
get destructuring working
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 11, 2018
1 parent a0aeb98 commit c3a0878
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/generators/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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')}
}));
}
`);
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 3 additions & 1 deletion src/generators/server-side-rendering/visitors/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3a0878

Please sign in to comment.