Repository: prettier/plugin-php
Pull request #2054 (2022-08-27 16:34:22)

Attempt to fix the formatting of long cases in match statement

czosel: Forgot to push my commit :see_no_evil: should be ready for review now.
czosel: @cseufert friendly ping :)
czosel: Yeah :sweat_smile: Thanks for the review!

Patch:
diff --git a/src/printer.js b/src/printer.js
index 4a0e41d..b5d8e46 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -3034,21 +3034,24 @@ function printNode(path, options, print) {
             : concat(
                 armPath.map(
                   (condPath, condIdx) =>
-                    group(
-                      concat(
-                        condIdx > 0
-                          ? [",", line, print(condPath)]
-                          : [print(condPath)]
-                      )
+                    concat(
+                      [",", line, print(condPath)].slice(condIdx === 0 ? 2 : 0)
                     ),
                   "conds"
                 )
               );
         const body = armPath.call(print, "body");
         return concat(
-          armIdx > 0
-            ? [", ", hardline, conds, " => ", body]
-            : [hardline, conds, " => ", body]
+          [
+            ",",
+            hardline,
+            group(
+              concat([
+                group(concat([conds, indent(line)])),
+                concat(["=> ", body]),
+              ])
+            ),
+          ].slice(armIdx > 0 ? 0 : 1)
         );
       }, "arms");
       return group(


Test patch:
diff --git a/tests/match/__snapshots__/jsfmt.spec.js.snap b/tests/match/__snapshots__/jsfmt.spec.js.snap
index d2414f3..92f1e46 100644
--- a/tests/match/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/match/__snapshots__/jsfmt.spec.js.snap
@@ -39,6 +39,19 @@ $nest = match(match($a) {true => 1, false => 2}) {
     },
     2 => 'null'
 };
+
+$extraLongMatch = match($a) {
+    'foo', 'foo2', 'foo3', 'foo4',
+    'foo5', 'foo6', 'foo7', 'foo8',
+    'foo9', 'foo10', 'foo11', 'foo12',
+    => ['bar'],
+    'bar', 'bar2', 'bar3', 'bar4',
+    'bar5', 'bar6', 'bar7', 'bar8',
+    'bar9', 'bar10', 'bar11', 'bar12',
+    => 'some really long value in the return part of the match statement',
+    'cd' => [],
+    default => [],
+};
 =====================================output=====================================
 <?php
 
@@ -80,6 +93,37 @@ match ($a) {
     2 => "null"
 };
 
+$extraLongMatch = match ($a) {
+    "foo",
+    "foo2",
+    "foo3",
+    "foo4",
+    "foo5",
+    "foo6",
+    "foo7",
+    "foo8",
+    "foo9",
+    "foo10",
+    "foo11",
+    "foo12"
+        => ["bar"],
+    "bar",
+    "bar2",
+    "bar3",
+    "bar4",
+    "bar5",
+    "bar6",
+    "bar7",
+    "bar8",
+    "bar9",
+    "bar10",
+    "bar11",
+    "bar12"
+        => "some really long value in the return part of the match statement",
+    "cd" => [],
+    default => []
+};
+
 ================================================================================
 `;
 
@@ -122,6 +166,19 @@ $nest = match(match($a) {true => 1, false => 2}) {
     },
     2 => 'null'
 };
+
+$extraLongMatch = match($a) {
+    'foo', 'foo2', 'foo3', 'foo4',
+    'foo5', 'foo6', 'foo7', 'foo8',
+    'foo9', 'foo10', 'foo11', 'foo12',
+    => ['bar'],
+    'bar', 'bar2', 'bar3', 'bar4',
+    'bar5', 'bar6', 'bar7', 'bar8',
+    'bar9', 'bar10', 'bar11', 'bar12',
+    => 'some really long value in the return part of the match statement',
+    'cd' => [],
+    default => [],
+};
 =====================================output=====================================
 <?php
 
@@ -163,5 +220,36 @@ match ($a) {
     2 => "null",
 };
 
+$extraLongMatch = match ($a) {
+    "foo",
+    "foo2",
+    "foo3",
+    "foo4",
+    "foo5",
+    "foo6",
+    "foo7",
+    "foo8",
+    "foo9",
+    "foo10",
+    "foo11",
+    "foo12"
+        => ["bar"],
+    "bar",
+    "bar2",
+    "bar3",
+    "bar4",
+    "bar5",
+    "bar6",
+    "bar7",
+    "bar8",
+    "bar9",
+    "bar10",
+    "bar11",
+    "bar12"
+        => "some really long value in the return part of the match statement",
+    "cd" => [],
+    default => [],
+};
+
 ================================================================================
 `;
diff --git a/tests/match/match.php b/tests/match/match.php
index ce35e7f..25206cb 100644
--- a/tests/match/match.php
+++ b/tests/match/match.php
@@ -28,4 +28,17 @@ $nest = match(match($a) {true => 1, false => 2}) {
         default => false
     },
     2 => 'null'
+};
+
+$extraLongMatch = match($a) {
+    'foo', 'foo2', 'foo3', 'foo4',
+    'foo5', 'foo6', 'foo7', 'foo8',
+    'foo9', 'foo10', 'foo11', 'foo12',
+    => ['bar'],
+    'bar', 'bar2', 'bar3', 'bar4',
+    'bar5', 'bar6', 'bar7', 'bar8',
+    'bar9', 'bar10', 'bar11', 'bar12',
+    => 'some really long value in the return part of the match statement',
+    'cd' => [],
+    default => [],
 };
\ No newline at end of file