Skip to content

Commit 1f0af18

Browse files
committed
address feedback
1 parent 269b460 commit 1f0af18

File tree

2 files changed

+24
-9
lines changed
  • packages/svelte/src
    • compiler/phases/3-transform/client/visitors
    • internal/client/dom/blocks

2 files changed

+24
-9
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,44 @@ import * as b from '../../../../utils/builders.js';
99
*/
1010
export function IfBlock(node, context) {
1111
context.state.template.push('<!>');
12+
const statements = [];
1213

1314
const consequent = /** @type {BlockStatement} */ (context.visit(node.consequent));
1415
const consequent_id = context.state.scope.generate('consequent');
1516

16-
context.state.init.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));
17+
statements.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));
1718

1819
let alternate_id;
1920

2021
if (node.alternate) {
2122
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
2223
alternate_id = context.state.scope.generate('alternate');
23-
context.state.init.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate)));
24+
statements.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate)));
2425
}
2526

2627
/** @type {Expression[]} */
2728
const args = [
2829
context.state.node,
2930
b.arrow(
30-
[b.id('$$branch')],
31+
[b.id('$$render')],
3132
b.block([
3233
b.if(
3334
/** @type {Expression} */ (context.visit(node.test)),
34-
b.stmt(b.call(b.id('$$branch'), b.literal(0), b.id(consequent_id))),
35+
b.stmt(
36+
b.call(
37+
b.id('$$render'),
38+
b.id(consequent_id),
39+
node.alternate ? b.literal(true) : undefined
40+
)
41+
),
3542
alternate_id
36-
? b.stmt(b.call(b.id('$$branch'), b.literal(1), b.id(alternate_id)))
43+
? b.stmt(
44+
b.call(
45+
b.id('$$render'),
46+
b.id(alternate_id),
47+
node.alternate ? b.literal(false) : undefined
48+
)
49+
)
3750
: undefined
3851
)
3952
])
@@ -65,5 +78,7 @@ export function IfBlock(node, context) {
6578
args.push(b.literal(true));
6679
}
6780

68-
context.state.init.push(b.stmt(b.call('$.if', ...args)));
81+
statements.push(b.stmt(b.call('$.if', ...args)));
82+
83+
context.state.init.push(b.block(statements));
6984
}

packages/svelte/src/internal/client/dom/blocks/if.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { HYDRATION_START_ELSE } from '../../../../constants.js';
1313

1414
/**
1515
* @param {TemplateNode} node
16-
* @param {(branch: (flag: 0 | 1, fn: (anchor: Node) => void) => void) => void} fn
16+
* @param {(branch: (fn: (anchor: Node) => void, flag?: boolean) => void) => void} fn
1717
* @param {boolean} [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local'
1818
* @returns {void}
1919
*/
@@ -37,9 +37,9 @@ export function if_block(node, fn, elseif = false) {
3737

3838
var has_branch = false;
3939

40-
const set_branch = (/** @type {0 | 1} */ flag, /** @type {(anchor: Node) => void} */ fn) => {
40+
const set_branch = (/** @type {(anchor: Node) => void} */ fn, flag = true) => {
4141
has_branch = true;
42-
update_branch(flag === 0, fn);
42+
update_branch(flag, fn);
4343
};
4444

4545
const update_branch = (

0 commit comments

Comments
 (0)