Skip to content

Commit

Permalink
fix: conditional/logical expression should request a new tree-shaking (
Browse files Browse the repository at this point in the history
…#5481)

* fix: conditional/logical expression should request a new tree-shaking

* perf: rarely expressionsToBeDeoptimized.length > 0, so move it outside
  • Loading branch information
liuly0322 committed Apr 21, 2024
1 parent 38fe707 commit 190893a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
18 changes: 14 additions & 4 deletions src/ast/nodes/ConditionalExpression.ts
Expand Up @@ -46,12 +46,22 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
}

deoptimizeCache(): void {
this.isBranchResolutionAnalysed = false;
if (
this.usedBranch ||
this.isBranchResolutionAnalysed ||
this.expressionsToBeDeoptimized.length > 0
) {
// Request another pass because we need to ensure "include" runs again if it is rendered
this.scope.context.requestTreeshakingPass();
}
const { expressionsToBeDeoptimized } = this;
this.expressionsToBeDeoptimized = [];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
if (expressionsToBeDeoptimized.length > 0) {
this.expressionsToBeDeoptimized = [];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
}
this.isBranchResolutionAnalysed = false;
if (this.usedBranch !== null) {
const unusedBranch = this.usedBranch === this.consequent ? this.alternate : this.consequent;
this.usedBranch = null;
Expand Down
20 changes: 11 additions & 9 deletions src/ast/nodes/LogicalExpression.ts
Expand Up @@ -57,20 +57,22 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
}

deoptimizeCache(): void {
this.isBranchResolutionAnalysed = false;
if (this.expressionsToBeDeoptimized.length > 0) {
const {
scope: { context },
expressionsToBeDeoptimized
} = this;
if (
this.usedBranch ||
this.isBranchResolutionAnalysed ||
this.expressionsToBeDeoptimized.length > 0
) {
// Request another pass because we need to ensure "include" runs again if it is rendered
this.scope.context.requestTreeshakingPass();
}
const { expressionsToBeDeoptimized } = this;
if (expressionsToBeDeoptimized.length > 0) {
this.expressionsToBeDeoptimized = [];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
// Request another pass because we need to ensure "include" runs again if
// it is rendered
context.requestTreeshakingPass();
}
this.isBranchResolutionAnalysed = false;
if (this.usedBranch) {
const unusedBranch = this.usedBranch === this.left ? this.right : this.left;
this.usedBranch = null;
Expand Down
@@ -0,0 +1,3 @@
module.exports = defineTest({
description: 'a new tree-shaking is required so render will not fail'
});
16 changes: 16 additions & 0 deletions test/form/samples/request-tree-shaking-before-render/_expected.js
@@ -0,0 +1,16 @@
function s(t) {
t(x);
}

function f(b) {
return x.concat((b ? 1 : 0))
}

function w(b) {
f(b);
}

w(1);
s(() => {
return w(0)
});
16 changes: 16 additions & 0 deletions test/form/samples/request-tree-shaking-before-render/main.js
@@ -0,0 +1,16 @@
function s(t) {
t(x)
}

function f(b) {
return x.concat((b ? 1 : 0))
}

function w(b) {
f(b)
}

w(1)
s(() => {
return w(0)
})

0 comments on commit 190893a

Please sign in to comment.