aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-03-24 02:10:02 +0000
committerGitHub <noreply@github.com>2021-03-24 10:10:02 +0800
commit40ef074cb30e0b0114466406f6b9d8592faad14e (patch)
tree6fd584494815cca737136604aec6163f6e5b2b6d
parent78e3936cd489b6a764027f35c27e8438c1bee132 (diff)
downloadtracifyjs-40ef074cb30e0b0114466406f6b9d8592faad14e.tar.gz
tracifyjs-40ef074cb30e0b0114466406f6b9d8592faad14e.zip
fix corner case in `comparisons` (#4820)
fixes #4819
-rw-r--r--lib/compress.js21
-rw-r--r--test/compress/assignments.js14
2 files changed, 27 insertions, 8 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 6db95187..cc67bb2b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3689,20 +3689,25 @@ merge(Compressor.prototype, {
(function(def) {
def(AST_Node, return_false);
def(AST_Array, return_true);
- def(AST_Assign, function(compressor) {
- return this.operator != "=" || this.right.is_defined(compressor);
- });
- def(AST_Binary, function(compressor) {
- switch (this.operator) {
+ function is_binary_defined(compressor, op, node) {
+ switch (op) {
case "&&":
- return this.left.is_defined(compressor) && this.right.is_defined(compressor);
+ return node.left.is_defined(compressor) && node.right.is_defined(compressor);
case "||":
- return this.left.is_truthy() || this.right.is_defined(compressor);
+ return node.left.is_truthy() || node.right.is_defined(compressor);
case "??":
- return this.left.is_defined(compressor) || this.right.is_defined(compressor);
+ return node.left.is_defined(compressor) || node.right.is_defined(compressor);
default:
return true;
}
+ }
+ def(AST_Assign, function(compressor) {
+ var op = this.operator;
+ if (op == "=") return this.right.is_defined(compressor);
+ return is_binary_defined(compressor, op.slice(0, -1), this);
+ });
+ def(AST_Binary, function(compressor) {
+ return is_binary_defined(compressor, this.operator, this);
});
def(AST_Conditional, function(compressor) {
return this.consequent.is_defined(compressor) && this.alternative.is_defined(compressor);
diff --git a/test/compress/assignments.js b/test/compress/assignments.js
index db562292..80538bd8 100644
--- a/test/compress/assignments.js
+++ b/test/compress/assignments.js
@@ -589,3 +589,17 @@ issue_4815_2: {
expect_stdout: "PASS"
node_version: ">=15"
}
+
+issue_4819: {
+ options = {
+ comparisons: true,
+ }
+ input: {
+ console.log(void 0 === ([].p &&= 42));
+ }
+ expect: {
+ console.log(void 0 === ([].p &&= 42));
+ }
+ expect_stdout: "true"
+ node_version: ">=15"
+}