Skip to content

Commit 9c69cd8

Browse files
committed
chore: make if blocks dead code eliminable
1 parent 3481857 commit 9c69cd8

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

.changeset/beige-windows-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: make if blocks dead code eliminable

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,35 @@ export function IfBlock(node, context) {
1111
context.state.template.push('<!>');
1212

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

16+
context.state.init.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));
17+
18+
let alternate_id;
19+
20+
if (node.alternate) {
21+
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
22+
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+
}
25+
26+
/** @type {Expression[]} */
1527
const args = [
1628
context.state.node,
17-
b.thunk(/** @type {Expression} */ (context.visit(node.test))),
18-
b.arrow([b.id('$$anchor')], consequent)
29+
b.arrow(
30+
[b.id('$$branch')],
31+
b.block([
32+
b.if(
33+
/** @type {Expression} */ (context.visit(node.test)),
34+
b.stmt(b.call(b.id('$$branch'), b.literal(0), b.id(consequent_id))),
35+
alternate_id
36+
? b.stmt(b.call(b.id('$$branch'), b.literal(1), b.id(alternate_id)))
37+
: undefined
38+
)
39+
])
40+
)
1941
];
2042

21-
if (node.alternate || node.elseif) {
22-
args.push(
23-
node.alternate
24-
? b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.alternate)))
25-
: b.literal(null)
26-
);
27-
}
28-
2943
if (node.elseif) {
3044
// We treat this...
3145
//

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

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

1414
/**
1515
* @param {TemplateNode} node
16-
* @param {() => boolean} get_condition
17-
* @param {(anchor: Node) => void} consequent_fn
18-
* @param {null | ((anchor: Node) => void)} [alternate_fn]
16+
* @param {(branch: (flag: 0 | 1, fn: (anchor: Node) => void) => void) => void} fn
1917
* @param {boolean} [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local'
2018
* @returns {void}
2119
*/
22-
export function if_block(node, get_condition, consequent_fn, alternate_fn = null, elseif = false) {
20+
export function if_block(node, fn, elseif = false) {
2321
if (hydrating) {
2422
hydrate_next();
2523
}
@@ -37,8 +35,18 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
3735

3836
var flags = elseif ? EFFECT_TRANSPARENT : 0;
3937

40-
block(() => {
41-
if (condition === (condition = !!get_condition())) return;
38+
var has_branch = false;
39+
40+
const set_branch = (/** @type {0 | 1} */ flag, /** @type {(anchor: Node) => void} */ fn) => {
41+
has_branch = true;
42+
update_branch(flag === 0, fn);
43+
};
44+
45+
const update_branch = (
46+
/** @type {boolean | null} */ new_condition,
47+
/** @type {null | ((anchor: Node) => void)} */ fn
48+
) => {
49+
if (condition === (condition = new_condition)) return;
4250

4351
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
4452
let mismatch = false;
@@ -60,8 +68,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
6068
if (condition) {
6169
if (consequent_effect) {
6270
resume_effect(consequent_effect);
63-
} else {
64-
consequent_effect = branch(() => consequent_fn(anchor));
71+
} else if (fn) {
72+
consequent_effect = branch(() => fn(anchor));
6573
}
6674

6775
if (alternate_effect) {
@@ -72,8 +80,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
7280
} else {
7381
if (alternate_effect) {
7482
resume_effect(alternate_effect);
75-
} else if (alternate_fn) {
76-
alternate_effect = branch(() => alternate_fn(anchor));
83+
} else if (fn) {
84+
alternate_effect = branch(() => fn(anchor));
7785
}
7886

7987
if (consequent_effect) {
@@ -87,6 +95,14 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
8795
// continue in hydration mode
8896
set_hydrating(true);
8997
}
98+
};
99+
100+
block(() => {
101+
has_branch = false;
102+
fn(set_branch);
103+
if (!has_branch) {
104+
update_branch(null, null);
105+
}
90106
}, flags);
91107

92108
if (hydrating) {

packages/svelte/tests/runtime-runes/samples/bind-this-no-state/main.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import { tick } from 'svelte';
3-
3+
44
let selected = $state(-1);
55
let current = $state();
66
@@ -27,4 +27,4 @@
2727

2828
<hr />
2929

30-
<p>{current ?? '...'}</p>
30+
<p>{current ?? '...'}</p>

0 commit comments

Comments
 (0)