Skip to content

Commit

Permalink
fix corner case in evaluate & unsafe (#5355)
Browse files Browse the repository at this point in the history
fixes #5354
  • Loading branch information
alexlamsl committed Feb 15, 2022
1 parent 316245e commit 0b50880
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
43 changes: 20 additions & 23 deletions lib/compress.js
Expand Up @@ -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;
Expand Down Expand Up @@ -4442,7 +4439,10 @@ Compressor.prototype.compress = function(node) {
"charAt",
"substr",
"substring",
"toExponential",
"toFixed",
"toLowerCase",
"toPrecision",
"toString",
"toUpperCase",
"trim",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down
36 changes: 34 additions & 2 deletions test/compress/evaluate.js
Expand Up @@ -745,7 +745,7 @@ call_args: {
expect: {
var a = 1;
console.log(1);
+(1, 1);
1, 1;
}
expect_stdout: true
}
Expand All @@ -769,7 +769,7 @@ call_args_drop_param: {
}
expect: {
console.log(1);
+(b, 1);
b, 1;
}
expect_stdout: true
}
Expand Down Expand Up @@ -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"
}
8 changes: 4 additions & 4 deletions test/compress/numbers.js
Expand Up @@ -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
Expand Down Expand Up @@ -1397,7 +1397,7 @@ issue_3695: {
}
expect: {
var a = [];
console.log(+(a * (a[0] = false)));
console.log(a * (a[0] = false));
}
expect_stdout: "NaN"
}
Expand Down

0 comments on commit 0b50880

Please sign in to comment.