fix: terminated state for `if/else` (#19404)

This commit is contained in:
Alexander Akait 2025-04-08 23:07:46 +03:00 committed by GitHub
parent 2470d8e74c
commit b92cb0cf72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 162 additions and 14 deletions

View File

@ -2197,16 +2197,22 @@ class JavascriptParser extends Parser {
walkIfStatement(statement) {
const result = this.hooks.statementIf.call(statement);
if (result === undefined) {
this.inExecutedPath(false, () => {
this.walkExpression(statement.test);
this.walkNestedStatement(statement.consequent);
});
this.walkExpression(statement.test);
this.walkNestedStatement(statement.consequent);
this.inExecutedPath(true, () => {
if (statement.alternate) {
this.walkNestedStatement(statement.alternate);
}
});
const consequentTerminated = this.scope.terminated;
this.scope.terminated = undefined;
if (statement.alternate) {
this.walkNestedStatement(statement.alternate);
}
const alternateTerminated = this.scope.terminated;
this.scope.terminated =
consequentTerminated && alternateTerminated
? this.scope.terminated
: undefined;
} else {
this.inExecutedPath(true, () => {
if (result) {

View File

@ -4788,7 +4788,7 @@ exports[`StatsTestCases should print correct stats for track-returned 1`] = `
./used48.js X bytes [built] [code generated]
./used35.js X bytes [built] [code generated]
./used10.js X bytes [built] [code generated]
+ 56 modules
+ 65 modules
webpack x.x.x compiled successfully in X ms"
`;

View File

@ -306,7 +306,7 @@ it("should work correct for try catch and loops", () => {
require("./used15")
}
// require("fail35");
require("fail35");
}
function test7() {
@ -826,7 +826,7 @@ it("should work correct for if #8", () => {
} else
return;
require("fail");
require("./used70");
});
it("should work correct for if #9", () => {
@ -1082,7 +1082,7 @@ it("should work correct for if #22", () => {
return;
}
// require("fail");
require("fail");
} else {
return;
}
@ -1192,6 +1192,139 @@ it("should work correct for if #29", () => {
}
});
it("should work correct for if #30", () => {
if (rand()) {
rand();
return;
} else {
rand();
}
require("./used71")
});
it("should work correct for if #31", () => {
if (rand()) {
rand();
} else {
rand();
return;
}
require("./used72")
});
it("should work correct for if #32", () => {
if (rand()) {
rand();
return;
} else {
rand();
return;
}
require("fail");
});
it("should work correct for if #33", () => {
if (rand()) {
rand();
return;
} else if (rand()) {
rand()
} else {
rand();
return;
}
require("./used73");
});
it("should work correct for if #34", () => {
if (rand()) {
rand();
return;
} else if (rand()) {
rand();
return;
} else {
rand();
}
require("./used74");
});
it("should work correct for if #35", () => {
if (rand()) {
rand();
} else if (rand()) {
rand();
return;
} else {
rand();
return;
}
require("./used75");
});
it("should work correct for if #36", () => {
if (rand()) {
if (rand()) {
return;
} else if (rand()) {
return;
} else {
return;
}
require("fail");
} else if (rand()) {
rand();
} else {
rand();
}
require("./used76");
});
it("should work correct for if #37", () => {
if (rand()) {
rand();
} else if (rand()) {
if (rand()) {
return;
} else if (rand()) {
return;
} else {
return;
}
require("fail");
} else {
rand();
}
require("./used77");
});
it("should work correct for if #38", () => {
if (rand()) {
rand();
} else if (rand()) {
rand();
} else {
if (rand()) {
return;
} else if (rand()) {
return;
} else {
return;
}
require("fail");
}
require("./used78");
});
it("should not include unused assets", (done) => {
let a, b;

View File

@ -1,5 +1,5 @@
module.exports = {
validate(stats) {
expect(stats.compilation.modules.size).toBe(70);
expect(stats.compilation.modules.size).toBe(79);
}
};

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;

View File

@ -0,0 +1 @@
module.exports = 10;