From b92cb0cf72dd8776da339074aa62a77a043030a3 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Tue, 8 Apr 2025 23:07:46 +0300 Subject: [PATCH] fix: terminated state for `if/else` (#19404) --- lib/javascript/JavascriptParser.js | 24 +-- .../StatsTestCases.basictest.js.snap | 2 +- test/statsCases/track-returned/index.js | 139 +++++++++++++++++- test/statsCases/track-returned/test.config.js | 2 +- test/statsCases/track-returned/used70.js | 1 + test/statsCases/track-returned/used71.js | 1 + test/statsCases/track-returned/used72.js | 1 + test/statsCases/track-returned/used73.js | 1 + test/statsCases/track-returned/used74.js | 1 + test/statsCases/track-returned/used75.js | 1 + test/statsCases/track-returned/used76.js | 1 + test/statsCases/track-returned/used77.js | 1 + test/statsCases/track-returned/used78.js | 1 + 13 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 test/statsCases/track-returned/used70.js create mode 100644 test/statsCases/track-returned/used71.js create mode 100644 test/statsCases/track-returned/used72.js create mode 100644 test/statsCases/track-returned/used73.js create mode 100644 test/statsCases/track-returned/used74.js create mode 100644 test/statsCases/track-returned/used75.js create mode 100644 test/statsCases/track-returned/used76.js create mode 100644 test/statsCases/track-returned/used77.js create mode 100644 test/statsCases/track-returned/used78.js diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index 5d65bd5bc..69b9c49e6 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -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) { diff --git a/test/__snapshots__/StatsTestCases.basictest.js.snap b/test/__snapshots__/StatsTestCases.basictest.js.snap index 3c3dc0c2b..96a77d0ec 100644 --- a/test/__snapshots__/StatsTestCases.basictest.js.snap +++ b/test/__snapshots__/StatsTestCases.basictest.js.snap @@ -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" `; diff --git a/test/statsCases/track-returned/index.js b/test/statsCases/track-returned/index.js index 353781a08..8f40b44f8 100644 --- a/test/statsCases/track-returned/index.js +++ b/test/statsCases/track-returned/index.js @@ -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; diff --git a/test/statsCases/track-returned/test.config.js b/test/statsCases/track-returned/test.config.js index a2ada3430..d233210ff 100644 --- a/test/statsCases/track-returned/test.config.js +++ b/test/statsCases/track-returned/test.config.js @@ -1,5 +1,5 @@ module.exports = { validate(stats) { - expect(stats.compilation.modules.size).toBe(70); + expect(stats.compilation.modules.size).toBe(79); } }; diff --git a/test/statsCases/track-returned/used70.js b/test/statsCases/track-returned/used70.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used70.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used71.js b/test/statsCases/track-returned/used71.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used71.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used72.js b/test/statsCases/track-returned/used72.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used72.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used73.js b/test/statsCases/track-returned/used73.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used73.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used74.js b/test/statsCases/track-returned/used74.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used74.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used75.js b/test/statsCases/track-returned/used75.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used75.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used76.js b/test/statsCases/track-returned/used76.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used76.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used77.js b/test/statsCases/track-returned/used77.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used77.js @@ -0,0 +1 @@ +module.exports = 10; diff --git a/test/statsCases/track-returned/used78.js b/test/statsCases/track-returned/used78.js new file mode 100644 index 000000000..4387befdd --- /dev/null +++ b/test/statsCases/track-returned/used78.js @@ -0,0 +1 @@ +module.exports = 10;