aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-11-25 21:14:13 +0800
committerGitHub <noreply@github.com>2019-11-25 21:14:13 +0800
commit48a0f6fe411c09ed3250974332266ed2495832a9 (patch)
tree6907c7b2f13f020bac0f424f5ddf621c2ee7ce77 /lib/compress.js
parent81caadb70900654d309f4750d8d0fbfb8f97a99a (diff)
downloadtracifyjs-48a0f6fe411c09ed3250974332266ed2495832a9.tar.gz
tracifyjs-48a0f6fe411c09ed3250974332266ed2495832a9.zip
enhance `unsafe_math` (#3603)
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js31
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 43c32a75..c195c423 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -5811,14 +5811,15 @@ merge(Compressor.prototype, {
self.right = tmp;
}
}
- if (commutativeOperators[self.operator] && self.right.is_constant() && !self.left.is_constant()) {
+ if (commutativeOperators[self.operator]
+ && self.right.is_constant()
+ && !self.left.is_constant()
+ && !(self.left instanceof AST_Binary
+ && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
// if right is a constant, whatever side effects the
// left side might have could not influence the
// result. hence, force switch.
- if (!(self.left instanceof AST_Binary
- && PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
- reverse();
- }
+ reverse();
}
self = self.lift_sequences(compressor);
if (compressor.option("assignments") && lazy_op[self.operator]) {
@@ -6129,6 +6130,26 @@ merge(Compressor.prototype, {
});
break;
}
+ // (a + b) + 3 => 3 + (a + b)
+ if (compressor.option("unsafe_math")
+ && self.left instanceof AST_Binary
+ && PRECEDENCE[self.left.operator] == PRECEDENCE[self.operator]
+ && self.right.is_constant()
+ && (self.right.is_boolean(compressor) || self.right.is_number(compressor))
+ && self.left.is_number(compressor)
+ && !self.left.right.is_constant()
+ && (self.left.left.is_boolean(compressor) || self.left.left.is_number(compressor))) {
+ self = make_node(AST_Binary, self, {
+ operator: self.left.operator,
+ left: make_node(AST_Binary, self, {
+ operator: self.operator,
+ left: self.right,
+ right: self.left.left
+ }),
+ right: self.left.right
+ });
+ break;
+ }
case "-":
// a - -b => a + b
if (self.right instanceof AST_UnaryPrefix