diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js index 65b8d09d6f..0249611a89 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js @@ -9,31 +9,44 @@ import * as b from '../../../../utils/builders.js'; */ export function IfBlock(node, context) { context.state.template.push(''); + const statements = []; const consequent = /** @type {BlockStatement} */ (context.visit(node.consequent)); const consequent_id = context.state.scope.generate('consequent'); - context.state.init.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent))); + statements.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent))); let alternate_id; if (node.alternate) { const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate)); alternate_id = context.state.scope.generate('alternate'); - context.state.init.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate))); + statements.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate))); } /** @type {Expression[]} */ const args = [ context.state.node, b.arrow( - [b.id('$$branch')], + [b.id('$$render')], b.block([ b.if( /** @type {Expression} */ (context.visit(node.test)), - b.stmt(b.call(b.id('$$branch'), b.literal(0), b.id(consequent_id))), + b.stmt( + b.call( + b.id('$$render'), + b.id(consequent_id), + node.alternate ? b.literal(true) : undefined + ) + ), alternate_id - ? b.stmt(b.call(b.id('$$branch'), b.literal(1), b.id(alternate_id))) + ? b.stmt( + b.call( + b.id('$$render'), + b.id(alternate_id), + node.alternate ? b.literal(false) : undefined + ) + ) : undefined ) ]) @@ -65,5 +78,7 @@ export function IfBlock(node, context) { args.push(b.literal(true)); } - context.state.init.push(b.stmt(b.call('$.if', ...args))); + statements.push(b.stmt(b.call('$.if', ...args))); + + context.state.init.push(b.block(statements)); } diff --git a/packages/svelte/src/internal/client/dom/blocks/if.js b/packages/svelte/src/internal/client/dom/blocks/if.js index 734cd7a0ae..6a880f28bc 100644 --- a/packages/svelte/src/internal/client/dom/blocks/if.js +++ b/packages/svelte/src/internal/client/dom/blocks/if.js @@ -13,7 +13,7 @@ import { HYDRATION_START_ELSE } from '../../../../constants.js'; /** * @param {TemplateNode} node - * @param {(branch: (flag: 0 | 1, fn: (anchor: Node) => void) => void) => void} fn + * @param {(branch: (fn: (anchor: Node) => void, flag?: boolean) => void) => void} fn * @param {boolean} [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local' * @returns {void} */ @@ -37,9 +37,9 @@ export function if_block(node, fn, elseif = false) { var has_branch = false; - const set_branch = (/** @type {0 | 1} */ flag, /** @type {(anchor: Node) => void} */ fn) => { + const set_branch = (/** @type {(anchor: Node) => void} */ fn, flag = true) => { has_branch = true; - update_branch(flag === 0, fn); + update_branch(flag, fn); }; const update_branch = (