diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-10-28 13:37:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-28 13:37:08 +0800 |
commit | 2f3b46021239c88e4eb38a8d26ae105aaf78d2d1 (patch) | |
tree | b725db253b0c0a45a91242fb38991da7db85624f /lib | |
parent | 06e135e35f688cc2527a92fd30c9a30f119805bd (diff) | |
download | tracifyjs-2f3b46021239c88e4eb38a8d26ae105aaf78d2d1.tar.gz tracifyjs-2f3b46021239c88e4eb38a8d26ae105aaf78d2d1.zip |
fix & enhance `unsafe_math` (#3537)
closes #3535
fixes #3536
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js index 8e116c6c..442df59d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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, { |