From 10548373d569966dbac1a1a052f2731a2c293c87 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 30 Aug 2019 12:02:20 -0400 Subject: [PATCH] dont generate code for non-updating if block - fixes #3447 --- .../compile/render_dom/wrappers/IfBlock.ts | 98 +++++++++---------- test/js/samples/if-block-complex/expected.js | 14 +-- 2 files changed, 50 insertions(+), 62 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/IfBlock.ts b/src/compiler/compile/render_dom/wrappers/IfBlock.ts index fb364a5343..f12929ffb1 100644 --- a/src/compiler/compile/render_dom/wrappers/IfBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/IfBlock.ts @@ -441,58 +441,58 @@ export default class IfBlockWrapper extends Wrapper { `if (${name}) ${name}.m(${initial_mount_node}, ${anchor_node});` ); - const update_mount_node = this.get_update_mount_node(anchor); - - const enter = dynamic - ? deindent` - if (${name}) { - ${name}.p(changed, ctx); - ${has_transitions && `@transition_in(${name}, 1);`} - } else { - ${name} = ${branch.block.name}(ctx); - ${name}.c(); - ${has_transitions && `@transition_in(${name}, 1);`} - ${name}.m(${update_mount_node}, ${anchor}); - } - ` - : deindent` - if (!${name}) { - ${name} = ${branch.block.name}(ctx); - ${name}.c(); - ${has_transitions && `@transition_in(${name}, 1);`} - ${name}.m(${update_mount_node}, ${anchor}); - ${has_transitions && `} else { - @transition_in(${name}, 1);`} - } - `; + if (!branch.dependencies || branch.dependencies.length > 0) { + const update_mount_node = this.get_update_mount_node(anchor); - if (branch.snippet && branch.dependencies.length) { - block.builders.update.add_block(`if (${branch.dependencies.map(n => `changed.${n}`).join(' || ')}) ${branch.condition} = ${branch.snippet}`); - } + const enter = dynamic + ? deindent` + if (${name}) { + ${name}.p(changed, ctx); + ${has_transitions && `@transition_in(${name}, 1);`} + } else { + ${name} = ${branch.block.name}(ctx); + ${name}.c(); + ${has_transitions && `@transition_in(${name}, 1);`} + ${name}.m(${update_mount_node}, ${anchor}); + } + ` + : deindent` + if (!${name}) { + ${name} = ${branch.block.name}(ctx); + ${name}.c(); + ${has_transitions && `@transition_in(${name}, 1);`} + ${name}.m(${update_mount_node}, ${anchor}); + } ${has_transitions && `else @transition_in(${name}, 1);`} + `; + + if (branch.snippet) { + block.builders.update.add_block(`if (${branch.dependencies.map(n => `changed.${n}`).join(' || ')}) ${branch.condition} = ${branch.snippet}`); + } - // no `p()` here — we don't want to update outroing nodes, - // as that will typically result in glitching - if (branch.block.has_outro_method) { - block.builders.update.add_block(deindent` - if (${branch.condition}) { - ${enter} - } else if (${name}) { - @group_outros(); - @transition_out(${name}, 1, 1, () => { + // no `p()` here — we don't want to update outroing nodes, + // as that will typically result in glitching + if (branch.block.has_outro_method) { + block.builders.update.add_block(deindent` + if (${branch.condition}) { + ${enter} + } else if (${name}) { + @group_outros(); + @transition_out(${name}, 1, 1, () => { + ${name} = null; + }); + @check_outros(); + } + `); + } else { + block.builders.update.add_block(deindent` + if (${branch.condition}) { + ${enter} + } else if (${name}) { + ${name}.d(1); ${name} = null; - }); - @check_outros(); - } - `); - } else { - block.builders.update.add_block(deindent` - if (${branch.condition}) { - ${enter} - } else if (${name}) { - ${name}.d(1); - ${name} = null; - } - `); + } + `); + } } block.builders.destroy.add_line(`${if_name}${name}.d(${detaching});`); diff --git a/test/js/samples/if-block-complex/expected.js b/test/js/samples/if-block-complex/expected.js index 8dffddef33..f0da492e3c 100644 --- a/test/js/samples/if-block-complex/expected.js +++ b/test/js/samples/if-block-complex/expected.js @@ -49,19 +49,7 @@ function create_fragment(ctx) { insert(target, if_block_anchor, anchor); }, - p(changed, ctx) { - if (show_if) { - if (!if_block) { - if_block = create_if_block(ctx); - if_block.c(); - if_block.m(if_block_anchor.parentNode, if_block_anchor); - } - } else if (if_block) { - if_block.d(1); - if_block = null; - } - }, - + p: noop, i: noop, o: noop,