Repository: prettier/plugin-php
Pull request #728 (2018-10-03 18:42:35)

feat: better print assign and retif without true expr

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/printer.js b/src/printer.js
index 96923b0..5026e8a 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -676,6 +676,10 @@ function wrapPropertyLookup(node, doc) {
   return addCurly ? concat(["{", doc, "}"]) : doc;
 }
 
+function shouldInlineRetifFalseExpression(node) {
+  return node.kind === "array" && node.items.length !== 0;
+}
+
 function shouldInlineLogicalExpression(node) {
   return node.right.kind === "array" && node.right.items.length !== 0;
 }
@@ -1379,7 +1383,8 @@ function printAssignmentRight(leftNode, rightNode, printedRight, options) {
   const canBreak =
     (rightNode.kind === "bin" && !shouldInlineLogicalExpression(rightNode)) ||
     (rightNode.kind === "retif" &&
-      (!rightNode.trueExpr ||
+      ((!rightNode.trueExpr &&
+        !shouldInlineRetifFalseExpression(rightNode.falseExpr)) ||
         (rightNode.test.kind === "bin" &&
           !shouldInlineLogicalExpression(rightNode.test)))) ||
     ((leftNode.kind === "variable" ||
@@ -2335,7 +2340,10 @@ function printNode(path, options, print) {
         ":",
         node.trueExpr
           ? concat([" ", printedFalseExpr])
-          : concat([line, printedFalseExpr])
+          : concat([
+              shouldInlineRetifFalseExpression(node.falseExpr) ? " " : line,
+              printedFalseExpr
+            ])
       ]);
 
       parts.push(part);


Test patch:
diff --git a/tests/retif/__snapshots__/jsfmt.spec.js.snap b/tests/retif/__snapshots__/jsfmt.spec.js.snap
index d050c5e..42f63bb 100644
--- a/tests/retif/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/retif/__snapshots__/jsfmt.spec.js.snap
@@ -147,6 +147,34 @@ exit($var ? 'string' : 'other-string');
 exit($var ?: $var ?: $var ?: 'string');
 exit($someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable);
 exit($someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string');
+
+$var = $abilities ?: [];
+$var = $abilities ?: ['foo', 'bar', 'bar'];
+$var = $abilities ?: ['foo', 'bar', 'bar', 'veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'];
+$var = $abilities ?: ['foo', 'bar', 'bar', 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'];
+$var = $abilities ?: ['view' => 'view'];
+$var = $abilities ?: [
+    'view' => 'view'
+];
+$var = $abilities ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+];
+$var = $abilities ?: ['foo', 'bar', 'bar'] ?: ['foo', 'bar', 'bar'];
+$var = $abilities ?: ['foo', 'bar', 'bar'] ?: ['foo', 'bar', 'bar', 'veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'];
+$var = $abilities ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+] ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+];
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -511,4 +539,47 @@ exit(
     'string'
 );
 
+$var = $abilities ?: [];
+$var = $abilities ?: ['foo', 'bar', 'bar'];
+$var = $abilities ?: [
+    'foo',
+    'bar',
+    'bar',
+    'veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'
+];
+$var = $abilities ?: [
+    'foo',
+    'bar',
+    'bar',
+    'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'
+];
+$var = $abilities ?: ['view' => 'view'];
+$var = $abilities ?: [
+    'view' => 'view'
+];
+$var = $abilities ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+];
+$var = $abilities ?: ['foo', 'bar', 'bar'] ?: ['foo', 'bar', 'bar'];
+$var = $abilities ?: ['foo', 'bar', 'bar'] ?: [
+    'foo',
+    'bar',
+    'bar',
+    'veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'
+];
+$var = $abilities ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+] ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+];
+
 `;
diff --git a/tests/retif/retif.php b/tests/retif/retif.php
index 6b2c523..1a126ea 100644
--- a/tests/retif/retif.php
+++ b/tests/retif/retif.php
@@ -144,3 +144,31 @@ exit($var ? 'string' : 'other-string');
 exit($var ?: $var ?: $var ?: 'string');
 exit($someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable);
 exit($someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string');
+
+$var = $abilities ?: [];
+$var = $abilities ?: ['foo', 'bar', 'bar'];
+$var = $abilities ?: ['foo', 'bar', 'bar', 'veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'];
+$var = $abilities ?: ['foo', 'bar', 'bar', 'veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'];
+$var = $abilities ?: ['view' => 'view'];
+$var = $abilities ?: [
+    'view' => 'view'
+];
+$var = $abilities ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+];
+$var = $abilities ?: ['foo', 'bar', 'bar'] ?: ['foo', 'bar', 'bar'];
+$var = $abilities ?: ['foo', 'bar', 'bar'] ?: ['foo', 'bar', 'bar', 'veryVeryVeryVeryVeryVeryVeryVeryVeryLongKey'];
+$var = $abilities ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+] ?: [
+    'view' => 'view',
+    'create' => 'create',
+    'update' => 'update',
+    'delete' => 'delete'
+];