aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-12-29 02:50:57 +0000
committerGitHub <noreply@github.com>2019-12-29 02:50:57 +0000
commitaf310ba2d0cd79fbc02d29db2d959367c276b2b9 (patch)
tree96d55232c11f9304250fe6ad08a367a46baa72a1
parent2f3930d1b92746be6a3b3e1d371b0ae25e56777e (diff)
downloadtracifyjs-af310ba2d0cd79fbc02d29db2d959367c276b2b9.tar.gz
tracifyjs-af310ba2d0cd79fbc02d29db2d959367c276b2b9.zip
fix corner case in `evaluate` (#3654)
fixes #3653
-rw-r--r--lib/compress.js42
-rw-r--r--test/compress/numbers.js45
2 files changed, 68 insertions, 19 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 70dafe99..9045a8dd 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -6466,13 +6466,6 @@ merge(Compressor.prototype, {
}).optimize(compressor);
}
break;
- // 0 - n => -n
- case "-":
- if (self.left.value == 0) return make_node(AST_UnaryPrefix, self, {
- operator: "-",
- expression: self.right
- }).optimize(compressor);
- break;
// 1 * n => n
case "*":
if (self.left.value == 1) {
@@ -6483,15 +6476,32 @@ merge(Compressor.prototype, {
}
break;
}
- // n - 0 => n
- // n / 1 => n
- if (self.right instanceof AST_Number && !self.left.is_constant() && self.right.value == {
- "-": 0,
- "/": 1,
- }[self.operator]) return self.left.is_number(compressor) ? self.left : make_node(AST_UnaryPrefix, self, {
- operator: "+",
- expression: self.left
- }).optimize(compressor);
+ if (self.right instanceof AST_Number && !self.left.is_constant()) switch (self.operator) {
+ // n + 0 => n
+ case "+":
+ if (self.right.value == 0 && (self.left.is_boolean(compressor) || self.left.is_number(compressor))) {
+ return self.left;
+ }
+ break;
+ // n - 0 => n
+ case "-":
+ if (self.right.value == 0) {
+ return self.left.is_number(compressor) ? self.left : make_node(AST_UnaryPrefix, self, {
+ operator: "+",
+ expression: self.left
+ }).optimize(compressor);
+ }
+ break;
+ // n / 1 => n
+ case "/":
+ if (self.right.value == 1) {
+ return self.left.is_number(compressor) ? self.left : make_node(AST_UnaryPrefix, self, {
+ operator: "+",
+ expression: self.left
+ }).optimize(compressor);
+ }
+ break;
+ }
}
if (compressor.option("typeofs")) switch (self.operator) {
case "&&":
diff --git a/test/compress/numbers.js b/test/compress/numbers.js
index c432a435..07e93f60 100644
--- a/test/compress/numbers.js
+++ b/test/compress/numbers.js
@@ -997,7 +997,7 @@ identity_1: {
expect: {
0 + a;
a + 0;
- -a;
+ 0 - a;
+a;
+a;
+a;
@@ -1023,7 +1023,7 @@ identity_2: {
expect: {
+!a;
+!a;
- -!a;
+ 0 - !a;
+!a;
+!a;
+!a;
@@ -1049,7 +1049,7 @@ identity_3: {
expect: {
--a;
--a;
- - --a;
+ 0 - --a;
--a;
--a;
--a;
@@ -1057,3 +1057,42 @@ identity_3: {
--a;
}
}
+
+issue_3653: {
+ options = {
+ evaluate: true,
+ }
+ input: {
+ console.log(0 - (console && 0));
+ console.log(0 + (0 - (console && 0)));
+ console.log(0 - (0 - (console && 0)));
+ console.log(1 * (0 - (console && 0)));
+ console.log(1 / (0 - (console && 0)));
+ console.log((0 - (console && 0)) + 0);
+ console.log((0 - (console && 0)) - 0);
+ console.log((0 - (console && 0)) * 1);
+ console.log((0 - (console && 0)) / 1);
+ }
+ expect: {
+ console.log(0 - (console && 0));
+ console.log(0 - (console && 0));
+ console.log(0 - (0 - (console && 0)));
+ console.log(0 - (console && 0));
+ console.log(1 / (0 - (console && 0)));
+ console.log(0 - (console && 0));
+ console.log(0 - (console && 0));
+ console.log(0 - (console && 0));
+ console.log(0 - (console && 0));
+ }
+ expect_stdout: [
+ "0",
+ "0",
+ "0",
+ "0",
+ "Infinity",
+ "0",
+ "0",
+ "0",
+ "0",
+ ]
+}