diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-12-29 02:50:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-29 02:50:57 +0000 |
commit | af310ba2d0cd79fbc02d29db2d959367c276b2b9 (patch) | |
tree | 96d55232c11f9304250fe6ad08a367a46baa72a1 /lib | |
parent | 2f3930d1b92746be6a3b3e1d371b0ae25e56777e (diff) | |
download | tracifyjs-af310ba2d0cd79fbc02d29db2d959367c276b2b9.tar.gz tracifyjs-af310ba2d0cd79fbc02d29db2d959367c276b2b9.zip |
fix corner case in `evaluate` (#3654)
fixes #3653
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 42 |
1 files changed, 26 insertions, 16 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 "&&": |