Skip to content

Commit

Permalink
fix corner cases in unsafe_math (#3532)
Browse files Browse the repository at this point in the history
fixes #3531
  • Loading branch information
alexlamsl committed Oct 27, 2019
1 parent 37f35e4 commit a270ba6
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 73 deletions.
18 changes: 15 additions & 3 deletions lib/compress.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -5991,14 +5993,23 @@ 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
if (self.right instanceof AST_Constant
&& 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, {
Expand All @@ -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,
Expand Down

0 comments on commit a270ba6

Please sign in to comment.