Repository: prettier/plugin-php
Pull request #693 (2018-10-01 16:39:52)

fix: print retif without true expr
Small fixes


Patch:
diff --git a/README.md b/README.md
index d354af8..a2a8e5c 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@
   <a href="https://gitter.im/jlongster/prettier">
     <img alt="Gitter" src="https://img.shields.io/gitter/room/jlongster/prettier.svg?style=flat-square">
   </a>
-  <a href="https://travis-ci.org/prettier/prettier-php">
+  <a href="https://travis-ci.org/prettier/plugin-php/">
     <img alt="Travis" src="https://img.shields.io/travis/prettier/plugin-php/master.svg?style=flat-square">
   </a>
   <a href="https://www.npmjs.com/package/@prettier/plugin-php">
diff --git a/src/printer.js b/src/printer.js
index ad092f5..788073d 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2045,11 +2045,10 @@ function printNode(path, options, print) {
         if (comments.returnArgumentHasLeadingComment(options, node.expr)) {
           parts.push(indent(concat([hardline, printedExpr])));
         } else if (
-          (node.expr.kind === "retif" && !node.expr.trueExpr) ||
-          (node.expr.kind === "bin" &&
-            (!isLookupNode(node.expr.left) &&
-              node.expr.left.kind !== "call" &&
-              node.expr.left.kind !== "new"))
+          node.expr.kind === "bin" &&
+          (!isLookupNode(node.expr.left) &&
+            node.expr.left.kind !== "call" &&
+            node.expr.left.kind !== "new")
         ) {
           parts.push(group(concat([" ", indent(concat([printedExpr]))])));
         } else {
@@ -2346,7 +2345,12 @@ function printNode(path, options, print) {
       if (!node.trueExpr) {
         const printed = concat([
           printedTest,
-          parent.kind === "bin" ? indent(concat(parts)) : concat(parts)
+          parent.kind === "bin" ||
+          ["print", "echo", "return", "include"].includes(
+            firstNonRetifParent.kind
+          )
+            ? indent(concat(parts))
+            : concat(parts)
         ]);
 
         // Break between the parens in unaries or in a lookup nodes, i.e.


Test patch:
diff --git a/tests/retif/__snapshots__/jsfmt.spec.js.snap b/tests/retif/__snapshots__/jsfmt.spec.js.snap
index 5c6d514..d050c5e 100644
--- a/tests/retif/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/retif/__snapshots__/jsfmt.spec.js.snap
@@ -117,6 +117,36 @@ $var = $this->databasePath ?: $this->basePath . DIRECTORY_SEPARATOR . 'database'
 $var = $var ?: $otherVar ?: 'string' . 'string';
 $var = $var ?: $otherVar ?: 'string' . 'string' . 'string' . 'string' . 'string' . 'string';
 $var = $var ?: $otherVar ?: 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string';
+
+echo $var ? 'string' : 'other-string';
+echo $var ?: $var ?: $var ?: 'string';
+echo $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+echo $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+print $var ? 'string' : 'other-string';
+print $var ?: $var ?: $var ?: 'string';
+print $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+print $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+include $var ? 'string' : 'other-string';
+include $var ?: $var ?: $var ?: 'string';
+include $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+include $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+$var = include $var ? 'string' : 'other-string';
+$var = include $var ?: $var ?: $var ?: 'string';
+$var = include $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+$var = include $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+call($var ? 'string' : 'other-string');
+call($var ?: $var ?: $var ?: 'string');
+call($someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable);
+call($someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string');
+
+exit($var ? 'string' : 'other-string');
+exit($var ?: $var ?: $var ?: 'string');
+exit($someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable);
+exit($someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string');
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -413,4 +443,72 @@ $var =
         'string' .
         'string';
 
+echo $var ? 'string' : 'other-string';
+echo $var ?: $var ?: $var ?: 'string';
+echo $someOtherReallyReallyLongVariable
+    ? $someOtherReallyReallyLongVariable
+    : $someOtherReallyReallyLongVariable;
+echo $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    'string';
+
+print $var ? 'string' : 'other-string';
+print $var ?: $var ?: $var ?: 'string';
+print $someOtherReallyReallyLongVariable
+    ? $someOtherReallyReallyLongVariable
+    : $someOtherReallyReallyLongVariable;
+print $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    'string';
+
+include $var ? 'string' : 'other-string';
+include $var ?: $var ?: $var ?: 'string';
+include $someOtherReallyReallyLongVariable
+    ? $someOtherReallyReallyLongVariable
+    : $someOtherReallyReallyLongVariable;
+include $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    'string';
+
+$var = include $var ? 'string' : 'other-string';
+$var = include $var ?: $var ?: $var ?: 'string';
+$var = include $someOtherReallyReallyLongVariable
+    ? $someOtherReallyReallyLongVariable
+    : $someOtherReallyReallyLongVariable;
+$var = include $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    'string';
+
+call($var ? 'string' : 'other-string');
+call($var ?: $var ?: $var ?: 'string');
+call(
+    $someOtherReallyReallyLongVariable
+        ? $someOtherReallyReallyLongVariable
+        : $someOtherReallyReallyLongVariable
+);
+call(
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    'string'
+);
+
+exit($var ? 'string' : 'other-string');
+exit($var ?: $var ?: $var ?: 'string');
+exit(
+    $someOtherReallyReallyLongVariable
+        ? $someOtherReallyReallyLongVariable
+        : $someOtherReallyReallyLongVariable
+);
+exit(
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    $someOtherReallyReallyLongVariable ?:
+    'string'
+);
+
 `;
diff --git a/tests/retif/retif.php b/tests/retif/retif.php
index 9e9ce01..6b2c523 100644
--- a/tests/retif/retif.php
+++ b/tests/retif/retif.php
@@ -114,3 +114,33 @@ $var = $this->databasePath ?: $this->basePath . DIRECTORY_SEPARATOR . 'database'
 $var = $var ?: $otherVar ?: 'string' . 'string';
 $var = $var ?: $otherVar ?: 'string' . 'string' . 'string' . 'string' . 'string' . 'string';
 $var = $var ?: $otherVar ?: 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string' . 'string';
+
+echo $var ? 'string' : 'other-string';
+echo $var ?: $var ?: $var ?: 'string';
+echo $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+echo $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+print $var ? 'string' : 'other-string';
+print $var ?: $var ?: $var ?: 'string';
+print $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+print $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+include $var ? 'string' : 'other-string';
+include $var ?: $var ?: $var ?: 'string';
+include $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+include $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+$var = include $var ? 'string' : 'other-string';
+$var = include $var ?: $var ?: $var ?: 'string';
+$var = include $someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable;
+$var = include $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string';
+
+call($var ? 'string' : 'other-string');
+call($var ?: $var ?: $var ?: 'string');
+call($someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable);
+call($someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string');
+
+exit($var ? 'string' : 'other-string');
+exit($var ?: $var ?: $var ?: 'string');
+exit($someOtherReallyReallyLongVariable ? $someOtherReallyReallyLongVariable : $someOtherReallyReallyLongVariable);
+exit($someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: $someOtherReallyReallyLongVariable ?: 'string');