aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-05-10 23:33:52 +0100
committerGitHub <noreply@github.com>2020-05-11 06:33:52 +0800
commitc1dd49e07550c3c4599dab6570ff8291f5722b59 (patch)
tree5cfa182fec128c38a5208b47b18aec7106c10c1a
parentc76ee4b868f2b2cdf87e7ab4a000997c475bdbf3 (diff)
downloadtracifyjs-c1dd49e07550c3c4599dab6570ff8291f5722b59.tar.gz
tracifyjs-c1dd49e07550c3c4599dab6570ff8291f5722b59.zip
fix corner case in `comparisons` (#3877)
-rw-r--r--lib/compress.js9
-rw-r--r--test/compress/comparisons.js30
2 files changed, 38 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 4f942429..1b5d1a3a 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -6819,7 +6819,7 @@ merge(Compressor.prototype, {
if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
(self.left.is_number(compressor) && self.right.is_number(compressor)) ||
(self.left.is_boolean(compressor) && self.right.is_boolean(compressor)) ||
- self.left.equivalent_to(self.right)) {
+ can_self_compare(self.left) && self.left.equivalent_to(self.right)) {
self.operator = self.operator.slice(0, 2);
}
// XXX: intentionally falling down to the next case
@@ -7342,6 +7342,13 @@ merge(Compressor.prototype, {
}
return try_evaluate(compressor, self);
+ function can_self_compare(node) {
+ if (node instanceof AST_Dot) return can_self_compare(node.expression);
+ if (node instanceof AST_Sub) return can_self_compare(node.expression) && can_self_compare(node.property);
+ if (node instanceof AST_Symbol) return true;
+ return !node.has_side_effects(compressor);
+ }
+
function align(ref, op) {
switch (ref) {
case "-":
diff --git a/test/compress/comparisons.js b/test/compress/comparisons.js
index a4e0acc9..f93ccbe1 100644
--- a/test/compress/comparisons.js
+++ b/test/compress/comparisons.js
@@ -93,6 +93,36 @@ self_comparison_2: {
expect_stdout: "false true"
}
+self_comparison_3: {
+ options = {
+ comparisons: true,
+ }
+ input: {
+ var a;
+ function f() {
+ var b = a;
+ a = null;
+ return b;
+ }
+ for (var i = 0; i < 2; i++)
+ console.log(f() === f());
+ }
+ expect: {
+ var a;
+ function f() {
+ var b = a;
+ a = null;
+ return b;
+ }
+ for (var i = 0; i < 2; i++)
+ console.log(f() === f());
+ }
+ expect_stdout: [
+ "false",
+ "true",
+ ]
+}
+
issue_2857_1: {
options = {
comparisons: true,