enhance unsafe_math (#3603)
This commit is contained in:
@@ -5811,14 +5811,15 @@ merge(Compressor.prototype, {
|
|||||||
self.right = tmp;
|
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
|
// if right is a constant, whatever side effects the
|
||||||
// left side might have could not influence the
|
// left side might have could not influence the
|
||||||
// result. hence, force switch.
|
// result. hence, force switch.
|
||||||
if (!(self.left instanceof AST_Binary
|
reverse();
|
||||||
&& PRECEDENCE[self.left.operator] >= PRECEDENCE[self.operator])) {
|
|
||||||
reverse();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
self = self.lift_sequences(compressor);
|
self = self.lift_sequences(compressor);
|
||||||
if (compressor.option("assignments") && lazy_op[self.operator]) {
|
if (compressor.option("assignments") && lazy_op[self.operator]) {
|
||||||
@@ -6129,6 +6130,26 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
break;
|
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 "-":
|
case "-":
|
||||||
// a - -b => a + b
|
// a - -b => a + b
|
||||||
if (self.right instanceof AST_UnaryPrefix
|
if (self.right instanceof AST_UnaryPrefix
|
||||||
|
|||||||
@@ -945,3 +945,37 @@ issue_3593: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "-2"
|
expect_stdout: "-2"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe_math_swap_constant: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
unsafe_math: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 1, b = 2;
|
||||||
|
console.log(
|
||||||
|
a++ + b-- + 3,
|
||||||
|
a++ + b + 3,
|
||||||
|
a + b-- + 3,
|
||||||
|
a + b + 3,
|
||||||
|
a++ - b-- + 3,
|
||||||
|
a++ - b + 3,
|
||||||
|
a - b-- + 3,
|
||||||
|
a - b + 3
|
||||||
|
);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 1, b = 2;
|
||||||
|
console.log(
|
||||||
|
3 + a++ + b--,
|
||||||
|
a++ + b + 3,
|
||||||
|
a + b-- + 3,
|
||||||
|
a + b + 3,
|
||||||
|
3 + a++ - b--,
|
||||||
|
3 + a++ - b,
|
||||||
|
a - b-- + 3,
|
||||||
|
a - b + 3
|
||||||
|
);
|
||||||
|
}
|
||||||
|
expect_stdout: "6 6 7 6 6 8 9 10"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user