Repository: prettier/plugin-php
Pull request #460 (2018-07-06 16:16:24)

feat: better print empty declarations


Patch:
diff --git a/src/printer.js b/src/printer.js
index b208d77..61ed450 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1420,15 +1420,28 @@ function printStatement(path, options, print) {
             ])
           )
         : "";
+
+      const hasEmptyBody =
+        ((node.kind === "function" || node.kind === "method") &&
+          node.body &&
+          node.body.children &&
+          node.body.children.length === 0 &&
+          !node.body.comments) ||
+        (isClassLikeNode &&
+          node.body &&
+          node.body.length === 0 &&
+          !node.comments);
+
       const printedBody = bodyContents
         ? concat([
             "{",
-            indent(concat([hardline, bodyContents])),
+            indent(concat([hasEmptyBody ? "" : hardline, bodyContents])),
             comments.printDanglingComments(path, options, true),
             hardline,
             "}"
           ])
         : "";
+
       return concat([
         group(
           concat([
@@ -1922,7 +1935,13 @@ function printStatement(path, options, print) {
           : "",
         node.alias ? concat([" as ", node.alias]) : ""
       ]);
-    case "closure":
+    case "closure": {
+      const hasEmptyBody =
+        node.body &&
+        node.body.children &&
+        node.body.children.length === 0 &&
+        !node.body.comments;
+
       return concat([
         "function ",
         printArgumentsList(path, options, print),
@@ -1935,9 +1954,12 @@ function printStatement(path, options, print) {
             )
           : "",
         " {",
-        indent(concat([hardline, path.call(print, "body")])),
+        indent(
+          concat([hasEmptyBody ? "" : hardline, path.call(print, "body")])
+        ),
         concat([hardline, "}"])
       ]);
+    }
     case "retif": {
       const printedExpr = concat([
         line,


Test patch:
diff --git a/tests/case/__snapshots__/jsfmt.spec.js.snap b/tests/case/__snapshots__/jsfmt.spec.js.snap
index c35374c..d672a67 100644
--- a/tests/case/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/case/__snapshots__/jsfmt.spec.js.snap
@@ -248,7 +248,6 @@ use BarClass as Bar;
 
 interface iTemplate
 {
-
 }
 
 abstract class AbstractClass
diff --git a/tests/class/__snapshots__/jsfmt.spec.js.snap b/tests/class/__snapshots__/jsfmt.spec.js.snap
index a099932..a994f86 100644
--- a/tests/class/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/class/__snapshots__/jsfmt.spec.js.snap
@@ -280,6 +280,10 @@ class test {
         return "The customer for that job, {$job->customer->name} has an error that shows up after the line gets waaaaay toooo long.";
     }
 }
+
+class EmptyClass {}
+
+class EmptyClassWithComments { /* Comment */ }
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 class Foo extends Bar implements Baz, Buzz
@@ -474,7 +478,6 @@ $this->something->method(
 
 class A
 {
-
 }
 
 $someVar = new ReaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalyLongClassName();
@@ -490,11 +493,9 @@ class FooBar
     public $property2;
     public function method()
     {
-
     }
     public function method2()
     {
-
     }
 }
 
@@ -508,40 +509,34 @@ class FooBarFoo
 
 class ClassName extends ParentClass implements InterfaceClass
 {
-
 }
 
 class ClassName extends ParentClass
     implements VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
-
 }
 
 class ClassName extends ParentClass
     implements
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
-
 }
 
 class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     implements InterfaceClass
 {
-
 }
 
 class ClassName
     extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     implements InterfaceClass
 {
-
 }
 
 class ClassName
     extends VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     implements VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
-
 }
 
 class ClassName
@@ -549,7 +544,6 @@ class ClassName
     implements
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
 {
-
 }
 
 class ClassName extends ParentClass
@@ -558,7 +552,6 @@ class ClassName extends ParentClass
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName2,
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName3
 {
-
 }
 
 class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
@@ -567,7 +560,6 @@ class ClassName extends VeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName2,
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName3
 {
-
 }
 
 class ClassName
@@ -577,20 +569,17 @@ class ClassName
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName2,
         VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName3
 {
-
 }
 
 class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongFileName1
     extends ParentClass
     implements InterfaceClass
 {
-
 }
 
 class Custom_Plugin_NotificationPlaceholderSource
     extends Notification_Manager_DefaultPlaceholderSource
 {
-
 }
 
 class field extends \\models\\base
@@ -621,4 +610,13 @@ class test
     }
 }
 
+class EmptyClass
+{
+}
+
+class EmptyClassWithComments
+{
+    /* Comment */
+}
+
 `;
diff --git a/tests/class/class.php b/tests/class/class.php
index 47e091b..285a7cd 100644
--- a/tests/class/class.php
+++ b/tests/class/class.php
@@ -218,3 +218,7 @@ class test {
         return "The customer for that job, {$job->customer->name} has an error that shows up after the line gets waaaaay toooo long.";
     }
 }
+
+class EmptyClass {}
+
+class EmptyClassWithComments { /* Comment */ }
diff --git a/tests/closure/__snapshots__/jsfmt.spec.js.snap b/tests/closure/__snapshots__/jsfmt.spec.js.snap
index 45d353b..4c96514 100644
--- a/tests/closure/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/closure/__snapshots__/jsfmt.spec.js.snap
@@ -62,6 +62,10 @@ $foo->bar(
     },
     $longLongLongLongLongLongLongLongLongLongArg3
 );
+
+$emptyFunc = function(){};
+
+$emptyFuncWithComment = function(){ /* Comment */ };
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 $closureWithArgs = function ($arg1, $arg2) {
@@ -124,4 +128,11 @@ $foo->bar(
     $longLongLongLongLongLongLongLongLongLongArg3
 );
 
+$emptyFunc = function () {
+};
+
+$emptyFuncWithComment = function () {
+    /* Comment */
+};
+
 `;
diff --git a/tests/closure/closure.php b/tests/closure/closure.php
index 87ed72d..1508771 100644
--- a/tests/closure/closure.php
+++ b/tests/closure/closure.php
@@ -59,3 +59,7 @@ $foo->bar(
     },
     $longLongLongLongLongLongLongLongLongLongArg3
 );
+
+$emptyFunc = function(){};
+
+$emptyFuncWithComment = function(){ /* Comment */ };
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 0907b97..03d8e3c 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -815,7 +815,6 @@ class Test
 
     function testFunction($arg1, /* inline */ $arg2)
     {
-
     }
 }
 
@@ -826,7 +825,6 @@ function testFunction(/* inline */ $arg)
 
 function testFunction($arg1, /* inline */ $arg2)
 {
-
 }
 
 `;
@@ -991,15 +989,12 @@ function emptyFn(/*Comment */) {}
 <?php
 function foo(/* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */)
 {
-
 }
 function foo($a): /* 5 */ ?string /* 6 */
 {
-
 }
 function foo(/* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */): /* 5 */ ?string /* 6 */
 {
-
 }
 function emptyFn()
 {
@@ -1252,7 +1247,6 @@ class Foo
     public function foo(
         /* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */
     ): /* 5 */ ?string /* 6 */ {
-
     }
 
     public function emptyMethod()
@@ -1351,12 +1345,10 @@ function foo(
     /* Comment */ array /* Comment */ $b /* Comment */
         = /* Comment */ [] /* Comment */
 ) {
-
 }
 
 function bar(/* Comment */ &$a /* Comment */)
 {
-
 }
 
 `;
diff --git a/tests/functions/__snapshots__/jsfmt.spec.js.snap b/tests/functions/__snapshots__/jsfmt.spec.js.snap
index 3039aaa..8c88c6b 100644
--- a/tests/functions/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/functions/__snapshots__/jsfmt.spec.js.snap
@@ -163,6 +163,9 @@ $shortArgs_longVars = function ($arg) use (
 ) {
     // body
 };
+
+function emptyFunction(){}
+function emptyFunctionWithComment(){ /* Comment */ }
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 function hello()
@@ -249,7 +252,6 @@ function &passByReferenceTest()
 
 function foo(iterable $iterable = [])
 {
-
 }
 
 function bar(): iterable
@@ -346,4 +348,12 @@ $shortArgs_longVars = function ($arg) use (
     // body
 };
 
+function emptyFunction()
+{
+}
+function emptyFunctionWithComment()
+{
+    /* Comment */
+}
+
 `;
diff --git a/tests/functions/functions.php b/tests/functions/functions.php
index 646f54b..488f82d 100644
--- a/tests/functions/functions.php
+++ b/tests/functions/functions.php
@@ -160,3 +160,6 @@ $shortArgs_longVars = function ($arg) use (
 ) {
     // body
 };
+
+function emptyFunction(){}
+function emptyFunctionWithComment(){ /* Comment */ }
diff --git a/tests/interface/__snapshots__/jsfmt.spec.js.snap b/tests/interface/__snapshots__/jsfmt.spec.js.snap
index 134be06..70c47a2 100644
--- a/tests/interface/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/interface/__snapshots__/jsfmt.spec.js.snap
@@ -11,6 +11,10 @@ interface testInterface {
 interface test2 extends testInterface {
   public function other($hi);
 }
+
+interface EmptyInterface {}
+
+interface EmptyInterfaceWithComments { /* Comment */ }
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 interface testInterface
@@ -24,4 +28,13 @@ interface test2 extends testInterface
     public function other($hi);
 }
 
+interface EmptyInterface
+{
+}
+
+interface EmptyInterfaceWithComments
+{
+    /* Comment */
+}
+
 `;
diff --git a/tests/interface/interface.php b/tests/interface/interface.php
index 050eeb0..af0e6eb 100644
--- a/tests/interface/interface.php
+++ b/tests/interface/interface.php
@@ -8,3 +8,7 @@ interface testInterface {
 interface test2 extends testInterface {
   public function other($hi);
 }
+
+interface EmptyInterface {}
+
+interface EmptyInterfaceWithComments { /* Comment */ }
diff --git a/tests/method/__snapshots__/jsfmt.spec.js.snap b/tests/method/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 0000000..caf32e6
--- /dev/null
+++ b/tests/method/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,88 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`method.php 1`] = `
+<?php
+
+class Foo
+{
+    protected $link;
+
+    public function __construct($link)
+    {
+        $this->link = $link;
+    }
+
+    public function publishMethod()
+    {
+        return 'public';
+    }
+
+    private function protectedMethod()
+    {
+        return 'protected';
+    }
+
+    private function privateMethod()
+    {
+        return 'private';
+    }
+
+    public function emptyMethod() {}
+
+    public function emptyMethodWithComment()
+    {
+        // Comment
+    }
+
+    function aMemberFunc() {
+        print 'Inside \`aMemberFunc()\`';
+    }
+
+    function bMemberFunc(){}
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+class Foo
+{
+    protected $link;
+
+    public function __construct($link)
+    {
+        $this->link = $link;
+    }
+
+    public function publishMethod()
+    {
+        return 'public';
+    }
+
+    private function protectedMethod()
+    {
+        return 'protected';
+    }
+
+    private function privateMethod()
+    {
+        return 'private';
+    }
+
+    public function emptyMethod()
+    {
+    }
+
+    public function emptyMethodWithComment()
+    {
+        // Comment
+    }
+
+    function aMemberFunc()
+    {
+        print 'Inside \`aMemberFunc()\`';
+    }
+
+    function bMemberFunc()
+    {
+    }
+}
+
+`;
diff --git a/tests/method/jsfmt.spec.js b/tests/method/jsfmt.spec.js
new file mode 100644
index 0000000..e395434
--- /dev/null
+++ b/tests/method/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, ["php"]);
diff --git a/tests/method/method.php b/tests/method/method.php
new file mode 100644
index 0000000..04a20f0
--- /dev/null
+++ b/tests/method/method.php
@@ -0,0 +1,39 @@
+<?php
+
+class Foo
+{
+    protected $link;
+
+    public function __construct($link)
+    {
+        $this->link = $link;
+    }
+
+    public function publishMethod()
+    {
+        return 'public';
+    }
+
+    private function protectedMethod()
+    {
+        return 'protected';
+    }
+
+    private function privateMethod()
+    {
+        return 'private';
+    }
+
+    public function emptyMethod() {}
+
+    public function emptyMethodWithComment()
+    {
+        // Comment
+    }
+
+    function aMemberFunc() {
+        print 'Inside `aMemberFunc()`';
+    }
+
+    function bMemberFunc(){}
+}
diff --git a/tests/statement/__snapshots__/jsfmt.spec.js.snap b/tests/statement/__snapshots__/jsfmt.spec.js.snap
index 19c8f0a..a4a9e36 100644
--- a/tests/statement/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/statement/__snapshots__/jsfmt.spec.js.snap
@@ -15,7 +15,6 @@ class Test
 {
     public function method()
     {
-
     }
 }
 if (true) {
diff --git a/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap b/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap
index b5493ed..f36dfff 100644
--- a/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/staticlookup/__snapshots__/jsfmt.spec.js.snap
@@ -160,7 +160,6 @@ abstract class Singleton
      */
     final private function __construct()
     {
-
     }
 
     /**
@@ -168,7 +167,6 @@ abstract class Singleton
      */
     final private function __clone()
     {
-
     }
 
     /**
diff --git a/tests/string/__snapshots__/jsfmt.spec.js.snap b/tests/string/__snapshots__/jsfmt.spec.js.snap
index 6766009..0bf73f0 100644
--- a/tests/string/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/string/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,30 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`multiline.php 1`] = `
+<?php
+$string = 'foo
+bar';
+$chart = '
+{
+  "chart": {
+    "caption": "Worlds Top 10 largest companies in 2017"
+  }
+}';
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$string = 'foo
+bar';
+$chart =
+    '
+{
+  "chart": {
+    "caption": "Worlds Top 10 largest companies in 2017"
+  }
+}';
+
+`;
+
 exports[`single.php 1`] = `
 <?php
 'string';
diff --git a/tests/string/multiline.php b/tests/string/multiline.php
new file mode 100644
index 0000000..2db8652
--- /dev/null
+++ b/tests/string/multiline.php
@@ -0,0 +1,10 @@
+<?php
+$string = 'foo
+bar';
+$chart = '
+{
+  "chart": {
+    "caption": "Worlds Top 10 largest companies in 2017"
+  }
+}';
+
diff --git a/tests/traits/__snapshots__/jsfmt.spec.js.snap b/tests/traits/__snapshots__/jsfmt.spec.js.snap
index 009bd20..a9ac61d 100644
--- a/tests/traits/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/traits/__snapshots__/jsfmt.spec.js.snap
@@ -36,6 +36,10 @@ class Partial {
         AbstractTrait::hasItem as has;
     }
 }
+
+trait emptyTrait {}
+
+trait EmptyTraitWithComment { /* Comment */ }
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 trait A
@@ -94,4 +98,13 @@ class Partial
     }
 }
 
+trait emptyTrait
+{
+}
+
+trait EmptyTraitWithComment
+{
+    /* Comment */
+}
+
 `;
diff --git a/tests/traits/traits.php b/tests/traits/traits.php
index 2b5d225..7d59335 100644
--- a/tests/traits/traits.php
+++ b/tests/traits/traits.php
@@ -33,3 +33,7 @@ class Partial {
         AbstractTrait::hasItem as has;
     }
 }
+
+trait emptyTrait {}
+
+trait EmptyTraitWithComment { /* Comment */ }