Repository: prettier/plugin-php
Pull request #615 (2018-09-12 09:30:30)

test: parens in `retif` node


Patch:
diff --git a/src/needs-parens.js b/src/needs-parens.js
index 42787d5..efd3608 100644
--- a/src/needs-parens.js
+++ b/src/needs-parens.js
@@ -165,6 +165,12 @@ function needsParens(path) {
     case "propertylookup":
     case "staticlookup":
     case "offsetlookup":
+      return false;
+    case "print":
+      if (parent.kind === "bin") {
+        return true;
+      }
+
       return false;
     case "bin": {
       if (["pre", "post"].includes(parent.kind)) {


Test patch:
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index c64663a..00c2976 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1055,6 +1055,90 @@ $a->c();
 
 `;
 
+exports[`echo.php 1`] = `
+<?php
+
+echo "Hello World";
+echo ("Hello World");
+
+echo "Sum: ", 1 + 2;
+echo "Hello ", isset($name) ? $name : "John Doe", "!";
+echo "Hello ", (isset($name) ? $name : "John Doe"), "!";
+
+echo 'Sum: ' . (1 + 2);
+echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';
+echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';
+
+echo $some_var ? 'true': 'false';
+echo ($some_var ? 'true': 'false');
+
+echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
+echo ('This '), ('string '), ('was '), ('made '), ('with multiple parameters.'), (chr(10));
+echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\\n";
+echo ('This ') . ('string ') . ('was ') . ('made ') . ('with concatenation.') . ("\\n");
+
+echo <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon. no extra whitespace!
+END;
+
+echo (<<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon. no extra whitespace!
+END
+);
+
+echo (function () { return 'test'; })();
+echo ((function () { return 'test'; })());
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+echo "Hello World";
+echo "Hello World";
+
+echo "Sum: ", 1 + 2;
+echo "Hello ", isset($name) ? $name : "John Doe", "!";
+echo "Hello ", isset($name) ? $name : "John Doe", "!";
+
+echo 'Sum: ' . (1 + 2);
+echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';
+echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';
+
+echo $some_var ? 'true' : 'false';
+echo $some_var ? 'true' : 'false';
+
+echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
+echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
+echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\\n";
+echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\\n";
+
+echo <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon. no extra whitespace!
+END;
+
+echo <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon. no extra whitespace!
+END;
+
+echo (function () {
+    return 'test';
+})();
+echo (function () {
+    return 'test';
+})();
+
+`;
+
 exports[`empty.php 1`] = `
 <?php
 
@@ -1551,6 +1635,98 @@ $var = call(--$var->_uuidCounter);
 
 `;
 
+exports[`print.php 1`] = `
+<?php
+
+print("Hello World");
+(print("Hello World"));
+
+print "print() also works without parentheses.";
+
+print "This spans
+multiple lines. The newlines will be
+output as well";
+(print "This spans
+multiple lines. The newlines will be
+output as well");
+
+print <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon no extra whitespace!
+END;
+
+print (<<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon no extra whitespace!
+END
+);
+
+if (print("foo") && print("bar")) {
+}
+
+if ((print("foo")) && (print("bar"))) {
+}
+
+$var = $var ? print "$string_message<br />" : print "$string_message\\n";
+$var = $var ? (print "$string_message<br />") : (print "$string_message\\n");
+$var = $var ? (print ("$string_message<br />")) : (print ("$string_message\\n"));
+
+print 1 . print(2) + 3; // 511
+print 1 . (print(2)) + 3; // 214
+
+print ($var || $var) && $var;
+print (($var || $var) && $var);
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+print "Hello World";
+print "Hello World";
+
+print "print() also works without parentheses.";
+
+print "This spans
+multiple lines. The newlines will be
+output as well";
+print "This spans
+multiple lines. The newlines will be
+output as well";
+
+print <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon no extra whitespace!
+END;
+
+print <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon no extra whitespace!
+END;
+
+if (print "foo" && (print "bar")) {
+}
+
+if ((print "foo") && (print "bar")) {
+}
+
+$var = $var ? print "$string_message<br />" : print "$string_message\\n";
+$var = $var ? print "$string_message<br />" : print "$string_message\\n";
+$var = $var ? print "$string_message<br />" : print "$string_message\\n";
+
+print 1 . (print 2 + 3); // 511
+print 1 . (print 2) + 3; // 214
+
+print ($var || $var) && $var;
+print ($var || $var) && $var;
+
+`;
+
 exports[`program.php 1`] = `
 <?php
 
@@ -1606,6 +1782,7 @@ $var = +($var ? 1 : 2);
 $var = +(+$var ? 1 : 2);
 $var = +($var++ ? 1 : 2);
 $var = ((true ? 'true' : false) ? (true ? 'true' : false) : (true ? 'true' : false));
+$var = $var ? $var1 ? 1 : 2 : $var2 ? 3 : 4;
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -1654,6 +1831,7 @@ $var = +($var ? 1 : 2);
 $var = +(+$var ? 1 : 2);
 $var = +($var++ ? 1 : 2);
 $var = (true ? 'true' : false) ? true ? 'true' : false : true ? 'true' : false;
+$var = ($var ? $var1 ? 1 : 2 : $var2) ? 3 : 4;
 
 `;
 
@@ -1711,6 +1889,45 @@ $var = @foo();
 
 `;
 
+exports[`throw.php 1`] = `
+<?php
+
+throw new Exception('Division by zero.');
+throw (new Exception('Division by zero.'));
+throw $e;
+throw ($e);
+throw new \\Exception("Bye");
+throw (new \\Exception("Bye"));
+
+if (!$var) {
+    throw new Exception('Division by zero.');
+    throw (new Exception('Division by zero.'));
+    throw $e;
+    throw ($e);
+    throw new \\Exception("Bye");
+    throw (new \\Exception("Bye"));
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+throw new Exception('Division by zero.');
+throw new Exception('Division by zero.');
+throw $e;
+throw $e;
+throw new \\Exception("Bye");
+throw new \\Exception("Bye");
+
+if (!$var) {
+    throw new Exception('Division by zero.');
+    throw new Exception('Division by zero.');
+    throw $e;
+    throw $e;
+    throw new \\Exception("Bye");
+    throw new \\Exception("Bye");
+}
+
+`;
+
 exports[`unary.php 1`] = `
 <?php
 
diff --git a/tests/parens/echo.php b/tests/parens/echo.php
new file mode 100644
index 0000000..1de4fcc
--- /dev/null
+++ b/tests/parens/echo.php
@@ -0,0 +1,38 @@
+<?php
+
+echo "Hello World";
+echo ("Hello World");
+
+echo "Sum: ", 1 + 2;
+echo "Hello ", isset($name) ? $name : "John Doe", "!";
+echo "Hello ", (isset($name) ? $name : "John Doe"), "!";
+
+echo 'Sum: ' . (1 + 2);
+echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';
+echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';
+
+echo $some_var ? 'true': 'false';
+echo ($some_var ? 'true': 'false');
+
+echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
+echo ('This '), ('string '), ('was '), ('made '), ('with multiple parameters.'), (chr(10));
+echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
+echo ('This ') . ('string ') . ('was ') . ('made ') . ('with concatenation.') . ("\n");
+
+echo <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon. no extra whitespace!
+END;
+
+echo (<<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon. no extra whitespace!
+END
+);
+
+echo (function () { return 'test'; })();
+echo ((function () { return 'test'; })());
diff --git a/tests/parens/print.php b/tests/parens/print.php
new file mode 100644
index 0000000..1651f0a
--- /dev/null
+++ b/tests/parens/print.php
@@ -0,0 +1,44 @@
+<?php
+
+print("Hello World");
+(print("Hello World"));
+
+print "print() also works without parentheses.";
+
+print "This spans
+multiple lines. The newlines will be
+output as well";
+(print "This spans
+multiple lines. The newlines will be
+output as well");
+
+print <<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon no extra whitespace!
+END;
+
+print (<<<END
+This uses the "here document" syntax to output
+multiple lines with $variable interpolation. Note
+that the here document terminator must appear on a
+line with just a semicolon no extra whitespace!
+END
+);
+
+if (print("foo") && print("bar")) {
+}
+
+if ((print("foo")) && (print("bar"))) {
+}
+
+$var = $var ? print "$string_message<br />" : print "$string_message\n";
+$var = $var ? (print "$string_message<br />") : (print "$string_message\n");
+$var = $var ? (print ("$string_message<br />")) : (print ("$string_message\n"));
+
+print 1 . print(2) + 3; // 511
+print 1 . (print(2)) + 3; // 214
+
+print ($var || $var) && $var;
+print (($var || $var) && $var);
diff --git a/tests/parens/retif.php b/tests/parens/retif.php
index 84861e8..418e6a3 100644
--- a/tests/parens/retif.php
+++ b/tests/parens/retif.php
@@ -41,3 +41,4 @@ $var = +($var ? 1 : 2);
 $var = +(+$var ? 1 : 2);
 $var = +($var++ ? 1 : 2);
 $var = ((true ? 'true' : false) ? (true ? 'true' : false) : (true ? 'true' : false));
+$var = $var ? $var1 ? 1 : 2 : $var2 ? 3 : 4;
diff --git a/tests/parens/throw.php b/tests/parens/throw.php
new file mode 100644
index 0000000..004add3
--- /dev/null
+++ b/tests/parens/throw.php
@@ -0,0 +1,17 @@
+<?php
+
+throw new Exception('Division by zero.');
+throw (new Exception('Division by zero.'));
+throw $e;
+throw ($e);
+throw new \Exception("Bye");
+throw (new \Exception("Bye"));
+
+if (!$var) {
+    throw new Exception('Division by zero.');
+    throw (new Exception('Division by zero.'));
+    throw $e;
+    throw ($e);
+    throw new \Exception("Bye");
+    throw (new \Exception("Bye"));
+}