Skip to content

Commit

Permalink
dont generate code for non-updating if block - fixes #3447
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 30, 2019
1 parent 13b1a80 commit 334323f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 62 deletions.
98 changes: 49 additions & 49 deletions src/compiler/compile/render_dom/wrappers/IfBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});`);
Expand Down
14 changes: 1 addition & 13 deletions test/js/samples/if-block-complex/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down

0 comments on commit 334323f

Please sign in to comment.