Skip to content

Commit 9c9f37c

Browse files
committed
fix code generation for if-else with static conditions - fixes #3505
1 parent c6b53b6 commit 9c9f37c

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

src/compiler/compile/render_dom/wrappers/IfBlock.ts

+58-33
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class IfBlockBranch extends Wrapper {
7474
export default class IfBlockWrapper extends Wrapper {
7575
node: IfBlock;
7676
branches: IfBlockBranch[];
77+
needs_update = false;
7778

7879
var = 'if_block';
7980

@@ -112,10 +113,16 @@ export default class IfBlockWrapper extends Wrapper {
112113
block.add_dependencies(node.expression.dependencies);
113114

114115
if (branch.block.dependencies.size > 0) {
116+
// the condition, or its contents, is dynamic
115117
is_dynamic = true;
116118
block.add_dependencies(branch.block.dependencies);
117119
}
118120

121+
if (branch.dependencies && branch.dependencies.length > 0) {
122+
// the condition itself is dynamic
123+
this.needs_update = true;
124+
}
125+
119126
if (branch.block.has_intros) has_intros = true;
120127
if (branch.block.has_outros) has_outros = true;
121128

@@ -239,15 +246,29 @@ export default class IfBlockWrapper extends Wrapper {
239246
const current_block_type_and = has_else ? '' : `${current_block_type} && `;
240247

241248
/* eslint-disable @typescript-eslint/indent,indent */
242-
block.builders.init.add_block(deindent`
243-
function ${select_block_type}(changed, ctx) {
244-
${this.branches.map(({ dependencies, condition, snippet, block }) => condition
245-
? deindent`
246-
${snippet && `if ((${condition} == null) || ${dependencies.map(n => `changed.${n}`).join(' || ')}) ${condition} = !!(${snippet})`}
247-
if (${condition}) return ${block.name};`
248-
: `return ${block.name};`)}
249-
}
250-
`);
249+
if (this.needs_update) {
250+
block.builders.init.add_block(deindent`
251+
function ${select_block_type}(changed, ctx) {
252+
${this.branches.map(({ dependencies, condition, snippet, block }) => condition
253+
? deindent`
254+
${snippet && (
255+
dependencies.length > 0
256+
? `if ((${condition} == null) || ${dependencies.map(n => `changed.${n}`).join(' || ')}) ${condition} = !!(${snippet})`
257+
: `if (${condition} == null) ${condition} = !!(${snippet})`
258+
)}
259+
if (${condition}) return ${block.name};`
260+
: `return ${block.name};`)}
261+
}
262+
`);
263+
} else {
264+
block.builders.init.add_block(deindent`
265+
function ${select_block_type}(changed, ctx) {
266+
${this.branches.map(({ condition, snippet, block }) => condition
267+
? `if (${snippet}) return ${block.name};`
268+
: `return ${block.name};`)}
269+
}
270+
`);
271+
}
251272
/* eslint-enable @typescript-eslint/indent,indent */
252273

253274
block.builders.init.add_block(deindent`
@@ -261,32 +282,36 @@ export default class IfBlockWrapper extends Wrapper {
261282
`${if_name}${name}.m(${initial_mount_node}, ${anchor_node});`
262283
);
263284

264-
const update_mount_node = this.get_update_mount_node(anchor);
265-
266-
const change_block = deindent`
267-
${if_name}${name}.d(1);
268-
${name} = ${current_block_type_and}${current_block_type}(ctx);
269-
if (${name}) {
270-
${name}.c();
271-
${has_transitions && `@transition_in(${name}, 1);`}
272-
${name}.m(${update_mount_node}, ${anchor});
273-
}
274-
`;
285+
if (this.needs_update) {
286+
const update_mount_node = this.get_update_mount_node(anchor);
275287

276-
if (dynamic) {
277-
block.builders.update.add_block(deindent`
278-
if (${current_block_type} === (${current_block_type} = ${select_block_type}(changed, ctx)) && ${name}) {
279-
${name}.p(changed, ctx);
280-
} else {
281-
${change_block}
282-
}
283-
`);
284-
} else {
285-
block.builders.update.add_block(deindent`
286-
if (${current_block_type} !== (${current_block_type} = ${select_block_type}(changed, ctx))) {
287-
${change_block}
288+
const change_block = deindent`
289+
${if_name}${name}.d(1);
290+
${name} = ${current_block_type_and}${current_block_type}(ctx);
291+
if (${name}) {
292+
${name}.c();
293+
${has_transitions && `@transition_in(${name}, 1);`}
294+
${name}.m(${update_mount_node}, ${anchor});
288295
}
289-
`);
296+
`;
297+
298+
if (dynamic) {
299+
block.builders.update.add_block(deindent`
300+
if (${current_block_type} === (${current_block_type} = ${select_block_type}(changed, ctx)) && ${name}) {
301+
${name}.p(changed, ctx);
302+
} else {
303+
${change_block}
304+
}
305+
`);
306+
} else {
307+
block.builders.update.add_block(deindent`
308+
if (${current_block_type} !== (${current_block_type} = ${select_block_type}(changed, ctx))) {
309+
${change_block}
310+
}
311+
`);
312+
}
313+
} else if (dynamic) {
314+
block.builders.update.add_line(`${name}.p(changed, ctx);`);
290315
}
291316

292317
block.builders.destroy.add_line(`${if_name}${name}.d(${detaching});`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
html: 'eee'
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{#if "Eva".startsWith('E')}
2+
eee
3+
{:else}
4+
rrr
5+
{/if}

0 commit comments

Comments
 (0)