Skip to content

Commit

Permalink
Merge pull request #1422 from sveltejs/each-object-create
Browse files Browse the repository at this point in the history
use Object.create for each block child contexts
  • Loading branch information
Rich-Harris authored May 6, 2018
2 parents 3f906fb + d85b60a commit c7c46de
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/compile/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ export default class EachBlock extends Node {
this.block.getUniqueName(this.index); // this prevents name collisions (#1254)
}

this.contextProps = this.contexts.map(prop => `${prop.key.name}: list[i]${prop.tail}`);
this.contextProps = this.contexts.map(prop => `child_ctx.${prop.key.name} = list[i]${prop.tail};`);

// TODO only add these if necessary
this.contextProps.push(
`${this.each_block_value}: list`,
`${indexName}: i`
`child_ctx.${this.each_block_value} = list;`,
`child_ctx.${indexName} = i;`
);

this.compiler.target.blocks.push(this.block);
Expand Down Expand Up @@ -162,9 +162,9 @@ export default class EachBlock extends Node {

this.compiler.target.blocks.push(deindent`
function ${this.get_each_context}(ctx, list, i) {
return @assign(@assign({}, ctx), {
${this.contextProps.join(',\n')}
});
const child_ctx = Object.create(ctx);
${this.contextProps}
return child_ctx;
}
`);

Expand Down
10 changes: 5 additions & 5 deletions test/js/samples/deconflict-builtins/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ function create_each_block(component, ctx) {
}

function get_each_context(ctx, list, i) {
return assign(assign({}, ctx), {
node: list[i],
each_value: list,
node_index: i
});
const child_ctx = Object.create(ctx);
child_ctx.node = list[i];
child_ctx.each_value = list;
child_ctx.node_index = i;
return child_ctx;
}

function SvelteComponent(options) {
Expand Down
10 changes: 5 additions & 5 deletions test/js/samples/deconflict-builtins/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ function create_each_block(component, ctx) {
}

function get_each_context(ctx, list, i) {
return assign(assign({}, ctx), {
node: list[i],
each_value: list,
node_index: i
});
const child_ctx = Object.create(ctx);
child_ctx.node = list[i];
child_ctx.each_value = list;
child_ctx.node_index = i;
return child_ctx;
}

function SvelteComponent(options) {
Expand Down
10 changes: 5 additions & 5 deletions test/js/samples/each-block-changed-check/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ function create_each_block(component, ctx) {
}

function get_each_context(ctx, list, i) {
return assign(assign({}, ctx), {
comment: list[i],
each_value: list,
i: i
});
const child_ctx = Object.create(ctx);
child_ctx.comment = list[i];
child_ctx.each_value = list;
child_ctx.i = i;
return child_ctx;
}

function SvelteComponent(options) {
Expand Down
10 changes: 5 additions & 5 deletions test/js/samples/each-block-changed-check/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ function create_each_block(component, ctx) {
}

function get_each_context(ctx, list, i) {
return assign(assign({}, ctx), {
comment: list[i],
each_value: list,
i: i
});
const child_ctx = Object.create(ctx);
child_ctx.comment = list[i];
child_ctx.each_value = list;
child_ctx.i = i;
return child_ctx;
}

function SvelteComponent(options) {
Expand Down

0 comments on commit c7c46de

Please sign in to comment.