From 962c815111aeb3e10ba5ebdb0e94594ccf4aaef6 Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Sun, 8 Sep 2019 00:46:20 -0700 Subject: [PATCH 1/7] Add tests --- test/runtime/samples/store-each-binding/_config.js | 14 ++++++++++++++ .../runtime/samples/store-each-binding/main.svelte | 13 +++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/runtime/samples/store-each-binding/_config.js create mode 100644 test/runtime/samples/store-each-binding/main.svelte diff --git a/test/runtime/samples/store-each-binding/_config.js b/test/runtime/samples/store-each-binding/_config.js new file mode 100644 index 0000000000..70776940dd --- /dev/null +++ b/test/runtime/samples/store-each-binding/_config.js @@ -0,0 +1,14 @@ +export default { + async test({ assert, target, window }) { + const input = target.querySelector('input'); + + const event = new window.Event('input'); + input.value = 'changed'; + await input.dispatchEvent(event); + + assert.htmlEqual(target.innerHTML, ` + +

changed

+ `); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/store-each-binding/main.svelte b/test/runtime/samples/store-each-binding/main.svelte new file mode 100644 index 0000000000..328d0eb63f --- /dev/null +++ b/test/runtime/samples/store-each-binding/main.svelte @@ -0,0 +1,13 @@ + + +{#each $items as item, index} + +{/each} + +

{$items[0].text}

\ No newline at end of file From 249b2a114c0b15e16bf487bef4872a8e98d107c5 Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Sun, 8 Sep 2019 00:48:47 -0700 Subject: [PATCH 2/7] Add destructuring tests --- .../store-each-binding-destructuring/_config.js | 14 ++++++++++++++ .../store-each-binding-destructuring/main.svelte | 13 +++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/runtime/samples/store-each-binding-destructuring/_config.js create mode 100644 test/runtime/samples/store-each-binding-destructuring/main.svelte diff --git a/test/runtime/samples/store-each-binding-destructuring/_config.js b/test/runtime/samples/store-each-binding-destructuring/_config.js new file mode 100644 index 0000000000..70776940dd --- /dev/null +++ b/test/runtime/samples/store-each-binding-destructuring/_config.js @@ -0,0 +1,14 @@ +export default { + async test({ assert, target, window }) { + const input = target.querySelector('input'); + + const event = new window.Event('input'); + input.value = 'changed'; + await input.dispatchEvent(event); + + assert.htmlEqual(target.innerHTML, ` + +

changed

+ `); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/store-each-binding-destructuring/main.svelte b/test/runtime/samples/store-each-binding-destructuring/main.svelte new file mode 100644 index 0000000000..9b29da1717 --- /dev/null +++ b/test/runtime/samples/store-each-binding-destructuring/main.svelte @@ -0,0 +1,13 @@ + + +{#each $items as { text }, index} + +{/each} + +

{$items[0].text}

\ No newline at end of file From 2ab77e249ba9969610f23164545d589d994d7001 Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Sun, 8 Sep 2019 00:49:30 -0700 Subject: [PATCH 3/7] Remove unused variables --- .../samples/store-each-binding-destructuring/main.svelte | 2 +- test/runtime/samples/store-each-binding/main.svelte | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtime/samples/store-each-binding-destructuring/main.svelte b/test/runtime/samples/store-each-binding-destructuring/main.svelte index 9b29da1717..7c7188203b 100644 --- a/test/runtime/samples/store-each-binding-destructuring/main.svelte +++ b/test/runtime/samples/store-each-binding-destructuring/main.svelte @@ -6,7 +6,7 @@ ]); -{#each $items as { text }, index} +{#each $items as { text }} {/each} diff --git a/test/runtime/samples/store-each-binding/main.svelte b/test/runtime/samples/store-each-binding/main.svelte index 328d0eb63f..1caeba2ceb 100644 --- a/test/runtime/samples/store-each-binding/main.svelte +++ b/test/runtime/samples/store-each-binding/main.svelte @@ -6,7 +6,7 @@ ]); -{#each $items as item, index} +{#each $items as item} {/each} From 0a4caeb7e915b73cde9d927e14036b86abd80104 Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Sun, 8 Sep 2019 00:58:37 -0700 Subject: [PATCH 4/7] Add store data to bindings --- src/compiler/compile/render_dom/Block.ts | 4 ++-- src/compiler/compile/render_dom/wrappers/EachBlock.ts | 6 +++++- .../compile/render_dom/wrappers/Element/Binding.ts | 10 ++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/compiler/compile/render_dom/Block.ts b/src/compiler/compile/render_dom/Block.ts index 59beae1a39..c1dfb31c4a 100644 --- a/src/compiler/compile/render_dom/Block.ts +++ b/src/compiler/compile/render_dom/Block.ts @@ -10,7 +10,7 @@ export interface BlockOptions { renderer?: Renderer; comment?: string; key?: string; - bindings?: Map; + bindings?: Map; dependencies?: Set; } @@ -27,7 +27,7 @@ export default class Block { dependencies: Set; - bindings: Map; + bindings: Map; builders: { init: CodeBuilder; diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index ecd55c9b1d..c8fdae54a2 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -121,11 +121,15 @@ export default class EachBlockWrapper extends Wrapper { view_length: fixed_length === null ? `${iterations}.[✂${c}-${c+4}✂]` : fixed_length }; + const store = node.expression.node.name[0] === '$' ? node.expression.node.name.slice(1) : null; + node.contexts.forEach(prop => { this.block.bindings.set(prop.key.name, { object: this.vars.each_block_value, property: this.index_name, - snippet: attach_head(`${this.vars.each_block_value}[${this.index_name}]`, prop.tail) + snippet: attach_head(`${this.vars.each_block_value}[${this.index_name}]`, prop.tail), + store, + tail: attach_head(`[${this.index_name}]`, prop.tail) }); }); diff --git a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts index 44372f8793..2fe54e7f33 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts @@ -249,7 +249,7 @@ function get_event_handler( snippet?: string; } { const value = get_value_from_dom(renderer, binding.parent, binding); - const store = binding.object[0] === '$' ? binding.object.slice(1) : null; + let store = binding.object[0] === '$' ? binding.object.slice(1) : null; let tail = ''; if (binding.node.expression.node.type === 'MemberExpression') { @@ -258,7 +258,13 @@ function get_event_handler( } if (binding.node.is_contextual) { - const { object, property, snippet } = block.bindings.get(name); + const binding = block.bindings.get(name); + const { object, property, snippet } = binding; + + if (binding.store) { + store = store || binding.store; + tail = `${binding.tail}${tail}`; + } return { uses_context: true, From cfc7e6af5b8c584969714ec3735c03e7abfdc63b Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Sun, 8 Sep 2019 01:12:13 -0700 Subject: [PATCH 5/7] Check expression type before getting store name --- src/compiler/compile/render_dom/wrappers/EachBlock.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index c8fdae54a2..a0a08fca0c 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -121,7 +121,11 @@ export default class EachBlockWrapper extends Wrapper { view_length: fixed_length === null ? `${iterations}.[✂${c}-${c+4}✂]` : fixed_length }; - const store = node.expression.node.name[0] === '$' ? node.expression.node.name.slice(1) : null; + const store = + node.expression.node.type === 'Identifier' && + node.expression.node.name[0] === '$' + ? node.expression.node.name.slice(1) + : null; node.contexts.forEach(prop => { this.block.bindings.set(prop.key.name, { From 63f69390fde7ced361e15b3e85e62748616f5ff1 Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Sun, 8 Sep 2019 01:14:33 -0700 Subject: [PATCH 6/7] Remove redundant store check --- src/compiler/compile/render_dom/wrappers/Element/Binding.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts index 2fe54e7f33..6eb0697c3c 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts @@ -262,7 +262,7 @@ function get_event_handler( const { object, property, snippet } = binding; if (binding.store) { - store = store || binding.store; + store = binding.store; tail = `${binding.tail}${tail}`; } From ab3b12b31085906bd22fe88cac782996317f5393 Mon Sep 17 00:00:00 2001 From: Bryan Terce Date: Sun, 8 Sep 2019 01:18:05 -0700 Subject: [PATCH 7/7] Test typos --- .../samples/store-each-binding-destructuring/main.svelte | 2 +- test/runtime/samples/store-each-binding/main.svelte | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtime/samples/store-each-binding-destructuring/main.svelte b/test/runtime/samples/store-each-binding-destructuring/main.svelte index 7c7188203b..0b68038e73 100644 --- a/test/runtime/samples/store-each-binding-destructuring/main.svelte +++ b/test/runtime/samples/store-each-binding-destructuring/main.svelte @@ -2,7 +2,7 @@ import { writable } from 'svelte/store'; const items = writable([ - { id: 0, text: 'inital' } + { id: 0, text: 'initial' } ]); diff --git a/test/runtime/samples/store-each-binding/main.svelte b/test/runtime/samples/store-each-binding/main.svelte index 1caeba2ceb..917d5c2001 100644 --- a/test/runtime/samples/store-each-binding/main.svelte +++ b/test/runtime/samples/store-each-binding/main.svelte @@ -2,7 +2,7 @@ import { writable } from 'svelte/store'; const items = writable([ - { id: 0, text: 'inital' } + { id: 0, text: 'initial' } ]);