Skip to content

Commit 0d881ee

Browse files
authored
Merge pull request #1383 from sveltejs/gh-1187-b
only add list/index to each block context if necessary
2 parents 11469ef + e7c62e9 commit 0d881ee

File tree

11 files changed

+35
-20
lines changed

11 files changed

+35
-20
lines changed

src/compile/render-dom/Block.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import deindent from '../../utils/deindent';
33
import { escape } from '../../utils/stringify';
44
import Renderer from './Renderer';
55
import Wrapper from './wrappers/shared/Wrapper';
6+
import EachBlockWrapper from './wrappers/EachBlock';
67

78
export interface BlockOptions {
89
parent?: Block;
@@ -11,6 +12,7 @@ export interface BlockOptions {
1112
comment?: string;
1213
key?: string;
1314
bindings?: Map<string, () => string>;
15+
contextOwners?: Map<string, EachBlockWrapper>;
1416
dependencies?: Set<string>;
1517
}
1618

@@ -28,6 +30,7 @@ export default class Block {
2830
dependencies: Set<string>;
2931

3032
bindings: Map<string, () => string>;
33+
contextOwners: Map<string, EachBlockWrapper>;
3134

3235
builders: {
3336
init: CodeBuilder;
@@ -74,6 +77,7 @@ export default class Block {
7477
this.dependencies = new Set();
7578

7679
this.bindings = options.bindings;
80+
this.contextOwners = options.contextOwners;
7781

7882
this.builders = {
7983
init: new CodeBuilder(),

src/compile/render-dom/Renderer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default class Renderer {
4747
key: null,
4848

4949
bindings: new Map(),
50+
contextOwners: new Map(),
5051

5152
dependencies: new Set(),
5253
});

src/compile/render-dom/wrappers/EachBlock.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export default class EachBlockWrapper extends Wrapper {
5353
node: EachBlock;
5454
fragment: FragmentWrapper;
5555
else?: ElseBlockWrapper;
56-
var: string;
5756
vars: {
5857
anchor: string;
5958
create_each_block: string;
@@ -64,6 +63,12 @@ export default class EachBlockWrapper extends Wrapper {
6463
mountOrIntro: string;
6564
}
6665

66+
contextProps: string[];
67+
indexName: string;
68+
69+
var = 'each';
70+
hasBinding = false;
71+
6772
constructor(
6873
renderer: Renderer,
6974
block: Block,
@@ -75,8 +80,6 @@ export default class EachBlockWrapper extends Wrapper {
7580
super(renderer, block, parent, node);
7681
this.cannotUseInnerHTML();
7782

78-
this.var = 'each';
79-
8083
const { dependencies } = node.expression;
8184
block.addDependencies(dependencies);
8285

@@ -85,7 +88,8 @@ export default class EachBlockWrapper extends Wrapper {
8588
name: renderer.component.getUniqueName('create_each_block'),
8689
key: <string>node.key, // TODO...
8790

88-
bindings: new Map(block.bindings)
91+
bindings: new Map(block.bindings),
92+
contextOwners: new Map(block.contextOwners)
8993
});
9094

9195
// TODO this seems messy
@@ -94,6 +98,8 @@ export default class EachBlockWrapper extends Wrapper {
9498
this.indexName = this.node.index || renderer.component.getUniqueName(`${this.node.context}_index`);
9599

96100
node.contexts.forEach(prop => {
101+
this.block.contextOwners.set(prop.key.name, this);
102+
97103
// TODO this doesn't feel great
98104
this.block.bindings.set(prop.key.name, () => `ctx.${this.vars.each_block_value}[ctx.${this.indexName}]${prop.tail}`);
99105
});
@@ -164,11 +170,8 @@ export default class EachBlockWrapper extends Wrapper {
164170

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

167-
// TODO only add these if necessary
168-
this.contextProps.push(
169-
`child_ctx.${this.vars.each_block_value} = list;`,
170-
`child_ctx.${this.indexName} = i;`
171-
);
173+
if (this.hasBinding) this.contextProps.push(`child_ctx.${this.vars.each_block_value} = list;`);
174+
if (this.hasBinding || this.node.index) this.contextProps.push(`child_ctx.${this.indexName} = i;`);
172175

173176
const { snippet } = this.node.expression;
174177

src/compile/render-dom/wrappers/Element/Binding.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export default class BindingWrapper {
4242
parent.renderer.component.indirectDependencies.set(prop, new Set());
4343
});
4444
}
45+
46+
if (node.isContextual) {
47+
// we need to ensure that the each block creates a context including
48+
// the list and the index, if they're not otherwise referenced
49+
const { name } = getObject(this.node.value.node);
50+
const eachBlock = block.contextOwners.get(name);
51+
52+
eachBlock.hasBinding = true;
53+
}
4554
}
4655

4756
isReadOnlyMediaAttribute() {

src/compile/render-dom/wrappers/InlineComponent/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ export default class InlineComponentWrapper extends Wrapper {
4141
});
4242

4343
this.node.bindings.forEach(binding => {
44+
if (binding.isContextual) {
45+
// we need to ensure that the each block creates a context including
46+
// the list and the index, if they're not otherwise referenced
47+
const { name } = getObject(binding.value.node);
48+
const eachBlock = block.contextOwners.get(name);
49+
50+
eachBlock.hasBinding = true;
51+
}
52+
4453
block.addDependencies(binding.value.dependencies);
4554
});
4655

test/js/samples/debug-foo-bar-baz-things/expected.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const file = undefined;
66
function get_each_context(ctx, list, i) {
77
const child_ctx = Object.create(ctx);
88
child_ctx.thing = list[i];
9-
child_ctx.each_value = list;
10-
child_ctx.thing_index = i;
119
return child_ctx;
1210
}
1311

test/js/samples/debug-foo/expected.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const file = undefined;
66
function get_each_context(ctx, list, i) {
77
const child_ctx = Object.create(ctx);
88
child_ctx.thing = list[i];
9-
child_ctx.each_value = list;
10-
child_ctx.thing_index = i;
119
return child_ctx;
1210
}
1311

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { append, assign, createComment, createElement, createText, destroyEach,
44
function get_each_context(ctx, list, i) {
55
const child_ctx = Object.create(ctx);
66
child_ctx.node = list[i];
7-
child_ctx.each_value = list;
8-
child_ctx.node_index = i;
97
return child_ctx;
108
}
119

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { append, assign, createElement, createText, destroyEach, detachAfter, de
44
function get_each_context(ctx, list, i) {
55
const child_ctx = Object.create(ctx);
66
child_ctx.comment = list[i];
7-
child_ctx.each_value = list;
87
child_ctx.i = i;
98
return child_ctx;
109
}

test/js/samples/each-block-keyed-animated/expected.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ function foo(node, animation, params) {
1818
function get_each_context(ctx, list, i) {
1919
const child_ctx = Object.create(ctx);
2020
child_ctx.thing = list[i];
21-
child_ctx.each_value = list;
22-
child_ctx.thing_index = i;
2321
return child_ctx;
2422
}
2523

test/js/samples/each-block-keyed/expected.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { append, assign, blankObject, createComment, createElement, createText,
44
function get_each_context(ctx, list, i) {
55
const child_ctx = Object.create(ctx);
66
child_ctx.thing = list[i];
7-
child_ctx.each_value = list;
8-
child_ctx.thing_index = i;
97
return child_ctx;
108
}
119

0 commit comments

Comments
 (0)