fix corner case with parentheses (#4409)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 18 Dec 2020 17:01:49 +0000 (17:01 +0000)
committerGitHub <noreply@github.com>
Fri, 18 Dec 2020 17:01:49 +0000 (01:01 +0800)
fixes #4408

lib/output.js
test/compress/arrows.js

index 7bb27d9..3d8d74c 100644 (file)
@@ -1198,11 +1198,9 @@ function OutputStream(options) {
         // need to take some precautions here:
         //    https://github.com/mishoo/UglifyJS/issues/60
         if (noin) node.walk(new TreeWalker(function(node) {
-            if (parens || node instanceof AST_Scope) return true;
-            if (node instanceof AST_Binary && node.operator == "in") {
-                parens = true;
-                return true;
-            }
+            if (parens) return true;
+            if (node instanceof AST_Binary && node.operator == "in") return parens = true;
+            if (node instanceof AST_Scope && !(node instanceof AST_Arrow && node.value)) return true;
         }));
         node.print(output, parens);
     }
index 0088392..7245c13 100644 (file)
@@ -43,6 +43,82 @@ await_parenthesis: {
     expect_exact: "async function f(){await(a=>a)}"
 }
 
+for_parenthesis_init: {
+    input: {
+        for (a => (a in a); console.log(42););
+    }
+    expect_exact: "for((a=>a in a);console.log(42););"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
+for_parenthesis_condition: {
+    input: {
+        for (console.log(42); a => (a in a);)
+            break;
+    }
+    expect_exact: "for(console.log(42);a=>a in a;)break;"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
+for_parenthesis_step: {
+    input: {
+        for (; console.log(42); a => (a in a));
+    }
+    expect_exact: "for(;console.log(42);a=>a in a);"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
+for_assign_parenthesis_init: {
+    input: {
+        for (f = a => (a in a); console.log(42););
+    }
+    expect_exact: "for((f=a=>a in a);console.log(42););"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
+for_assign_parenthesis_condition: {
+    input: {
+        for (console.log(42); f = a => (a in a);)
+            break;
+    }
+    expect_exact: "for(console.log(42);f=a=>a in a;)break;"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
+for_assign_parenthesis_step: {
+    input: {
+        for (; console.log(42); f = a => (a in a));
+    }
+    expect_exact: "for(;console.log(42);f=a=>a in a);"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
+for_declaration_parenthesis_init: {
+    input: {
+        for (var f = a => (a in a); console.log(42););
+    }
+    expect_exact: "for(var f=(a=>a in a);console.log(42););"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
+for_statement_parenthesis_init: {
+    input: {
+        for (a => {
+            a in a;
+        }; console.log(42););
+    }
+    expect_exact: "for(a=>{a in a};console.log(42););"
+    expect_stdout: "42"
+    node_version: ">=4"
+}
+
 body_call: {
     input: {
         (() => {