From 49ce573971fdd533b0a82f8c775ff2f41e63b77a Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 25 Dec 2017 17:25:38 +0800 Subject: [PATCH] handle non-ES5 node types in `inline` (#2648) fixes #2647 --- lib/compress.js | 6 ++- test/compress/functions.js | 92 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 4c4f91f1..05c7fb65 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4277,9 +4277,11 @@ merge(Compressor.prototype, { } else if (scope instanceof AST_Catch) { catches[scope.argname.name] = true; } - } while (!(scope instanceof AST_Scope)); + } while (!(scope instanceof AST_Scope) || scope instanceof AST_Arrow); var safe_to_inject = compressor.toplevel.vars || !(scope instanceof AST_Toplevel); return all(fn.argnames, function(arg) { + if (arg instanceof AST_DefaultAssign) return arg.left.__unused; + if (arg instanceof AST_Destructuring) return false; if (arg instanceof AST_Expansion) return arg.expression.__unused; return arg.__unused || safe_to_inject @@ -4295,7 +4297,7 @@ merge(Compressor.prototype, { for (var len = fn.argnames.length, i = len; --i >= 0;) { var name = fn.argnames[i]; var value = self.args[i]; - if (name.__unused || name instanceof AST_Expansion) { + if (name.__unused || !name.name) { if (value || expressions.length) { expressions.unshift(value || make_node(AST_Undefined, self)); } diff --git a/test/compress/functions.js b/test/compress/functions.js index 381b53a8..0b564aa5 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1456,3 +1456,95 @@ issue_2630_5: { } expect_stdout: "155" } + +issue_2647_1: { + options = { + inline: true, + reduce_vars: true, + side_effects: true, + unused: true, + } + input: { + (function(n, o = "FAIL") { + console.log(n); + })("PASS"); + (function(n, o = "PASS") { + console.log(o); + })("FAIL"); + (function(o = "PASS") { + console.log(o); + })(); + (function(n, {o = "FAIL"}) { + console.log(n); + })("PASS", {}); + } + expect: { + console.log("PASS"); + (function(n, o = "PASS") { + console.log(o); + })(); + (function(o = "PASS") { + console.log(o); + })(); + (function(n, {o = "FAIL"}) { + console.log("PASS"); + })(0, {}); + } + expect_stdout: [ + "PASS", + "PASS", + "PASS", + "PASS", + ] + node_version: ">=6" +} + +issue_2647_2: { + options = { + collapse_vars: true, + inline: true, + reduce_vars: true, + unused: true, + } + input: { + (function() { + function foo(x) { + return x.toUpperCase(); + } + console.log((() => foo("pass"))()); + }()); + } + expect: { + (function() { + console.log("pass".toUpperCase()); + })(); + } + expect_stdout: "PASS" + node_version: ">=4" +} + +issue_2647_3: { + options = { + collapse_vars: true, + inline: true, + reduce_vars: true, + unused: true, + } + input: { + (function() { + function foo(x) { + return x.toUpperCase(); + } + console.log((() => { + return foo("pass"); + })()); + }()); + } + expect: { + (function() { + console.log("pass".toUpperCase()); + })(); + } + expect_stdout: "PASS" + node_version: ">=4" +}