Repository: prettier/plugin-php
Pull request #729 (2018-10-03 18:50:06)

test: bin `instanceof`
Just test


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/bin/__snapshots__/jsfmt.spec.js.snap b/tests/bin/__snapshots__/jsfmt.spec.js.snap
index 5d54098..9541fc2 100644
--- a/tests/bin/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/bin/__snapshots__/jsfmt.spec.js.snap
@@ -240,6 +240,15 @@ $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable
 $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? call($someOtherReallyReallyLongVariable, $someOtherReallyReallyLongVariable, $someOtherReallyReallyLongVariable);
 $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
 $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable;
+
+$var = $var instanceof Arrayable ? $var : $var;
+$var = $someOtherReallyReallyReallyLongVariable instanceof Arrayable ? $var : $var;
+$var = $someOtherReallyReallyReallyReallyLongVariable instanceof Arrayable ? $var : $var;
+$var = $var instanceof Arrayable ? $someOtherReallyReallyReallyLongVariable : $var;
+$var = $var instanceof Arrayable ? $someOtherReallyReallyReallyReallyLongVariable : $var;
+$var = $var instanceof Arrayable ? $var : $someOtherReallyReallyReallyLongVariable;
+$var = $var instanceof Arrayable ? $var : $someOtherReallyReallyReallyReallyLongVariable;
+$var = $someOtherReallyReallyReallyLongVariable instanceof Arrayable ? $someOtherReallyReallyReallyLongVariable : $someOtherReallyReallyReallyLongVariable;
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 $a++;
@@ -633,6 +642,30 @@ $var =
     $someOtherReallyReallyLongVariable ?:
     $someOtherReallyReallyLongVariable;
 
+$var = $var instanceof Arrayable ? $var : $var;
+$var =
+    $someOtherReallyReallyReallyLongVariable instanceof Arrayable ? $var : $var;
+$var =
+    $someOtherReallyReallyReallyReallyLongVariable instanceof Arrayable
+        ? $var
+        : $var;
+$var =
+    $var instanceof Arrayable ? $someOtherReallyReallyReallyLongVariable : $var;
+$var =
+    $var instanceof Arrayable
+        ? $someOtherReallyReallyReallyReallyLongVariable
+        : $var;
+$var =
+    $var instanceof Arrayable ? $var : $someOtherReallyReallyReallyLongVariable;
+$var =
+    $var instanceof Arrayable
+        ? $var
+        : $someOtherReallyReallyReallyReallyLongVariable;
+$var =
+    $someOtherReallyReallyReallyLongVariable instanceof Arrayable
+        ? $someOtherReallyReallyReallyLongVariable
+        : $someOtherReallyReallyReallyLongVariable;
+
 `;
 
 exports[`single.php - php-verify: single.php 1`] = `
diff --git a/tests/bin/bin.php b/tests/bin/bin.php
index dfbd442..d55ab80 100644
--- a/tests/bin/bin.php
+++ b/tests/bin/bin.php
@@ -237,3 +237,12 @@ $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable
 $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? call($someOtherReallyReallyLongVariable, $someOtherReallyReallyLongVariable, $someOtherReallyReallyLongVariable);
 $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
 $var = $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?? $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable;
+
+$var = $var instanceof Arrayable ? $var : $var;
+$var = $someOtherReallyReallyReallyLongVariable instanceof Arrayable ? $var : $var;
+$var = $someOtherReallyReallyReallyReallyLongVariable instanceof Arrayable ? $var : $var;
+$var = $var instanceof Arrayable ? $someOtherReallyReallyReallyLongVariable : $var;
+$var = $var instanceof Arrayable ? $someOtherReallyReallyReallyReallyLongVariable : $var;
+$var = $var instanceof Arrayable ? $var : $someOtherReallyReallyReallyLongVariable;
+$var = $var instanceof Arrayable ? $var : $someOtherReallyReallyReallyReallyLongVariable;
+$var = $someOtherReallyReallyReallyLongVariable instanceof Arrayable ? $someOtherReallyReallyReallyLongVariable : $someOtherReallyReallyReallyLongVariable;
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'
+];