aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-09-04 03:14:39 +0100
committerGitHub <noreply@github.com>2020-09-04 10:14:39 +0800
commit226aa1f76b557aee6eed50e6a578d88a2c656cf2 (patch)
tree9f4fc338a98f49e3ae7a74a40c6bbe8c7cc0ff3f
parent6e235602fb9d7876150a0abf5c918e28db240f2a (diff)
downloadtracifyjs-226aa1f76b557aee6eed50e6a578d88a2c656cf2.tar.gz
tracifyjs-226aa1f76b557aee6eed50e6a578d88a2c656cf2.zip
enhance `unsafe_math` (#4093)
-rw-r--r--lib/compress.js14
-rw-r--r--test/compress/numbers.js16
2 files changed, 24 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 7355ff53..1ae5e8eb 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -7556,13 +7556,15 @@ merge(Compressor.prototype, {
&& self.left.is_number(compressor)) {
if (self.left.left instanceof AST_Constant) {
var lhs = make_binary(self.left, self.operator, self.left.left, self.right, self.left.left.start, self.right.end);
- self = make_binary(self, self.left.operator, lhs, self.left.right);
+ self = make_binary(self, self.left.operator, try_evaluate(compressor, lhs), self.left.right);
} else if (self.left.right instanceof AST_Constant) {
- var rhs = make_binary(self.left, align(self.left.operator, self.operator), self.left.right, self.right, self.left.right.start, self.right.end);
- if (self.left.operator != "-"
- || !self.right.value
- || rhs.evaluate(compressor)
- || !self.left.left.is_negative_zero()) {
+ var op = align(self.left.operator, self.operator);
+ var rhs = try_evaluate(compressor, make_binary(self.left, op, self.left.right, self.right));
+ if (rhs.is_constant()
+ && !(self.left.operator == "-"
+ && self.right.value != 0
+ && +rhs.value == 0
+ && self.left.left.is_negative_zero())) {
self = make_binary(self, self.left.operator, self.left.left, rhs);
}
}
diff --git a/test/compress/numbers.js b/test/compress/numbers.js
index 9fca46f3..32987a6b 100644
--- a/test/compress/numbers.js
+++ b/test/compress/numbers.js
@@ -637,6 +637,22 @@ evaluate_7_unsafe_math: {
]
}
+evaluate_8_unsafe_math: {
+ options = {
+ evaluate: true,
+ unsafe_math: true,
+ }
+ input: {
+ var a = [ "42" ];
+ console.log(a * (1 / 7));
+ }
+ expect: {
+ var a = [ "42" ];
+ console.log(+a / 7);
+ }
+ expect_stdout: "6"
+}
+
NaN_redefined: {
options = {
evaluate: true,