diff --git a/lib/compress.js b/lib/compress.js index b0a85dddad..91c057f5d6 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4389,9 +4389,6 @@ Compressor.prototype.compress = function(node) { "setUTCMonth", "setUTCSeconds", "setYear", - "toExponential", - "toFixed", - "toPrecision", ]); def(AST_Call, function(compressor) { if (!compressor.option("unsafe")) return false; @@ -4442,7 +4439,10 @@ Compressor.prototype.compress = function(node) { "charAt", "substr", "substring", + "toExponential", + "toFixed", "toLowerCase", + "toPrecision", "toString", "toUpperCase", "trim", @@ -10771,6 +10771,9 @@ Compressor.prototype.compress = function(node) { if (seq !== self) return seq.optimize(compressor); } switch (op) { + case "+": + if (compressor.option("evaluate") && exp.is_number(compressor)) return exp; + break; case "-": if (exp instanceof AST_Infinity) exp = exp.transform(compressor); // avoids infinite recursion of numerals @@ -11477,19 +11480,17 @@ Compressor.prototype.compress = function(node) { if (self.left.value == 0) { if (self.right.is_boolean(compressor)) return make_node(AST_UnaryPrefix, self, { operator: "+", - expression: self.right + expression: self.right, }).optimize(compressor); if (self.right.is_number(compressor) && !self.right.is_negative_zero()) return self.right; } break; // 1 * n ---> n case "*": - if (self.left.value == 1) { - return self.right.is_number(compressor) ? self.right : make_node(AST_UnaryPrefix, self, { - operator: "+", - expression: self.right - }).optimize(compressor); - } + if (self.left.value == 1) return make_node(AST_UnaryPrefix, self, { + operator: "+", + expression: self.right, + }).optimize(compressor); break; } if (self.right instanceof AST_Number && !self.left.is_constant()) switch (self.operator) { @@ -11498,28 +11499,24 @@ Compressor.prototype.compress = function(node) { if (self.right.value == 0) { if (self.left.is_boolean(compressor)) return make_node(AST_UnaryPrefix, self, { operator: "+", - expression: self.left + expression: self.left, }).optimize(compressor); if (self.left.is_number(compressor) && !self.left.is_negative_zero()) return self.left; } break; // n - 0 ---> n case "-": - if (self.right.value == 0) { - return self.left.is_number(compressor) ? self.left : make_node(AST_UnaryPrefix, self, { - operator: "+", - expression: self.left - }).optimize(compressor); - } + if (self.right.value == 0) return make_node(AST_UnaryPrefix, self, { + operator: "+", + expression: self.left, + }).optimize(compressor); break; // n / 1 ---> n case "/": - if (self.right.value == 1) { - return self.left.is_number(compressor) ? self.left : make_node(AST_UnaryPrefix, self, { - operator: "+", - expression: self.left - }).optimize(compressor); - } + if (self.right.value == 1) return make_node(AST_UnaryPrefix, self, { + operator: "+", + expression: self.left, + }).optimize(compressor); break; } } diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index acd218ed2b..858869e377 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -745,7 +745,7 @@ call_args: { expect: { var a = 1; console.log(1); - +(1, 1); + 1, 1; } expect_stdout: true } @@ -769,7 +769,7 @@ call_args_drop_param: { } expect: { console.log(1); - +(b, 1); + b, 1; } expect_stdout: true } @@ -3241,3 +3241,35 @@ issue_4886_2: { } expect_stdout: "true" } + +issue_5354: { + options = { + evaluate: true, + unsafe: true, + } + input: { + function f(a) { + return +a.toExponential(1); + } + function g(b) { + return 0 + b.toFixed(2); + } + function h(c) { + return 1 * c.toPrecision(3); + } + console.log(typeof f(45), typeof g(67), typeof h(89)); + } + expect: { + function f(a) { + return +a.toExponential(1); + } + function g(b) { + return 0 + b.toFixed(2); + } + function h(c) { + return +c.toPrecision(3); + } + console.log(typeof f(45), typeof g(67), typeof h(89)); + } + expect_stdout: "number string number" +} diff --git a/test/compress/numbers.js b/test/compress/numbers.js index 124d86ca2c..4996f31ea2 100644 --- a/test/compress/numbers.js +++ b/test/compress/numbers.js @@ -842,9 +842,9 @@ unary_binary_parentheses: { v.forEach(function(x) { v.forEach(function(y) { console.log( - +x*y, - +x/y, - +x%y, + x*y, + x/y, + x%y, -x*y, -x/y, -x%y @@ -1397,7 +1397,7 @@ issue_3695: { } expect: { var a = []; - console.log(+(a * (a[0] = false))); + console.log(a * (a[0] = false)); } expect_stdout: "NaN" }