fix & enhance unsafe_math (#3537)

closes #3535
fixes #3536
This commit is contained in:
Alex Lam S.L
2019-10-28 13:37:08 +08:00
committed by GitHub
parent 06e135e35f
commit 2f3b460212
3 changed files with 43 additions and 6 deletions

View File

@@ -2899,7 +2899,19 @@ merge(Compressor.prototype, {
case ">=" : result = left >= right; break;
default : return this;
}
return isNaN(result) && compressor.find_parent(AST_With) ? this : result;
if (isNaN(result)) return compressor.find_parent(AST_With) ? this : result;
if (compressor.option("unsafe_math")
&& typeof result == "number"
&& (this.operator == "+" || this.operator == "-")) {
var digits = Math.max(0, decimals(left), decimals(right));
if (digits < 21) return +result.toFixed(digits);
}
return result;
function decimals(operand) {
var match = /(\.[0-9]*)?(e.+)?$/.exec(+operand);
return (match[1] || ".").length - 1 - (match[2] || "").slice(1);
}
});
def(AST_Conditional, function(compressor, cached, depth) {
var condition = this.condition._eval(compressor, cached, depth);
@@ -5980,8 +5992,11 @@ merge(Compressor.prototype, {
// a + (b + c) => (a + b) + c
if (self.right instanceof AST_Binary
&& self.right.operator != "%"
&& PRECEDENCE[self.right.operator] == PRECEDENCE[self.operator]
&& self.right.is_number(compressor)
&& PRECEDENCE[self.right.operator] == PRECEDENCE[self.operator]) {
&& (self.operator != "+"
|| self.right.left.is_boolean(compressor)
|| self.right.left.is_number(compressor))) {
self = make_node(AST_Binary, self, {
operator: align(self.operator, self.right.operator),
left: make_node(AST_Binary, self.left, {