From 7f587010854f87cf65e9541250f9870140b6622a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 1 May 2018 21:24:22 -0400 Subject: [PATCH 1/2] evaluate each block key in child scope - fixes #1397 --- src/compile/nodes/EachBlock.ts | 8 +++---- .../dev-warning-missing-data-each/_config.js | 22 +++++++++++++++++++ .../dev-warning-missing-data-each/main.html | 3 +++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 test/runtime/samples/dev-warning-missing-data-each/_config.js create mode 100644 test/runtime/samples/dev-warning-missing-data-each/main.html diff --git a/src/compile/nodes/EachBlock.ts b/src/compile/nodes/EachBlock.ts index 6d771dbf87..35376c9e2b 100644 --- a/src/compile/nodes/EachBlock.ts +++ b/src/compile/nodes/EachBlock.ts @@ -31,10 +31,6 @@ export default class EachBlock extends Node { this.context = info.context.name || 'each'; // TODO this is used to facilitate binding; currently fails with destructuring this.index = info.index; - this.key = info.key - ? new Expression(compiler, this, scope, info.key) - : null; - this.scope = scope.child(); this.contexts = []; @@ -50,6 +46,10 @@ export default class EachBlock extends Node { this.scope.add(this.index, dependencies); } + this.key = info.key + ? new Expression(compiler, this, this.scope, info.key) + : null; + this.children = mapChildren(compiler, this, this.scope, info.children); this.else = info.else diff --git a/test/runtime/samples/dev-warning-missing-data-each/_config.js b/test/runtime/samples/dev-warning-missing-data-each/_config.js new file mode 100644 index 0000000000..a8eb59216a --- /dev/null +++ b/test/runtime/samples/dev-warning-missing-data-each/_config.js @@ -0,0 +1,22 @@ +export default { + dev: true, + + data: { + letters: [ + { + id: 1, + char: 'a', + }, + { + id: 2, + char: 'b', + }, + { + id: 3, + char: 'c', + }, + ], + }, + + warnings: [], +}; diff --git a/test/runtime/samples/dev-warning-missing-data-each/main.html b/test/runtime/samples/dev-warning-missing-data-each/main.html new file mode 100644 index 0000000000..6b6a72204e --- /dev/null +++ b/test/runtime/samples/dev-warning-missing-data-each/main.html @@ -0,0 +1,3 @@ +{#each letters as letter (letter.id)} +
{letter.char}
+{/each} \ No newline at end of file From 3f012bbdb8e5fca204cfb330ad812cf67968ca23 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 1 May 2018 21:55:08 -0400 Subject: [PATCH 2/2] create key before testing for the existence of key. doh --- src/compile/nodes/EachBlock.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compile/nodes/EachBlock.ts b/src/compile/nodes/EachBlock.ts index 35376c9e2b..d9c93fc036 100644 --- a/src/compile/nodes/EachBlock.ts +++ b/src/compile/nodes/EachBlock.ts @@ -40,16 +40,16 @@ export default class EachBlock extends Node { this.scope.add(context.key.name, this.expression.dependencies); }); + this.key = info.key + ? new Expression(compiler, this, this.scope, info.key) + : null; + if (this.index) { // index can only change if this is a keyed each block const dependencies = this.key ? this.expression.dependencies : []; this.scope.add(this.index, dependencies); } - this.key = info.key - ? new Expression(compiler, this, this.scope, info.key) - : null; - this.children = mapChildren(compiler, this, this.scope, info.children); this.else = info.else