fix corner cases in unsafe_math (#3532)

fixes #3531
This commit is contained in:
Alex Lam S.L
2019-10-27 08:25:11 +08:00
committed by GitHub
parent 37f35e4ac2
commit a270ba6b59
2 changed files with 259 additions and 73 deletions

View File

@@ -5930,7 +5930,8 @@ merge(Compressor.prototype, {
// a - -b => a + b
if (self.right instanceof AST_UnaryPrefix
&& self.right.operator == "-"
&& self.left.is_number(compressor)) {
&& self.left.is_number(compressor)
&& self.right.expression.is_number(compressor)) {
self = make_node(AST_Binary, self, {
operator: "+",
left: self.left,
@@ -5979,6 +5980,7 @@ merge(Compressor.prototype, {
// a + (b + c) => (a + b) + c
if (self.right instanceof AST_Binary
&& self.right.operator != "%"
&& self.right.is_number(compressor)
&& PRECEDENCE[self.right.operator] == PRECEDENCE[self.operator]) {
self = make_node(AST_Binary, self, {
operator: align(self.operator, self.right.operator),
@@ -5991,6 +5993,14 @@ merge(Compressor.prototype, {
}),
right: self.right.right
});
if (self.operator == "+"
&& !self.right.is_boolean(compressor)
&& !self.right.is_number(compressor)) {
self.right = make_node(AST_UnaryPrefix, self.right, {
operator: "+",
expression: self.right
});
}
}
// (2 * n) * 3 => 6 * n
// (n + 2) + 3 => n + 5
@@ -5998,7 +6008,8 @@ merge(Compressor.prototype, {
&& self.left instanceof AST_Binary
&& self.left.operator != "%"
&& PRECEDENCE[self.left.operator] == PRECEDENCE[self.operator]) {
if (self.left.left instanceof AST_Constant) {
if (self.left.left instanceof AST_Constant
&& (self.left.operator != "+" || self.left.right.is_number(compressor))) {
self = make_node(AST_Binary, self, {
operator: self.left.operator,
left: make_node(AST_Binary, self.left, {
@@ -6010,7 +6021,8 @@ merge(Compressor.prototype, {
}),
right: self.left.right
});
} else if (self.left.right instanceof AST_Constant) {
} else if (self.left.right instanceof AST_Constant
&& (self.left.operator != "+" || self.left.left.is_number(compressor))) {
self = make_node(AST_Binary, self, {
operator: self.left.operator,
left: self.left.left,