aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-10-27 08:25:11 +0800
committerGitHub <noreply@github.com>2019-10-27 08:25:11 +0800
commita270ba6b590791d846d4966c8e3a0ba4d6273191 (patch)
treef8dd22ecdac0fcd5756c41814f4c758296ffbbe9 /lib
parent37f35e4ac2b67cbcef7404fbe624574139f5e2a8 (diff)
downloadtracifyjs-a270ba6b590791d846d4966c8e3a0ba4d6273191.tar.gz
tracifyjs-a270ba6b590791d846d4966c8e3a0ba4d6273191.zip
fix corner cases in `unsafe_math` (#3532)
fixes #3531
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js18
1 files changed, 15 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 6d8c1f8f..8e116c6c 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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,