From 6886d3260f7bd36db47299287bfa99e9ff317b49 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Tue, 25 Jun 2019 07:43:00 -0400 Subject: [PATCH 1/5] remove components without props from each block - fixes #3035 --- .../compile/render_dom/wrappers/EachBlock.ts | 2 +- .../each-block-component-no-props/Child.svelte | 1 + .../each-block-component-no-props/_config.js | 10 ++++++++++ .../each-block-component-no-props/main.svelte | 13 +++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/each-block-component-no-props/Child.svelte create mode 100644 test/runtime/samples/each-block-component-no-props/_config.js create mode 100644 test/runtime/samples/each-block-component-no-props/main.svelte diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index 9e4f0689fa..b57ce46b29 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -465,7 +465,7 @@ export default class EachBlockWrapper extends Wrapper { `); remove_old_blocks = deindent` @group_outros(); - for (; #i < ${view_length}; #i += 1) ${out}(#i); + for (#i = ${this.vars.each_block_value}.${length}; #i < ${view_length}; #i += 1) ${out}(#i); @check_outros(); `; } else { diff --git a/test/runtime/samples/each-block-component-no-props/Child.svelte b/test/runtime/samples/each-block-component-no-props/Child.svelte new file mode 100644 index 0000000000..86ef4f3319 --- /dev/null +++ b/test/runtime/samples/each-block-component-no-props/Child.svelte @@ -0,0 +1 @@ +

hello

\ No newline at end of file diff --git a/test/runtime/samples/each-block-component-no-props/_config.js b/test/runtime/samples/each-block-component-no-props/_config.js new file mode 100644 index 0000000000..6a2bc67697 --- /dev/null +++ b/test/runtime/samples/each-block-component-no-props/_config.js @@ -0,0 +1,10 @@ +export default { + html: ` +

hello

+ `, + + async test({ assert, component, target }) { + await component.remove(); + assert.htmlEqual(target.innerHTML, ``); + } +}; diff --git a/test/runtime/samples/each-block-component-no-props/main.svelte b/test/runtime/samples/each-block-component-no-props/main.svelte new file mode 100644 index 0000000000..15139fa17b --- /dev/null +++ b/test/runtime/samples/each-block-component-no-props/main.svelte @@ -0,0 +1,13 @@ + + +{#each items as item} + +{/each} From 525e4c885311d2a1769c7cb0cd127586eca1c7ad Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Tue, 25 Jun 2019 08:05:10 -0400 Subject: [PATCH 2/5] actually this test should fail --- .../samples/each-block-component-no-props/_config.js | 6 ++++++ .../samples/each-block-component-no-props/main.svelte | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/test/runtime/samples/each-block-component-no-props/_config.js b/test/runtime/samples/each-block-component-no-props/_config.js index 6a2bc67697..e8344496ca 100644 --- a/test/runtime/samples/each-block-component-no-props/_config.js +++ b/test/runtime/samples/each-block-component-no-props/_config.js @@ -6,5 +6,11 @@ export default { async test({ assert, component, target }) { await component.remove(); assert.htmlEqual(target.innerHTML, ``); + + await component.add(); + assert.htmlEqual(target.innerHTML, `

hello

`); + + await component.remove(); + assert.htmlEqual(target.innerHTML, ``); } }; diff --git a/test/runtime/samples/each-block-component-no-props/main.svelte b/test/runtime/samples/each-block-component-no-props/main.svelte index 15139fa17b..01299fe314 100644 --- a/test/runtime/samples/each-block-component-no-props/main.svelte +++ b/test/runtime/samples/each-block-component-no-props/main.svelte @@ -3,6 +3,10 @@ let items = [1]; + export function add() { + items = [1]; + } + export function remove() { items = []; } From 6f9230a915d8b997ed1ab7e164ecfd467f20ee9d Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Tue, 25 Jun 2019 08:43:09 -0400 Subject: [PATCH 3/5] fix #3035 for real --- .../compile/render_dom/wrappers/EachBlock.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index b57ce46b29..6f592d3869 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -445,13 +445,18 @@ export default class EachBlockWrapper extends Wrapper { } ` : deindent` - ${iterations}[#i] = ${create_each_block}(child_ctx); - ${iterations}[#i].c(); - ${has_transitions && `@transition_in(${this.vars.iterations}[#i], 1);`} - ${iterations}[#i].m(${update_mount_node}, ${anchor}); + if (${iterations}[#i]) { + ${has_transitions && `@transition_in(${this.vars.iterations}[#i], 1);`} + } else { + ${iterations}[#i] = ${create_each_block}(child_ctx); + ${iterations}[#i].c(); + ${has_transitions && `@transition_in(${this.vars.iterations}[#i], 1);`} + ${iterations}[#i].m(${update_mount_node}, ${anchor}); + } + `; - const start = this.block.has_update_method ? '0' : `${view_length}`; + const start = this.block.has_update_method ? '0' : `old_length`; let remove_old_blocks; @@ -470,7 +475,7 @@ export default class EachBlockWrapper extends Wrapper { `; } else { remove_old_blocks = deindent` - for (${this.block.has_update_method ? `` : `#i = ${this.vars.each_block_value}.${length}`}; #i < ${view_length}; #i += 1) { + for (${this.block.has_update_method ? `` : `#i = ${this.vars.each_block_value}.${length}`}; #i < ${this.block.has_update_method ? view_length : 'old_length'}; #i += 1) { ${iterations}[#i].d(1); } ${!fixed_length && `${view_length} = ${this.vars.each_block_value}.${length};`} @@ -478,6 +483,7 @@ export default class EachBlockWrapper extends Wrapper { } const update = deindent` + ${!this.block.has_update_method && `const old_length = ${this.vars.each_block_value}.length;`} ${this.vars.each_block_value} = ${snippet}; for (var #i = ${start}; #i < ${this.vars.each_block_value}.${length}; #i += 1) { From 5a732658cecb4a021c4cfaa6a32f58e20cf6f85d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 25 Jun 2019 10:41:42 -0400 Subject: [PATCH 4/5] prevent empty if block --- .../compile/render_dom/wrappers/EachBlock.ts | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index 6f592d3869..c2f6c50249 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -444,17 +444,24 @@ export default class EachBlockWrapper extends Wrapper { ${iterations}[#i].m(${update_mount_node}, ${anchor}); } ` - : deindent` - if (${iterations}[#i]) { - ${has_transitions && `@transition_in(${this.vars.iterations}[#i], 1);`} - } else { - ${iterations}[#i] = ${create_each_block}(child_ctx); - ${iterations}[#i].c(); - ${has_transitions && `@transition_in(${this.vars.iterations}[#i], 1);`} - ${iterations}[#i].m(${update_mount_node}, ${anchor}); - } - - `; + : has_transitions + ? deindent` + if (${iterations}[#i]) { + @transition_in(${this.vars.iterations}[#i], 1); + } else { + ${iterations}[#i] = ${create_each_block}(child_ctx); + ${iterations}[#i].c(); + @transition_in(${this.vars.iterations}[#i], 1); + ${iterations}[#i].m(${update_mount_node}, ${anchor}); + } + ` + : deindent` + if (!${iterations}[#i]) { + ${iterations}[#i] = ${create_each_block}(child_ctx); + ${iterations}[#i].c(); + ${iterations}[#i].m(${update_mount_node}, ${anchor}); + } + `; const start = this.block.has_update_method ? '0' : `old_length`; From a93f089765bcc89aba482c1e4fba07e1d77b896c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 25 Jun 2019 10:49:03 -0400 Subject: [PATCH 5/5] doh --- src/compiler/compile/render_dom/wrappers/EachBlock.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index c2f6c50249..06d8d5593f 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -463,7 +463,7 @@ export default class EachBlockWrapper extends Wrapper { } `; - const start = this.block.has_update_method ? '0' : `old_length`; + const start = this.block.has_update_method ? '0' : `#old_length`; let remove_old_blocks; @@ -482,7 +482,7 @@ export default class EachBlockWrapper extends Wrapper { `; } else { remove_old_blocks = deindent` - for (${this.block.has_update_method ? `` : `#i = ${this.vars.each_block_value}.${length}`}; #i < ${this.block.has_update_method ? view_length : 'old_length'}; #i += 1) { + for (${this.block.has_update_method ? `` : `#i = ${this.vars.each_block_value}.${length}`}; #i < ${this.block.has_update_method ? view_length : '#old_length'}; #i += 1) { ${iterations}[#i].d(1); } ${!fixed_length && `${view_length} = ${this.vars.each_block_value}.${length};`} @@ -490,7 +490,7 @@ export default class EachBlockWrapper extends Wrapper { } const update = deindent` - ${!this.block.has_update_method && `const old_length = ${this.vars.each_block_value}.length;`} + ${!this.block.has_update_method && `const #old_length = ${this.vars.each_block_value}.length;`} ${this.vars.each_block_value} = ${snippet}; for (var #i = ${start}; #i < ${this.vars.each_block_value}.${length}; #i += 1) {