From f1b3e9df1e4d009a653ae26f1ba51809f3364c95 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 26 Jun 2022 13:48:14 +0100 Subject: [PATCH] fix corner case in `inline` (#5527) fixes #5526 --- lib/compress.js | 8 +++++-- test/compress/arrows.js | 1 + test/compress/awaits.js | 2 ++ test/compress/classes.js | 1 + test/compress/collapse_vars.js | 2 +- test/compress/const.js | 1 + test/compress/default-values.js | 1 + test/compress/functions.js | 26 ++++++++++++++++------ test/compress/nullish.js | 1 + test/compress/spreads.js | 1 + test/compress/yields.js | 39 ++++++++++++++++++++++++++++++++- 11 files changed, 72 insertions(+), 11 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 10ef1f34..d8160222 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -13523,7 +13523,9 @@ Compressor.prototype.compress = function(node) { return make_node(AST_If, self, { condition: make_condition(self.left), body: inlined, - alternative: no_return ? null : make_node(AST_Return, self, { value: null }), + alternative: no_return ? null : make_node(AST_Return, self, { + value: make_node(AST_Undefined, self).transform(compressor), + }), }); function make_condition(cond) { @@ -13723,7 +13725,9 @@ Compressor.prototype.compress = function(node) { if (is_undefined(value)) return; node.value = make_node(AST_Await, call, { expression: value }); }); - body.push(make_node(AST_Return, call, { value: null })); + body.push(make_node(AST_Return, call, { + value: make_node(AST_Undefined, call).transform(compressor), + })); } return inlined; diff --git a/test/compress/arrows.js b/test/compress/arrows.js index b0c820b3..a9efd79c 100644 --- a/test/compress/arrows.js +++ b/test/compress/arrows.js @@ -1024,6 +1024,7 @@ issue_5414_1: { arrows: true, if_return: true, inline: true, + side_effects: true, toplevel: true, } input: { diff --git a/test/compress/awaits.js b/test/compress/awaits.js index c05bd5d6..1e1b482b 100644 --- a/test/compress/awaits.js +++ b/test/compress/awaits.js @@ -415,6 +415,7 @@ inline_block: { awaits: true, if_return: true, inline: true, + side_effects: true, } input: { console.log("foo"); @@ -450,6 +451,7 @@ inline_block_async: { awaits: true, if_return: true, inline: true, + side_effects: true, } input: { console.log("foo"); diff --git a/test/compress/classes.js b/test/compress/classes.js index 2dc7e3b0..3ea7e283 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -1815,6 +1815,7 @@ issue_4725_2: { options = { if_return: true, inline: true, + side_effects: true, } input: { "use strict"; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index b16450d7..d28fe175 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -9611,6 +9611,7 @@ inline_throw: { collapse_vars: true, inline: true, keep_fargs: false, + side_effects: true, unused: true, } input: { @@ -9630,7 +9631,6 @@ inline_throw: { try { (function(a) { throw a; - return; })("PASS"); } catch (e) { console.log(e); diff --git a/test/compress/const.js b/test/compress/const.js index a8837500..d7d38cc8 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -1287,6 +1287,7 @@ issue_4261_2: { inline: true, reduce_funcs: true, reduce_vars: true, + side_effects: true, toplevel: true, unused: true, } diff --git a/test/compress/default-values.js b/test/compress/default-values.js index 7fdd83cc..5e24120b 100644 --- a/test/compress/default-values.js +++ b/test/compress/default-values.js @@ -1930,6 +1930,7 @@ issue_5057_4: { var b = "FAIL 2"; (function(a = console.log("FAIL 1")) {})(b); console.log(a); + 0; })("PASS"); } expect_stdout: "PASS" diff --git a/test/compress/functions.js b/test/compress/functions.js index 8c2138bb..62b22a52 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -607,6 +607,7 @@ empty_body: { inline_binary_and: { options = { inline: true, + side_effects: true, } input: { console.log(function() { @@ -626,7 +627,6 @@ inline_binary_and: { return "bar"; }()) { while (console.log("baz")); - return void "moo"; return; } else return; @@ -1121,6 +1121,7 @@ inline_return_binary: { inline_return_conditional: { options = { inline: true, + side_effects: true, } input: { console.log(function() { @@ -2295,6 +2296,7 @@ duplicate_argnames_4: { options = { if_return: true, inline: true, + side_effects: true, } input: { (function() { @@ -5545,6 +5547,7 @@ issue_3833_2: { inline: true, keep_fargs: false, reduce_vars: true, + side_effects: true, toplevel: true, unused: true, } @@ -5582,7 +5585,7 @@ issue_3835: { return f(); })(); } - expect_stdout: true + expect_stdout: RangeError("Maximum call stack size exceeded") } issue_3836_1: { @@ -5610,6 +5613,7 @@ issue_3836_2: { options = { if_return: true, inline: true, + side_effects: true, } input: { (function() { @@ -5867,6 +5871,7 @@ statement_var_inline: { options = { inline: true, join_vars: true, + side_effects: true, unused: true, } input: { @@ -6157,6 +6162,7 @@ issue_4261: { inline: true, reduce_funcs: true, reduce_vars: true, + side_effects: true, toplevel: true, unused: true, } @@ -6449,6 +6455,7 @@ issue_4659_1: { if_return: true, inline: true, reduce_vars: true, + side_effects: true, } input: { var a = 0; @@ -6486,6 +6493,7 @@ issue_4659_2: { if_return: true, inline: true, reduce_vars: true, + side_effects: true, } input: { var a = 0; @@ -6510,7 +6518,7 @@ issue_4659_2: { function f() { return a++; } - void (f && a++); + f && a++; (function() { var a = console && a; })(); @@ -6525,6 +6533,7 @@ issue_4659_3: { if_return: true, inline: true, reduce_vars: true, + side_effects: true, unused: true, } input: { @@ -6731,6 +6740,7 @@ issue_4725_2: { options = { if_return: true, inline: true, + side_effects: true, } input: { var o = { @@ -7715,6 +7725,7 @@ issue_5239: { functions: true, inline: true, reduce_vars: true, + side_effects: true, unused: true, } input: { @@ -7730,7 +7741,6 @@ issue_5239: { var f = void 0; var a = 42, f = function() {}; while (console.log(f.p || a++)); - return; })(); } expect_stdout: "42" @@ -7833,9 +7843,9 @@ issue_5249_1: { var a = "FAIL 1"; else if (a) { while (console.log("FAIL 2")); - return; + return void 0; } else - return; + return void 0; throw "FAIL 3"; }()); } @@ -7996,6 +8006,7 @@ issue_5264_1: { (function(arguments) { console.log(arguments); while (console.log("foo")); + 0; })("bar"); return arguments; }("baz")[0]); @@ -8114,6 +8125,7 @@ issue_5290: { issue_5296: { options = { inline: true, + side_effects: true, } input: { var a = "PASS"; @@ -8376,6 +8388,7 @@ issue_5409: { inline: true, merge_vars: true, reduce_vars: true, + side_effects: true, unused: true, } input: { @@ -8393,7 +8406,6 @@ issue_5409: { a = void 0; console.log(a && a); while (!console); - return; })(); } expect_stdout: "undefined" diff --git a/test/compress/nullish.js b/test/compress/nullish.js index b141d10b..2f438d45 100644 --- a/test/compress/nullish.js +++ b/test/compress/nullish.js @@ -308,6 +308,7 @@ issue_4679: { issue_5266: { options = { inline: true, + side_effects: true, } input: { [ diff --git a/test/compress/spreads.js b/test/compress/spreads.js index 82658dfe..21d99473 100644 --- a/test/compress/spreads.js +++ b/test/compress/spreads.js @@ -188,6 +188,7 @@ do_inline_3: { options = { if_return: true, inline: true, + side_effects: true, } input: { (function() { diff --git a/test/compress/yields.js b/test/compress/yields.js index fbbe51a5..c2061d40 100644 --- a/test/compress/yields.js +++ b/test/compress/yields.js @@ -836,8 +836,10 @@ inline_nested_async: { inline_nested_block: { options = { + dead_code: true, if_return: true, inline: true, + side_effects: true, yields: true, } input: { @@ -857,7 +859,6 @@ inline_nested_block: { var a = function*() { for (var a of [ "foo", "bar" ]) yield a; - "FAIL"; }(), b; do { b = a.next(); @@ -1562,3 +1563,39 @@ issue_5506: { expect_stdout: "PASS" node_version: ">=4" } + +issue_5526: { + options = { + inline: true, + side_effects: true, + } + input: { + (async function*() { + try { + return function() { + while (console.log("foo")); + }(); + } finally { + console.log("bar"); + } + })().next(); + console.log("baz"); + } + expect: { + (async function*() { + try { + while (console.log("foo")); + return void 0; + } finally { + console.log("bar"); + } + })().next(); + console.log("baz"); + } + expect_stdout: [ + "foo", + "baz", + "bar", + ] + node_version: ">=10" +}