Repository: prettier/plugin-php
Pull request #637 (2018-09-15 14:47:53)

feat: better print static
https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAbOMAEASAhgGhwCMBuAHSnS2wDc4AnATwDUGW3WnP2uO+mAMtADmzXPQCWuIukK1+PRd2ULuQqKPFSZccpQw4wmALyYATHqqGTmAIz2Hjp85eu37j59eWD2eAGcYACU4XFRURhCwiPVhG1s5fwgAWzgAeRgACwYo8MjQvNixSWl0GzM5aDgAWQh6OFyYkWLtMtMAZh9qXBsAcjpeQaVVEaGVMYVYgGUYSQ1eruI+geGJtdWN8Y3p2Yl5-ApMI6XTftHN862ry7YdueEFiis8GwBtfrCAVzhewl6ILIMAC0NC+PwAuosXqZ3qDUN9fph-oD6CCwYjkdlUQCsUCccD8diUXjiYSSbi4QjIU9fD0YR94T8-mTKRC5EQ3gyEcziazetT9N1OXyeRT0aKCaSpbiybLpcC+eD2cLxUiWarMZKZfKidq9VqFeiBc86ZhgL0ANZwRi9JBIkVq4lWm12zWovkAXyhpuAh2Oluttvt4r9RzdQOdQfDfIoXppQtM5sjrod4eTjrFjN6HuViYDLuDWYlqPT0fRccFOB9oeOYdLIprtbTgdd6qzFDIMBzNewHMTjf99ZDUFrTbJpbbCJrFZNfUC916mAA1Ej53sHsvV7t5pvemudyu99uN4f9yetwvd2fHpW8LmL+vF6fj0+Hwe3+ej5fn9+P6+v4+JAgPgIAQAADjAEjQP4yCgOI9AQAA7gACuICAwSguA0BAEgACbASARD0LgYBWjAUxgSR67ILM3wgXs-gMDAyHEcIyS4MgABmYSMSBABW-gAB4AELEaRGBTLgqQCHscBcTxcAgZR9CMfQyCEdIjCoNABFgXMMAAOp4VkyAABwAAxKQhjEGcRYHqXpcCqXQBH1AAjp8Ej1CxuBsRxSDcagvEgIxyQSDR9B0SF67oAAip8AJyQFCkgTA0hGbhJlIBUICzLgEioOuADCKTsepUBVARnyMQAKtIGGBYxHoekAA


Patch:
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 97416b8..868a841 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -166,6 +166,8 @@ function needsParens(path) {
         return false;
       } else if (parent.kind === "assign") {
         return false;
+      } else if (parent.kind === "static") {
+        return false;
       }
 
       return true;
diff --git a/src/printer.js b/src/printer.js
index 784b5c9..5f85ed0 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2202,31 +2202,36 @@ function printNode(path, options, print) {
         path.call(print, "name"),
         node.curly ? "}" : ""
       ]);
-    case "static":
+    case "static": {
+      const printed = path.map(childPath => {
+        return print(childPath);
+      }, "items");
+
+      const hasValue = node.items.some(item => item.kind === "assign");
+
+      let firstVariable;
+
+      if (printed.length === 1) {
+        [firstVariable] = printed;
+      } else if (printed.length > 1) {
+        // Indent first item
+        firstVariable = indent(printed[0]);
+      }
+
       return group(
         concat([
-          "static ",
+          "static",
+          firstVariable ? concat([" ", firstVariable]) : "",
           indent(
-            concat([
-              join(
-                concat([",", line]),
-                path.map(item => {
-                  // @TODO: hacking this for now. assignments nested inside a static
-                  // declaration doesn't have the operator set, so printing manually
-                  if (item.getValue().kind === "assign") {
-                    return concat([
-                      item.call(print, "left"),
-                      " = ",
-                      item.call(print, "right")
-                    ]);
-                  }
-                  return item.call(print);
-                }, "items")
-              )
-            ])
+            concat(
+              printed
+                .slice(1)
+                .map(p => concat([",", hasValue ? hardline : line, p]))
+            )
           )
         ])
       );
+    }
     case "list":
     case "array": {
       const open = node.shortForm ? "[" : concat([node.kind, "("]);
@@ -2317,7 +2322,8 @@ function printNode(path, options, print) {
       return printAssignment(
         node.left,
         path.call(print, "left"),
-        concat([" ", node.operator]),
+        // Assignments nested inside a static declaration doesn't have the operator set, so printing manually
+        concat([" ", node.operator ? node.operator : "="]),
         node.right,
         path.call(print, "right"),
         options


Test patch:
diff --git a/tests/static/__snapshots__/jsfmt.spec.js.snap b/tests/static/__snapshots__/jsfmt.spec.js.snap
index 539d486..183cee7 100644
--- a/tests/static/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/static/__snapshots__/jsfmt.spec.js.snap
@@ -3,14 +3,100 @@
 exports[`static.php 1`] = `
 <?php
 static $a, $b;
+static $veryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable, $veryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable;
 static $c = 2;
+static $c = 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111;
 static $testReallyReallyLong = 1, $someOtherReallyReallyLongVariable = 2, $oneMoreReallyLongVariable = 3;
+static $a = 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
+static $b = 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString',
+    $b = 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
+static $a = ['value', 'other-value'];
+static $a = ['value', 'other-value', 'other-other-other-other-other-other-other-other-value'];
+static $a = ['value', 'other-value'], $b = ['value', 'other-value'];
+static $a = ['value', 'other-value', 'other-other-other-other-other-other-other-other-value'], $b = ['value', 'other-value', 'other-other-other-other-other-other-other-other-value'];
+static $a = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+    'key' => 'value',
+    'other-key' => 'other-value'
+];
+static $a = ['key' => 'value', 'other-key' => 'other-value'], $b = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+        'key' => 'value',
+        'other-key' => 'other-value'
+    ],
+    $b = [
+        'key' => 'value',
+        'other-key' => 'other-value'
+    ];
+static $a = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';
+static $a, $b = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 static $a, $b;
+static $veryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable,
+    $veryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable;
 static $c = 2;
+static $c = 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111;
 static $testReallyReallyLong = 1,
     $someOtherReallyReallyLongVariable = 2,
     $oneMoreReallyLongVariable = 3;
+static $a =
+    'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
+static $b =
+        'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString',
+    $b =
+        'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
+static $a = ['value', 'other-value'];
+static $a = [
+    'value',
+    'other-value',
+    'other-other-other-other-other-other-other-other-value'
+];
+static $a = ['value', 'other-value'],
+    $b = ['value', 'other-value'];
+static $a = [
+        'value',
+        'other-value',
+        'other-other-other-other-other-other-other-other-value'
+    ],
+    $b = [
+        'value',
+        'other-value',
+        'other-other-other-other-other-other-other-other-value'
+    ];
+static $a = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+    'key' => 'value',
+    'other-key' => 'other-value'
+];
+static $a = ['key' => 'value', 'other-key' => 'other-value'],
+    $b = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+        'key' => 'value',
+        'other-key' => 'other-value'
+    ],
+    $b = [
+        'key' => 'value',
+        'other-key' => 'other-value'
+    ];
+static $a =
+    'string' .
+    'string' .
+    'string' .
+    'string' .
+    'string' .
+    'string' .
+    'string' .
+    'string';
+static $a,
+    $b =
+        'string' .
+        'string' .
+        'string' .
+        'string' .
+        'string' .
+        'string' .
+        'string' .
+        'string';
 
 `;
diff --git a/tests/static/static.php b/tests/static/static.php
index 103bb15..833446a 100644
--- a/tests/static/static.php
+++ b/tests/static/static.php
@@ -1,4 +1,29 @@
 <?php
 static $a, $b;
+static $veryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable, $veryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable;
 static $c = 2;
+static $c = 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111;
 static $testReallyReallyLong = 1, $someOtherReallyReallyLongVariable = 2, $oneMoreReallyLongVariable = 3;
+static $a = 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
+static $b = 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString',
+    $b = 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString';
+static $a = ['value', 'other-value'];
+static $a = ['value', 'other-value', 'other-other-other-other-other-other-other-other-value'];
+static $a = ['value', 'other-value'], $b = ['value', 'other-value'];
+static $a = ['value', 'other-value', 'other-other-other-other-other-other-other-other-value'], $b = ['value', 'other-value', 'other-other-other-other-other-other-other-other-value'];
+static $a = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+    'key' => 'value',
+    'other-key' => 'other-value'
+];
+static $a = ['key' => 'value', 'other-key' => 'other-value'], $b = ['key' => 'value', 'other-key' => 'other-value'];
+static $a = [
+        'key' => 'value',
+        'other-key' => 'other-value'
+    ],
+    $b = [
+        'key' => 'value',
+        'other-key' => 'other-value'
+    ];
+static $a = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';
+static $a, $b = 'string' . 'string' . 'string' . 'string' . 'string' . 'string'. 'string' . 'string';