Repository: prettier/plugin-php
Pull request #487 (2018-07-23 18:50:17)

feat: better print empty namespace

evilebottnawi: Need release 0.4.0 after merge :star: 

Patch:
diff --git a/src/printer.js b/src/printer.js
index 71abb53..a84ba30 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1070,8 +1070,6 @@ function printExpression(path, options, print) {
         return node.value ? "true" : "false";
       case "string": {
         // @TODO: need resolve https://github.com/glayzzle/php-parser/issues/101
-        // @TODO: need resolve https://github.com/glayzzle/php-parser/issues/123
-        // @TODO: need resolve https://github.com/glayzzle/php-parser/issues/124
         // @TODO: for now just reusing double/single quote preference from doc. could eventually
         // use setting from options. need to figure out how this works w/ complex strings and interpolation
         // also need to figure out splitting long strings
@@ -1333,12 +1331,18 @@ function printStatement(path, options, print) {
         return concat([
           "namespace ",
           hasName ? node.name : "",
-          node.withBrackets ? concat([" ", "{"]) : ";",
-          // don't know why we need 2 line breaks here, but 1 doesn't work
-          node.children.length > 0 && !node.withBrackets
-            ? concat([hardline, hardline])
+          node.withBrackets
+            ? " {"
+            : concat([
+                ";",
+                // Second hardline for newline between `namespace` and first child node
+                node.children.length > 0 ? concat([hardline, hardline]) : ""
+              ]),
+          node.children.length > 0
+            ? node.withBrackets
+              ? indent(concat([hardline, printed]))
+              : printed
             : "",
-          node.withBrackets ? indent(concat([hardline, printed])) : printed,
           node.withBrackets ? concat([hardline, "}"]) : ""
         ]);
       }


Test patch:
diff --git a/tests/namespace/__snapshots__/jsfmt.spec.js.snap b/tests/namespace/__snapshots__/jsfmt.spec.js.snap
index 7fbb510..96d658c 100644
--- a/tests/namespace/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/namespace/__snapshots__/jsfmt.spec.js.snap
@@ -24,6 +24,11 @@ namespace foo {
     }
     echo bar(2, 3);
 }
+
+namespace Name\\Space {
+    const FOO = 42;
+    function f() { echo __FUNCTION__."\\n"; }
+}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 namespace foo {
@@ -34,6 +39,14 @@ namespace foo {
     echo bar(2, 3);
 }
 
+namespace Name\\Space {
+    const FOO = 42;
+    function f()
+    {
+        echo __FUNCTION__ . "\\n";
+    }
+}
+
 `;
 
 exports[`single.php 1`] = `
@@ -51,7 +64,6 @@ namespace {}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 namespace  {
-
 }
 
 `;
diff --git a/tests/namespace/nested.php b/tests/namespace/nested.php
index 0e7cde3..2491457 100644
--- a/tests/namespace/nested.php
+++ b/tests/namespace/nested.php
@@ -6,3 +6,8 @@ namespace foo {
     }
     echo bar(2, 3);
 }
+
+namespace Name\Space {
+    const FOO = 42;
+    function f() { echo __FUNCTION__."\n"; }
+}
diff --git a/tests/string/__snapshots__/jsfmt.spec.js.snap b/tests/string/__snapshots__/jsfmt.spec.js.snap
index 8263803..6230d1b 100644
--- a/tests/string/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/string/__snapshots__/jsfmt.spec.js.snap
@@ -49,13 +49,16 @@ exports[`single.php 1`] = `
 exports[`string.php 1`] = `
 <?php
 $string = 'this is a simple string';
-// $string = 'You can also have embedded\\n newlines in
-// strings this way as it is
-// okay to do';
+$string = 'You can also have embedded\\n newlines in
+strings this way as it is
+okay to do';
+$string = "You can also have embedded\\n newlines in
+strings this way as it is
+okay to do";
 $string = 'Arnold once said: "I\\'ll be back"';
 $string = 'You deleted C:\\\\*.*?';
 $string = 'You deleted C:\\*.*?';
-// $string = 'This will not expand: \\n a newline';
+$string = 'This will not expand: \\n a newline';
 $string = 'Variables do not $expand $either';
 $string = 'qwe{$a}rty';
 $string = 'qwe\${a}rty';
@@ -67,10 +70,10 @@ $string = "foo\\vbar";
 $string = "foo\\ebar";
 $string = "foo\\fbar";
 $string = "foo\\\\bar";
-// $string = "foo\\\\\\bar";
+$string = "foo\\\\\\bar";
 $string = "foo\\\\\\\\bar";
-// $string = "foo\\$bar";
-// $string = "foo\\\\\\$bar";
+$string = "foo\\$bar";
+$string = "foo\\\\\\$bar";
 $string = "foo\\"bar";
 $string = "foo\\"";
 $string = "foo'bar";
@@ -89,19 +92,19 @@ $string = "encapsed variable {$value}.";
 $string = "encapsed offset {$value[0]}.";
 $string = "encapsed {$method->call()}.";
 $string = "combined \${$encapsed}.";
-// $string = "He drank some $juices[koolaid1] juice.";
+$string = "He drank some $juices[koolaid1] juice.";
 $string = "$people->john drank some $juices[0] juice.";
 $string = "This is { $great}";
 $string = "This is {$great}";
-// $string = "This square is {$square->width}00 centimeters broad.";
-// $string = "This works: {$arr['key']}";
-// $string = "This works: {$arr[4][3]}";
+$string = "This square is {$square->width}00 centimeters broad.";
+$string = "This works: {$arr['key']}";
+$string = "This works: {$arr[4][3]}";
 $string = "This works: " . $arr['foo'][3];
-// $string = "This works too: {$obj->values[3]->name}";
+$string = "This works too: {$obj->values[3]->name}";
 $string = "This is the value of the var named $name: {\${$name}}";
 $string = "This is the value of the var named by the return value of getName(): {\${getName()}}";
 $string = "This is the value of the var named by the return value of getName(): {\${getName($arg1, $arg2, $arg3)}}";
-// $string = "This is the value of the var named by the return value of \\$object->getName(): {\${$object->getName()}}";
+$string = "This is the value of the var named by the return value of \\$object->getName(): {\${$object->getName()}}";
 $string = "I'd like an {\${beers::softdrink}}\\n";
 $string = "I'd like an {\${beers::$ale}}\\n";
 $test = "\\\\";
@@ -122,13 +125,16 @@ $string = "\\u{9999}";
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 $string = 'this is a simple string';
-// $string = 'You can also have embedded\\n newlines in
-// strings this way as it is
-// okay to do';
+$string = 'You can also have embedded\\n newlines in
+strings this way as it is
+okay to do';
+$string = "You can also have embedded\\n newlines in
+strings this way as it is
+okay to do";
 $string = 'Arnold once said: "I\\'ll be back"';
 $string = 'You deleted C:\\\\*.*?';
 $string = 'You deleted C:\\*.*?';
-// $string = 'This will not expand: \\n a newline';
+$string = 'This will not expand: \\n a newline';
 $string = 'Variables do not $expand $either';
 $string = 'qwe{$a}rty';
 $string = 'qwe\${a}rty';
@@ -140,10 +146,10 @@ $string = "foo\\vbar";
 $string = "foo\\ebar";
 $string = "foo\\fbar";
 $string = "foo\\\\bar";
-// $string = "foo\\\\\\bar";
+$string = "foo\\\\\\bar";
 $string = "foo\\\\\\\\bar";
-// $string = "foo\\$bar";
-// $string = "foo\\\\\\$bar";
+$string = "foo\\$bar";
+$string = "foo\\\\\\$bar";
 $string = "foo\\"bar";
 $string = "foo\\"";
 $string = "foo'bar";
@@ -162,15 +168,15 @@ $string = "encapsed variable {$value}.";
 $string = "encapsed offset {$value[0]}.";
 $string = "encapsed {$method->call()}.";
 $string = "combined \${$encapsed}.";
-// $string = "He drank some $juices[koolaid1] juice.";
+$string = "He drank some {$juices[koolaid1]} juice.";
 $string = "{$people->john} drank some {$juices[0]} juice.";
 $string = "This is { $great}";
 $string = "This is {$great}";
-// $string = "This square is {$square->width}00 centimeters broad.";
-// $string = "This works: {$arr['key']}";
-// $string = "This works: {$arr[4][3]}";
+$string = "This square is {$square->width}00 centimeters broad.";
+$string = "This works: {$arr['key']}";
+$string = "This works: {$arr[4][3]}";
 $string = "This works: " . $arr['foo'][3];
-// $string = "This works too: {$obj->values[3]->name}";
+$string = "This works too: {$obj->values[3]->name}";
 $string = "This is the value of the var named $name: \${$name}";
 $string = "This is the value of the var named by the return value of getName(): \${getName()}";
 $string = "This is the value of the var named by the return value of getName(): \${getName(
@@ -178,7 +184,7 @@ $string = "This is the value of the var named by the return value of getName():
     $arg2,
     $arg3
 )}";
-// $string = "This is the value of the var named by the return value of \\$object->getName(): {\${$object->getName()}}";
+$string = "This is the value of the var named by the return value of \\$object->getName(): \${$object->getName()}";
 $string = "I'd like an \${beers::softdrink}\\n";
 $string = "I'd like an \${beers::$ale}\\n";
 $test = "\\\\";
diff --git a/tests/string/string.php b/tests/string/string.php
index b52be64..c16d7b1 100644
--- a/tests/string/string.php
+++ b/tests/string/string.php
@@ -1,12 +1,15 @@
 <?php
 $string = 'this is a simple string';
-// $string = 'You can also have embedded\n newlines in
-// strings this way as it is
-// okay to do';
+$string = 'You can also have embedded\n newlines in
+strings this way as it is
+okay to do';
+$string = "You can also have embedded\n newlines in
+strings this way as it is
+okay to do";
 $string = 'Arnold once said: "I\'ll be back"';
 $string = 'You deleted C:\\*.*?';
 $string = 'You deleted C:\*.*?';
-// $string = 'This will not expand: \n a newline';
+$string = 'This will not expand: \n a newline';
 $string = 'Variables do not $expand $either';
 $string = 'qwe{$a}rty';
 $string = 'qwe${a}rty';
@@ -18,10 +21,10 @@ $string = "foo\vbar";
 $string = "foo\ebar";
 $string = "foo\fbar";
 $string = "foo\\bar";
-// $string = "foo\\\bar";
+$string = "foo\\\bar";
 $string = "foo\\\\bar";
-// $string = "foo\$bar";
-// $string = "foo\\\$bar";
+$string = "foo\$bar";
+$string = "foo\\\$bar";
 $string = "foo\"bar";
 $string = "foo\"";
 $string = "foo'bar";
@@ -40,19 +43,19 @@ $string = "encapsed variable {$value}.";
 $string = "encapsed offset {$value[0]}.";
 $string = "encapsed {$method->call()}.";
 $string = "combined ${$encapsed}.";
-// $string = "He drank some $juices[koolaid1] juice.";
+$string = "He drank some $juices[koolaid1] juice.";
 $string = "$people->john drank some $juices[0] juice.";
 $string = "This is { $great}";
 $string = "This is {$great}";
-// $string = "This square is {$square->width}00 centimeters broad.";
-// $string = "This works: {$arr['key']}";
-// $string = "This works: {$arr[4][3]}";
+$string = "This square is {$square->width}00 centimeters broad.";
+$string = "This works: {$arr['key']}";
+$string = "This works: {$arr[4][3]}";
 $string = "This works: " . $arr['foo'][3];
-// $string = "This works too: {$obj->values[3]->name}";
+$string = "This works too: {$obj->values[3]->name}";
 $string = "This is the value of the var named $name: {${$name}}";
 $string = "This is the value of the var named by the return value of getName(): {${getName()}}";
 $string = "This is the value of the var named by the return value of getName(): {${getName($arg1, $arg2, $arg3)}}";
-// $string = "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";
+$string = "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";
 $string = "I'd like an {${beers::softdrink}}\n";
 $string = "I'd like an {${beers::$ale}}\n";
 $test = "\\";