Repository: prettier/plugin-php
Pull request #499 (2018-07-26 19:25:38)

test: unnecessary parens
Just tests. But you can look what we have unnecessary parens around number. Solution:
1. Create issue about implement `parenthesis` through `extra` option (look https://astexplorer.net/#/gist/3b8d354d8e9c4d18758916a3c3fc3f3b/5f02fe391ef5b6727c0632afa857b442d232d936), but it is require a lot of work on parser side.
2. Create function to remove unnecessary parens (we can implement this right now and when parser will support parens through extra options just remove).


Patch:
diff --git a/src/printer.js b/src/printer.js
index aa12c16..fe6e18b 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1229,22 +1229,22 @@ function wrapPartsIntoGroups(parts, indexes) {
   return indexes.reduce((accumulator, index) => {
     const { start, end, alignment, before, after } = index;
 
+    const printedPartsForGrouping = concat([
+      before || "",
+      concat(parts.slice(start, end)),
+      after || ""
+    ]);
     const newArray = accumulator.concat(
       parts.slice(lastEnd, start),
-      dedentToRoot(
-        group(
-          concat([
-            align(
-              new Array(alignment).join(" "),
+      alignment
+        ? dedentToRoot(
+            group(
               concat([
-                before ? before : "",
-                concat(parts.slice(start, end)),
-                after ? after : ""
+                align(new Array(alignment).join(" "), printedPartsForGrouping)
               ])
             )
-          ])
-        )
-      ),
+          )
+        : group(printedPartsForGrouping),
       end === parts.length - 1 ? parts.slice(end) : ""
     );
 
@@ -1315,7 +1315,11 @@ function printLines(path, options, print, childrenAttribute = "children") {
           ? getAlignment(prevInlineNode.raw)
           : "";
         const shouldBreak = end - start > 1;
-        const before = shouldBreak ? hardline : "";
+        const before = shouldBreak
+          ? isBlockNestedNode && !prevInlineNode
+            ? ""
+            : hardline
+          : "";
         const after = shouldBreak
           ? isBlockNestedNode && isLastNode
             ? ""


Test patch:
diff --git a/tests/inline/__snapshots__/jsfmt.spec.js.snap b/tests/inline/__snapshots__/jsfmt.spec.js.snap
index 25397bf..406e5bc 100644
--- a/tests/inline/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/inline/__snapshots__/jsfmt.spec.js.snap
@@ -120,6 +120,83 @@ function fff()
     </div>
     <?php
 }
+
+function ffff()
+{
+  echo '1';
+  echo '2';
+  echo '3';
+  echo '4';
+  ?>
+  <div>foo</div>
+  <?php
+  echo '5';
+  echo '6';
+  echo '7';
+  echo '8';
+}
+
+function fffff()
+{
+    echo '1';
+    echo '2';
+    echo '3';
+    echo '4';
+    ?>
+    <div>
+      <div>
+        <div>
+          <?php
+          echo 'nested';
+          echo 'nested';
+          echo 'nested';
+          ?>
+        </div>
+      </div>
+    </div>
+    <?php
+    echo '5';
+    echo '6';
+    echo '7';
+    echo '8';
+}
+
+function ffffff()
+{
+    echo '1';
+    echo '2';
+    echo '3';
+    echo '4';
+    ?>
+      <div>
+        <div>
+          <div>
+            <div>
+                <?php
+                function ffffff() {
+                    echo '1';
+                    echo '2';
+                    echo '3';
+                    echo '4';
+                    ?>
+                    <div>FooBar</div>
+                    <?php
+                    echo '5';
+                    echo '6';
+                    echo '7';
+                    echo '8';
+                }
+                ?>
+            </div>
+          </div>
+        </div>
+      </div>
+    <?php
+    echo '5';
+    echo '6';
+    echo '7';
+    echo '8';
+}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -222,6 +299,82 @@ function fff()
     <?php
 }
 
+function ffff()
+{
+    echo '1';
+    echo '2';
+    echo '3';
+    echo '4';
+    ?>
+  <div>foo</div>
+  <?php
+  echo '5';
+  echo '6';
+  echo '7';
+  echo '8';
+}
+
+function fffff()
+{
+    echo '1';
+    echo '2';
+    echo '3';
+    echo '4';
+    ?>
+    <div>
+      <div>
+        <div>
+          <?php
+          echo 'nested';
+          echo 'nested';
+          echo 'nested';
+          ?>
+        </div>
+      </div>
+    </div>
+    <?php
+    echo '5';
+    echo '6';
+    echo '7';
+    echo '8';
+}
+
+function ffffff()
+{
+    echo '1';
+    echo '2';
+    echo '3';
+    echo '4';
+    ?>
+      <div>
+        <div>
+          <div>
+            <div>
+                <?php function ffffff()
+                {
+                    echo '1';
+                    echo '2';
+                    echo '3';
+                    echo '4';
+                    ?>
+                    <div>FooBar</div>
+                    <?php
+                    echo '5';
+                    echo '6';
+                    echo '7';
+                    echo '8';
+                } ?>
+            </div>
+          </div>
+        </div>
+      </div>
+    <?php
+    echo '5';
+    echo '6';
+    echo '7';
+    echo '8';
+}
+
 `;
 
 exports[`mixed.php 1`] = `
diff --git a/tests/inline/inline.php b/tests/inline/inline.php
index 44361e2..4b4e2ae 100644
--- a/tests/inline/inline.php
+++ b/tests/inline/inline.php
@@ -98,3 +98,80 @@ function fff()
     </div>
     <?php
 }
+
+function ffff()
+{
+  echo '1';
+  echo '2';
+  echo '3';
+  echo '4';
+  ?>
+  <div>foo</div>
+  <?php
+  echo '5';
+  echo '6';
+  echo '7';
+  echo '8';
+}
+
+function fffff()
+{
+    echo '1';
+    echo '2';
+    echo '3';
+    echo '4';
+    ?>
+    <div>
+      <div>
+        <div>
+          <?php
+          echo 'nested';
+          echo 'nested';
+          echo 'nested';
+          ?>
+        </div>
+      </div>
+    </div>
+    <?php
+    echo '5';
+    echo '6';
+    echo '7';
+    echo '8';
+}
+
+function ffffff()
+{
+    echo '1';
+    echo '2';
+    echo '3';
+    echo '4';
+    ?>
+      <div>
+        <div>
+          <div>
+            <div>
+                <?php
+                function ffffff() {
+                    echo '1';
+                    echo '2';
+                    echo '3';
+                    echo '4';
+                    ?>
+                    <div>FooBar</div>
+                    <?php
+                    echo '5';
+                    echo '6';
+                    echo '7';
+                    echo '8';
+                }
+                ?>
+            </div>
+          </div>
+        </div>
+      </div>
+    <?php
+    echo '5';
+    echo '6';
+    echo '7';
+    echo '8';
+}
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 06efe2a..e6d5dcf 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -52,3 +52,18 @@ exports[`new.php 1`] = `
 ?>
 
 `;
+
+exports[`unnecessary.php 1`] = `
+<?php
+$test = 1;
+$test = (1);
+$test = ((1));
+$test = (((1)));
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$test = 1;
+$test = (1);
+$test = (1);
+$test = (1);
+
+`;
diff --git a/tests/parens/unnecessary.php b/tests/parens/unnecessary.php
new file mode 100644
index 0000000..2e88693
--- /dev/null
+++ b/tests/parens/unnecessary.php
@@ -0,0 +1,5 @@
+<?php
+$test = 1;
+$test = (1);
+$test = ((1));
+$test = (((1)));