Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/ast/nodes/ConditionalExpression.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
})