Repository: prettier/plugin-php
Pull request #496 (2018-07-26 16:33:03)

fix: alignment statement in block node with inline node
:star: 


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';
+}