Repository: prettier/plugin-php
Pull request #520 (2018-08-09 15:17:43)

fix: better print key/value in foreach
key/value in own group


Patch:
diff --git a/src/printer.js b/src/printer.js
index 20b5be5..1ae6b54 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2042,14 +2042,16 @@ function printStatement(path, options, print) {
                 path.call(print, "source"),
                 line,
                 "as ",
-                node.key
-                  ? indent(
-                      join(concat([" =>", line]), [
-                        path.call(print, "key"),
-                        path.call(print, "value")
-                      ])
-                    )
-                  : path.call(print, "value")
+                group(
+                  node.key
+                    ? indent(
+                        join(concat([" =>", line]), [
+                          path.call(print, "key"),
+                          path.call(print, "value")
+                        ])
+                      )
+                    : path.call(print, "value")
+                )
               ])
             ),
             softline
@@ -2412,9 +2414,12 @@ function printNode(path, options, print) {
       return "continue";
     case "return": {
       const parts = [];
+
       parts.push("return");
+
       if (node.expr) {
         const printedExpr = path.call(print, "expr");
+
         if (node.expr.kind === "bin") {
           parts.push(
             group(
@@ -2430,6 +2435,14 @@ function printNode(path, options, print) {
           parts.push(" ", printedExpr);
         }
       }
+
+      if (hasDanglingComments(node)) {
+        parts.push(
+          " ",
+          comments.printDanglingComments(path, options, /* sameIndent */ true)
+        );
+      }
+
       return concat(parts);
     }
     case "entry": {


Test patch:
diff --git a/tests/operations/__snapshots__/jsfmt.spec.js.snap b/tests/bin/__snapshots__/jsfmt.spec.js.snap
similarity index 95%
rename from tests/operations/__snapshots__/jsfmt.spec.js.snap
rename to tests/bin/__snapshots__/jsfmt.spec.js.snap
index d2d88e9..fe25ee7 100644
--- a/tests/operations/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/bin/__snapshots__/jsfmt.spec.js.snap
@@ -1,6 +1,6 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
-exports[`operations.php 1`] = `
+exports[`bin.php 1`] = `
 <?php
 $a++;
 ++$a;
@@ -167,3 +167,12 @@ $value =
         : 'disabled';
 
 `;
+
+exports[`single.php 1`] = `
+<?php
+$a++;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$a++;
+
+`;
diff --git a/tests/operations/operations.php b/tests/bin/bin.php
similarity index 100%
rename from tests/operations/operations.php
rename to tests/bin/bin.php
diff --git a/tests/operations/jsfmt.spec.js b/tests/bin/jsfmt.spec.js
similarity index 100%
rename from tests/operations/jsfmt.spec.js
rename to tests/bin/jsfmt.spec.js
diff --git a/tests/bin/single.php b/tests/bin/single.php
new file mode 100644
index 0000000..a064c8e
--- /dev/null
+++ b/tests/bin/single.php
@@ -0,0 +1,2 @@
+<?php
+$a++;
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 5258f0f..420b4f2 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1546,6 +1546,22 @@ echo /*Comment*/ /*Comment*/ $a->bar();
 
 `;
 
+exports[`return.php 1`] = `
+<?php
+
+function f() {
+    return /* a */;
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+function f()
+{
+    return /* a */;
+}
+
+`;
+
 exports[`silent.php 1`] = `
 <?php
 
diff --git a/tests/comments/return.php b/tests/comments/return.php
new file mode 100644
index 0000000..a95c979
--- /dev/null
+++ b/tests/comments/return.php
@@ -0,0 +1,5 @@
+<?php
+
+function f() {
+    return /* a */;
+}
diff --git a/tests/foreach/__snapshots__/jsfmt.spec.js.snap b/tests/foreach/__snapshots__/jsfmt.spec.js.snap
index 0930657..b21b305 100644
--- a/tests/foreach/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/foreach/__snapshots__/jsfmt.spec.js.snap
@@ -47,6 +47,21 @@ foreach ($points as ["x" => $veryVeryVeryVeryVeryVeryVeryVeryLongValue, "y" => $
 foreach ($points as ["veryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryLongX, "veryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryLongY]) {
     var_dump($x, $y);
 }
+
+foreach (
+    [
+        'one' => [
+            'string',
+            'other-string'
+        ],
+        'two' => [
+            'string',
+            'other-string'
+        ],
+    ]
+    as $key => $aliases
+) {
+}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 foreach ($test as $i) {
@@ -55,8 +70,7 @@ foreach ($test as $i) {
 
 foreach (
     $really_really_really_really_really_long_array
-    as $key =>
-        $really_really_really_long_value
+    as $key => $really_really_really_long_value
 ) {
     $test3 = $really_really_really_long_value;
 }
@@ -135,4 +149,13 @@ foreach (
     var_dump($x, $y);
 }
 
+foreach (
+    [
+        'one' => ['string', 'other-string'],
+        'two' => ['string', 'other-string']
+    ]
+    as $key => $aliases
+) {
+}
+
 `;
diff --git a/tests/foreach/foreach.php b/tests/foreach/foreach.php
index 153a609..cda81f5 100644
--- a/tests/foreach/foreach.php
+++ b/tests/foreach/foreach.php
@@ -44,3 +44,18 @@ foreach ($points as ["x" => $veryVeryVeryVeryVeryVeryVeryVeryLongValue, "y" => $
 foreach ($points as ["veryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryLongX, "veryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryLongY]) {
     var_dump($x, $y);
 }
+
+foreach (
+    [
+        'one' => [
+            'string',
+            'other-string'
+        ],
+        'two' => [
+            'string',
+            'other-string'
+        ],
+    ]
+    as $key => $aliases
+) {
+}