Repository: prettier/plugin-php
Pull request #184 (2018-03-08 22:57:49)

fix: nested if formatting in function parameter
#182 


Patch:
diff --git a/src/printer.js b/src/printer.js
index ffdb8d0..a59f0be 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -270,9 +270,11 @@ function printMemberChain(path, options, print) {
   return expanded;
 }
 
-function printArgumentsList(path, options, print) {
-  const args = path.getValue().arguments;
-  const printed = path.map(print, "arguments");
+function printArgumentsList(path, options, print, argumentsKey = "arguments") {
+  const args = path.getValue()[argumentsKey];
+  const printed = path
+    .map(print, argumentsKey)
+    .map(argument => group(argument));
   if (printed.length === 0) {
     return "()";
   }
@@ -1131,38 +1133,17 @@ function printStatement(path, options, print) {
       return node.alias ? concat([node.name, " as ", node.alias]) : node.name;
     case "closure":
       return concat([
-        "function (",
-        group(
-          concat([
-            indent(
-              join(
-                ", ",
-                path.map(
-                  argument => concat([softline, print(argument)]),
-                  "arguments"
-                )
-              )
-            ),
-            softline
-          ])
-        ),
+        "function ",
+        printArgumentsList(path, options, print),
         node.uses && node.uses.length > 0
           ? group(
               concat([
-                ") use (",
-                indent(
-                  join(
-                    ", ",
-                    path.map(use => {
-                      return concat([softline, print(use)]);
-                    }, "uses")
-                  )
-                ),
-                softline
+                " use ",
+                printArgumentsList(path, options, print, "uses")
               ])
             )
           : "",
-        group(") {"),
+        group(" {"),
         indent(concat([hardline, path.call(print, "body")])),
         concat([hardline, "}"])
       ]);


Test patch:
diff --git a/tests/closure/__snapshots__/jsfmt.spec.js.snap b/tests/closure/__snapshots__/jsfmt.spec.js.snap
index 45d353b..194deac 100644
--- a/tests/closure/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/closure/__snapshots__/jsfmt.spec.js.snap
@@ -108,11 +108,9 @@ $longArgs_shortVars = function (
     // body
 };
 
-$shortArgs_longVars = function ($arg) use (
-    $longVar1,
-    $longerVar2,
-    $muchLongerVar3
-) {
+$shortArgs_longVars = function (
+    $arg
+) use ($longVar1, $longerVar2, $muchLongerVar3) {
     // body
 };
 
diff --git a/tests/kitchen_sink/__snapshots__/jsfmt.spec.js.snap b/tests/kitchen_sink/__snapshots__/jsfmt.spec.js.snap
index 0d464c2..898e172 100644
--- a/tests/kitchen_sink/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/kitchen_sink/__snapshots__/jsfmt.spec.js.snap
@@ -33,6 +33,18 @@ do {
     really_long_function($test, $test, $test);
   }
 } while ($really_really_really_really_really_really_really_really_long_array >= 10);
+
+$this->test(function ($a, $b) {
+  if ($a == $b) {
+    return 0;
+  } else {
+    if ($a) {
+      return 1;
+    } else {
+      return 0;
+    }
+  }
+});
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 function hello($input)
@@ -77,4 +89,16 @@ do {
     $really_really_really_really_really_really_really_really_long_array >= 10
 );
 
+$this->test(function ($a, $b) {
+    if ($a == $b) {
+        return 0;
+    } else {
+        if ($a) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+});
+
 `;
diff --git a/tests/kitchen_sink/kitchen_sink.php b/tests/kitchen_sink/kitchen_sink.php
index a8522d1..638cecc 100644
--- a/tests/kitchen_sink/kitchen_sink.php
+++ b/tests/kitchen_sink/kitchen_sink.php
@@ -30,3 +30,15 @@ do {
     really_long_function($test, $test, $test);
   }
 } while ($really_really_really_really_really_really_really_really_long_array >= 10);
+
+$this->test(function ($a, $b) {
+  if ($a == $b) {
+    return 0;
+  } else {
+    if ($a) {
+      return 1;
+    } else {
+      return 0;
+    }
+  }
+});