Repository: prettier/plugin-php
Pull request #578 (2018-09-06 15:44:53)

feat: avoid unnecessary parent for `unary` node

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 9ca2485..615c4f1 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -7,6 +7,7 @@ function needsParens(path) {
     return false;
   }
 
+  const name = path.getName();
   const node = path.getNode();
 
   if (["include", "print", "return", "echo"].includes(parent.kind)) {
@@ -14,6 +15,17 @@ function needsParens(path) {
   }
 
   switch (node.kind) {
+    case "unary":
+      switch (parent.kind) {
+        case "unary":
+          return (
+            node.type === parent.type &&
+            (node.type === "+" || node.type === "-")
+          );
+        case "bin":
+          return parent.type === "**" && name === "left";
+      }
+    // else fallthrough
     case "boolean":
     case "string":
     case "number":


Test patch:
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 273626d..5c2858d 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -118,10 +118,10 @@ $var = --$var;
 $var = (--$var);
 
 $var = ~$var;
-$var = (~$var);
+$var = ~$var;
 
 $var = !$var;
-$var = (!$var);
+$var = !$var;
 
 $var = $var += 10;
 $var = ($var += 10);
@@ -390,6 +390,119 @@ include $test ? 'foo' : 'bar';
 
 `;
 
+exports[`unary.php 1`] = `
+<?php
+
++$var;
++($var);
+(+$var);
+
+-$var;
+-($var);
+(-$var);
+
+~$var;
+~($var);
+(~$var);
+
+!$var;
+!($var);
+(!$var);
+
+!!$var;
+!(!$var);
+(!(!$var));
+(!!$var);
+
+$var = (+$var);
+$var = +(+$var);
+
+$var = (-$var);
+$var = -(-$var);
+
+$var = (~$var);
+$var = ~(~$var);
+
+$var = (!$var);
+$var = !(!$var);
+
+$a = +$a ** 1;
+$a = (+$a) ** 1;
+$a = 1 ** (+$a);
+
+$var = call(+$a);
+$var = call((+$a));
+
+$var = +($foo->bar);
+
+$var = +$var || +$var;
+$var = (+$var) || (+$var);
+$var = ((+$var) || (+$var));
+
+$var = -(+($var));
+
+$var = ~(+$var);
+
+$var = ~$var += 1;
+$var = ~($var += 1);
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
++$var;
++$var;
++$var;
+
+-$var;
+-$var;
+-$var;
+
+~$var;
+~$var;
+~$var;
+
+!$var;
+!$var;
+!$var;
+
+!!$var;
+!!$var;
+!!$var;
+!!$var;
+
+$var = +$var;
+$var = +(+$var);
+
+$var = -$var;
+$var = -(-$var);
+
+$var = ~$var;
+$var = ~~$var;
+
+$var = !$var;
+$var = !!$var;
+
+$a = (+$a) ** 1;
+$a = (+$a) ** 1;
+$a = 1 ** +$a;
+
+$var = call(+$a);
+$var = call(+$a);
+
+$var = +($foo->bar);
+
+$var = +$var || +$var;
+$var = +$var || +$var;
+$var = (+$var || +$var);
+
+$var = -+$var;
+
+$var = ~+$var;
+
+$var = ~$var += 1;
+$var = ~($var += 1);
+
+`;
+
 exports[`unnecessary.php 1`] = `
 <?php
 $test = 1;
diff --git a/tests/parens/unary.php b/tests/parens/unary.php
new file mode 100644
index 0000000..1ed04a5
--- /dev/null
+++ b/tests/parens/unary.php
@@ -0,0 +1,54 @@
+<?php
+
++$var;
++($var);
+(+$var);
+
+-$var;
+-($var);
+(-$var);
+
+~$var;
+~($var);
+(~$var);
+
+!$var;
+!($var);
+(!$var);
+
+!!$var;
+!(!$var);
+(!(!$var));
+(!!$var);
+
+$var = (+$var);
+$var = +(+$var);
+
+$var = (-$var);
+$var = -(-$var);
+
+$var = (~$var);
+$var = ~(~$var);
+
+$var = (!$var);
+$var = !(!$var);
+
+$a = +$a ** 1;
+$a = (+$a) ** 1;
+$a = 1 ** (+$a);
+
+$var = call(+$a);
+$var = call((+$a));
+
+$var = +($foo->bar);
+
+$var = +$var || +$var;
+$var = (+$var) || (+$var);
+$var = ((+$var) || (+$var));
+
+$var = -(+($var));
+
+$var = ~(+$var);
+
+$var = ~$var += 1;
+$var = ~($var += 1);