Repository: prettier/plugin-php
Pull request #562 (2018-08-31 15:29:58)

feat: better print entry node
Logic from prettier https://github.com/prettier/prettier/blob/master/src/language-js/printer-estree.js#L1270


Patch:
diff --git a/src/printer.js b/src/printer.js
index 7c9b0ee..2179b49 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1174,7 +1174,9 @@ function printAssignmentRight(leftNode, rightNode, printedRight) {
     (rightNode.kind === "retif" &&
       rightNode.test.kind === "bin" &&
       !shouldInlineLogicalExpression(rightNode.test)) ||
-    ((leftNode.kind === "variable" || isLookupNode(leftNode)) &&
+    ((leftNode.kind === "variable" ||
+      leftNode.kind === "string" ||
+      isLookupNode(leftNode)) &&
       ((rightNode.kind === "string" && !rightNode.raw.includes("\n")) ||
         isLookupNodeChain(rightNode)));
 
@@ -2450,31 +2452,15 @@ function printNode(path, options, print) {
 
       return concat(parts);
     }
-    case "entry": {
-      const dangling = comments.printDanglingComments(
-        path,
-        options,
-        /* sameLine */ true
+    case "entry":
+      return printAssignment(
+        node.key,
+        path.call(print, "key"),
+        " =>",
+        node.value,
+        path.call(print, "value"),
+        options
       );
-      const printedComments = dangling ? concat([hardline, dangling]) : "";
-      const printed = concat([path.call(print, "value"), printedComments]);
-
-      if (node.key) {
-        return group(
-          concat([
-            path.call(print, "key"),
-            " =>",
-            ["array", "call", "closure", "retif", "number"].includes(
-              node.value.kind
-            )
-              ? concat([" ", printed])
-              : indent(concat([line, printed]))
-          ])
-        );
-      }
-
-      return printed;
-    }
     case "traituse":
       return group(
         concat([


Test patch:
diff --git a/tests/array/__snapshots__/jsfmt.spec.js.snap b/tests/array/__snapshots__/jsfmt.spec.js.snap
index 72157ac..130f5db 100644
--- a/tests/array/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/array/__snapshots__/jsfmt.spec.js.snap
@@ -681,12 +681,13 @@ $array = [
     'key-two' => true
         ? 1
         : 'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString',
-    'key-two' => 'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
-    'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
-        ? 'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
-            'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
-        : 'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
-            'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
+    'key-two' =>
+        'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
+        'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
+            ? 'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
+                'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
+            : 'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString' .
+                'VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString'
 ];
 
 $array = [
diff --git a/tests/case/__snapshots__/jsfmt.spec.js.snap b/tests/case/__snapshots__/jsfmt.spec.js.snap
index d672a67..3ec0fd9 100644
--- a/tests/case/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/case/__snapshots__/jsfmt.spec.js.snap
@@ -153,6 +153,8 @@ FINAL CLASS MyClass EXTENDS Bar IMPLEMENTS iTemplate
 
         var_dump($a INSTANCEOF MyClass);
 
+        $bar = (INT) $foo;
+
         DIE();
         EXIT();
         EVAL('test');
@@ -400,6 +402,8 @@ final class MyClass extends Bar implements iTemplate
 
         var_dump($a instanceof MyClass);
 
+        $bar = (int) $foo;
+
         die();
         exit();
         eval('test');
diff --git a/tests/case/case.php b/tests/case/case.php
index abf739f..23019a8 100644
--- a/tests/case/case.php
+++ b/tests/case/case.php
@@ -150,6 +150,8 @@ FINAL CLASS MyClass EXTENDS Bar IMPLEMENTS iTemplate
 
         var_dump($a INSTANCEOF MyClass);
 
+        $bar = (INT) $foo;
+
         DIE();
         EXIT();
         EVAL('test');
diff --git a/tests/foreach/__snapshots__/jsfmt.spec.js.snap b/tests/foreach/__snapshots__/jsfmt.spec.js.snap
index b21b305..057e4d6 100644
--- a/tests/foreach/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/foreach/__snapshots__/jsfmt.spec.js.snap
@@ -140,10 +140,8 @@ foreach (
 foreach (
     $points
     as [
-        "veryVeryVeryVeryVeryVeryVeryLongKey" =>
-            $veryVeryVeryVeryVeryVeryVeryVeryLongX,
-        "veryVeryVeryVeryVeryVeryVeryLongKey" =>
-            $veryVeryVeryVeryVeryVeryVeryVeryLongY
+        "veryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryLongX,
+        "veryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryLongY
     ]
 ) {
     var_dump($x, $y);
diff --git a/tests/list/__snapshots__/jsfmt.spec.js.snap b/tests/list/__snapshots__/jsfmt.spec.js.snap
index e4c4cd5..171421d 100644
--- a/tests/list/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/list/__snapshots__/jsfmt.spec.js.snap
@@ -142,10 +142,8 @@ list(
     "veryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryLongValue
 ) = $data[0];
 list(
-    "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" =>
-        $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue,
-    "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" =>
-        $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue
+    "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue,
+    "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue
 ) = $data[0];
 
 foreach ($data as list("id" => $id, "name" => $name)) {
@@ -165,10 +163,8 @@ foreach (
 foreach (
     $data
     as list(
-        "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" =>
-            $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue,
-        "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" =>
-            $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue
+        "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue,
+        "veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" => $veryVeryVeryVeryVeryVeryVeryVeryVeryLongValue
     )
 ) {
     // logic here with $id and $name
diff --git a/tests/retif/__snapshots__/jsfmt.spec.js.snap b/tests/retif/__snapshots__/jsfmt.spec.js.snap
index 7d27199..14b522c 100644
--- a/tests/retif/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/retif/__snapshots__/jsfmt.spec.js.snap
@@ -55,6 +55,19 @@ $var = $test
         ? $someOtherReallyReallyLongVariable
         : $someOtherReallyReallyLongVariable)
     : 'string';
+
+$arr = [
+    'step' => !empty($field['sliderStep']) && $field['sliderStep']
+        ? (int) $field['sliderStep']
+        : 1,
+    'step' => true ? (int) $field['sliderStep'] : 1,
+    'step' => true ? $someOtherReallyReallyLongVariable . $someOtherReallyReallyLongVariable : 1,
+    'step' => $someOtherReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyLongVariable . 'string' ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyLongVariable . $someOtherReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyReallyReallyLongVariable . $someOtherReallyReallyReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyReallyReallyLongVariable && $someOtherReallyReallyReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+];
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -181,4 +194,37 @@ $var = $test
         : $someOtherReallyReallyLongVariable)
     : 'string';
 
+$arr = [
+    'step' =>
+        !empty($field['sliderStep']) && $field['sliderStep']
+            ? (int) $field['sliderStep']
+            : 1,
+    'step' => true ? (int) $field['sliderStep'] : 1,
+    'step' => true
+        ? $someOtherReallyReallyLongVariable .
+            $someOtherReallyReallyLongVariable
+        : 1,
+    'step' => $someOtherReallyReallyLongVariable
+        ? (int) $field['sliderStep']
+        : 1,
+    'step' =>
+        $someOtherReallyReallyLongVariable . 'string'
+            ? (int) $field['sliderStep']
+            : 1,
+    'step' =>
+        $someOtherReallyReallyLongVariable . $someOtherReallyReallyLongVariable
+            ? (int) $field['sliderStep']
+            : 1,
+    'step' =>
+        $someOtherReallyReallyReallyReallyLongVariable .
+        $someOtherReallyReallyReallyReallyLongVariable
+            ? (int) $field['sliderStep']
+            : 1,
+    'step' =>
+        $someOtherReallyReallyReallyReallyLongVariable &&
+        $someOtherReallyReallyReallyReallyLongVariable
+            ? (int) $field['sliderStep']
+            : 1
+];
+
 `;
diff --git a/tests/retif/retif.php b/tests/retif/retif.php
index ee28e27..0f5c1f4 100644
--- a/tests/retif/retif.php
+++ b/tests/retif/retif.php
@@ -52,3 +52,16 @@ $var = $test
         ? $someOtherReallyReallyLongVariable
         : $someOtherReallyReallyLongVariable)
     : 'string';
+
+$arr = [
+    'step' => !empty($field['sliderStep']) && $field['sliderStep']
+        ? (int) $field['sliderStep']
+        : 1,
+    'step' => true ? (int) $field['sliderStep'] : 1,
+    'step' => true ? $someOtherReallyReallyLongVariable . $someOtherReallyReallyLongVariable : 1,
+    'step' => $someOtherReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyLongVariable . 'string' ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyLongVariable . $someOtherReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyReallyReallyLongVariable . $someOtherReallyReallyReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+    'step' => $someOtherReallyReallyReallyReallyLongVariable && $someOtherReallyReallyReallyReallyLongVariable ? (int) $field['sliderStep'] : 1,
+];