Skip to content

Commit c7c46de

Browse files
authored
Merge pull request #1422 from sveltejs/each-object-create
use Object.create for each block child contexts
2 parents 3f906fb + d85b60a commit c7c46de

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

src/compile/nodes/EachBlock.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ export default class EachBlock extends Node {
9191
this.block.getUniqueName(this.index); // this prevents name collisions (#1254)
9292
}
9393

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

9696
// TODO only add these if necessary
9797
this.contextProps.push(
98-
`${this.each_block_value}: list`,
99-
`${indexName}: i`
98+
`child_ctx.${this.each_block_value} = list;`,
99+
`child_ctx.${indexName} = i;`
100100
);
101101

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

163163
this.compiler.target.blocks.push(deindent`
164164
function ${this.get_each_context}(ctx, list, i) {
165-
return @assign(@assign({}, ctx), {
166-
${this.contextProps.join(',\n')}
167-
});
165+
const child_ctx = Object.create(ctx);
166+
${this.contextProps}
167+
return child_ctx;
168168
}
169169
`);
170170

test/js/samples/deconflict-builtins/expected-bundle.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ function create_each_block(component, ctx) {
238238
}
239239

240240
function get_each_context(ctx, list, i) {
241-
return assign(assign({}, ctx), {
242-
node: list[i],
243-
each_value: list,
244-
node_index: i
245-
});
241+
const child_ctx = Object.create(ctx);
242+
child_ctx.node = list[i];
243+
child_ctx.each_value = list;
244+
child_ctx.node_index = i;
245+
return child_ctx;
246246
}
247247

248248
function SvelteComponent(options) {

test/js/samples/deconflict-builtins/expected.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ function create_each_block(component, ctx) {
9292
}
9393

9494
function get_each_context(ctx, list, i) {
95-
return assign(assign({}, ctx), {
96-
node: list[i],
97-
each_value: list,
98-
node_index: i
99-
});
95+
const child_ctx = Object.create(ctx);
96+
child_ctx.node = list[i];
97+
child_ctx.each_value = list;
98+
child_ctx.node_index = i;
99+
return child_ctx;
100100
}
101101

102102
function SvelteComponent(options) {

test/js/samples/each-block-changed-check/expected-bundle.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ function create_each_block(component, ctx) {
279279
}
280280

281281
function get_each_context(ctx, list, i) {
282-
return assign(assign({}, ctx), {
283-
comment: list[i],
284-
each_value: list,
285-
i: i
286-
});
282+
const child_ctx = Object.create(ctx);
283+
child_ctx.comment = list[i];
284+
child_ctx.each_value = list;
285+
child_ctx.i = i;
286+
return child_ctx;
287287
}
288288

289289
function SvelteComponent(options) {

test/js/samples/each-block-changed-check/expected.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ function create_each_block(component, ctx) {
131131
}
132132

133133
function get_each_context(ctx, list, i) {
134-
return assign(assign({}, ctx), {
135-
comment: list[i],
136-
each_value: list,
137-
i: i
138-
});
134+
const child_ctx = Object.create(ctx);
135+
child_ctx.comment = list[i];
136+
child_ctx.each_value = list;
137+
child_ctx.i = i;
138+
return child_ctx;
139139
}
140140

141141
function SvelteComponent(options) {

0 commit comments

Comments
 (0)