Skip to content

Commit 190893a

Browse files
authored
fix: conditional/logical expression should request a new tree-shaking (#5481)
* fix: conditional/logical expression should request a new tree-shaking * perf: rarely expressionsToBeDeoptimized.length > 0, so move it outside
1 parent 38fe707 commit 190893a

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

src/ast/nodes/ConditionalExpression.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,22 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
4646
}
4747

4848
deoptimizeCache(): void {
49-
this.isBranchResolutionAnalysed = false;
49+
if (
50+
this.usedBranch ||
51+
this.isBranchResolutionAnalysed ||
52+
this.expressionsToBeDeoptimized.length > 0
53+
) {
54+
// Request another pass because we need to ensure "include" runs again if it is rendered
55+
this.scope.context.requestTreeshakingPass();
56+
}
5057
const { expressionsToBeDeoptimized } = this;
51-
this.expressionsToBeDeoptimized = [];
52-
for (const expression of expressionsToBeDeoptimized) {
53-
expression.deoptimizeCache();
58+
if (expressionsToBeDeoptimized.length > 0) {
59+
this.expressionsToBeDeoptimized = [];
60+
for (const expression of expressionsToBeDeoptimized) {
61+
expression.deoptimizeCache();
62+
}
5463
}
64+
this.isBranchResolutionAnalysed = false;
5565
if (this.usedBranch !== null) {
5666
const unusedBranch = this.usedBranch === this.consequent ? this.alternate : this.consequent;
5767
this.usedBranch = null;

src/ast/nodes/LogicalExpression.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,22 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
5757
}
5858

5959
deoptimizeCache(): void {
60-
this.isBranchResolutionAnalysed = false;
61-
if (this.expressionsToBeDeoptimized.length > 0) {
62-
const {
63-
scope: { context },
64-
expressionsToBeDeoptimized
65-
} = this;
60+
if (
61+
this.usedBranch ||
62+
this.isBranchResolutionAnalysed ||
63+
this.expressionsToBeDeoptimized.length > 0
64+
) {
65+
// Request another pass because we need to ensure "include" runs again if it is rendered
66+
this.scope.context.requestTreeshakingPass();
67+
}
68+
const { expressionsToBeDeoptimized } = this;
69+
if (expressionsToBeDeoptimized.length > 0) {
6670
this.expressionsToBeDeoptimized = [];
6771
for (const expression of expressionsToBeDeoptimized) {
6872
expression.deoptimizeCache();
6973
}
70-
// Request another pass because we need to ensure "include" runs again if
71-
// it is rendered
72-
context.requestTreeshakingPass();
7374
}
75+
this.isBranchResolutionAnalysed = false;
7476
if (this.usedBranch) {
7577
const unusedBranch = this.usedBranch === this.left ? this.right : this.left;
7678
this.usedBranch = null;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = defineTest({
2+
description: 'a new tree-shaking is required so render will not fail'
3+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function s(t) {
2+
t(x);
3+
}
4+
5+
function f(b) {
6+
return x.concat((b ? 1 : 0))
7+
}
8+
9+
function w(b) {
10+
f(b);
11+
}
12+
13+
w(1);
14+
s(() => {
15+
return w(0)
16+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function s(t) {
2+
t(x)
3+
}
4+
5+
function f(b) {
6+
return x.concat((b ? 1 : 0))
7+
}
8+
9+
function w(b) {
10+
f(b)
11+
}
12+
13+
w(1)
14+
s(() => {
15+
return w(0)
16+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy