From 7ebfb22d16a314404e1255aee9a470e1dd7b0af4 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 5 Jan 2021 16:15:12 +0000 Subject: [PATCH] fix corner cases in `inline` & `unused` (#4509) fixes #4508 --- lib/compress.js | 23 ++++++++++------------- test/compress/destructured.js | 28 ++++++++++++++++++++++++++++ test/compress/drop-unused.js | 15 ++++++++------- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 459d54f0..1714306e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5674,12 +5674,11 @@ merge(Compressor.prototype, { var write_only = def.value.write_only; var value = def.value.drop_side_effect_free(compressor); if (def.value !== value) { - def.value = value && make_sequence(def.value, [ - value, - make_node(AST_Number, def.value, { - value: 0 - }), - ]); + def.value = null; + if (value) { + AST_Node.warn("Side effects in last use of variable {name} [{file}:{line},{col}]", template(def.name)); + side_effects.push(value); + } } else if (def.value.write_only !== write_only) { def.value.write_only = write_only; } @@ -8139,7 +8138,7 @@ merge(Compressor.prototype, { return node; } } - var insert, in_loop, scope; + var arg_used, insert, in_loop, scope; if (replacing && can_inject_symbols()) { fn._squeezed = true; if (exp !== fn) fn.parent_scope = exp.scope; @@ -8453,8 +8452,9 @@ merge(Compressor.prototype, { } } var inline = compressor.option("inline"); - var used = Object.create(defined); - if (!can_inject_args(defined, used, inline >= 2 && safe_to_inject)) return false; + arg_used = Object.create(defined); + if (!can_inject_args(defined, arg_used, inline >= 2 && safe_to_inject)) return false; + var used = Object.create(arg_used); if (!can_inject_vars(defined, used, inline >= 3 && safe_to_inject)) return false; return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop); } @@ -8558,10 +8558,7 @@ merge(Compressor.prototype, { name.thedef = redef; } append_var(decls, expressions, name, var_def.value); - if (in_loop && all(fn.argnames, function(argname) { - if (argname instanceof AST_DefaultValue) argname = argname.name; - return argname.name != name.name; - })) { + if (in_loop && !HOP(arg_used, name.name)) { var def = fn.variables.get(name.name); var sym = make_node(AST_SymbolRef, name, name); def.references.push(sym); diff --git a/test/compress/destructured.js b/test/compress/destructured.js index f9d32ce0..a476705d 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -2356,3 +2356,31 @@ issue_4504: { expect_stdout: "PASS" node_version: ">=6" } + +issue_4508: { + options = { + inline: true, + toplevel: true, + unused: true, + } + input: { + for (var i = 0; i < 2; i++) + (function f([ a ]) { + var a = console.log(a) && b, b = null; + })([ "PASS" ]); + } + expect: { + for (var i = 0; i < 2; i++) + [ [ a ] ] = [ [ "PASS" ] ], + b = void 0, + a = console.log(a) && b, + b = null, + void 0; + var a, b; + } + expect_stdout: [ + "PASS", + "PASS", + ] + node_version: ">=6" +} diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index ae1a6e5b..bb47682c 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -2669,8 +2669,7 @@ issue_3956: { })(); } expect: { - var c, d; - c += 0, + var d; console.log(NaN), d = 1 ^ console.log(1), console.log(d); @@ -2703,13 +2702,13 @@ issue_3962_1: { } expect: { var a = 0; - a = (function(c) { + (function(c) { do { console; 0..toString(); } while (0); if (c) console.log("PASS"); - }(1), 0); + })(1); void 0; } expect_stdout: "PASS" @@ -2736,13 +2735,13 @@ issue_3962_2: { } expect: { var a = 0; - a = (function(c) { + (function(c) { do { console; 0..toString(); } while (0); if (c) console.log("PASS"); - }(1), 0); + })(1); } expect_stdout: "PASS" } @@ -2799,7 +2798,9 @@ issue_4017: { var a = 0; console.log(function() { c &= 0; - var c = (a++, A = a, 0); + var c; + a++, + A = a; }()); } expect_stdout: "undefined" -- 2.34.1