Repository: prettier/plugin-php
Pull request #746 (2018-10-08 15:05:39)

feat: better print empty `class`, `trait` and `interface`

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/printer.js b/src/printer.js
index 7e46a67..a9845b4 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1191,9 +1191,7 @@ function printClass(path, options, print) {
   );
 
   const hasEmptyClassBody =
-    node.body &&
-    node.body.length === 0 &&
-    (!node.comments || node.comments.length === 0);
+    node.body && node.body.length === 0 && !hasDanglingComments(node);
   const printedBody = concat([
     "{",
     indent(


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index c77cfde..5534aac 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -504,6 +504,30 @@ class ResponseMethodFailure extends ResponseNotAcceptable implements
 {
 
 }
+
+class MyClass
+{
+}
+
+// Comment
+class MyClass
+{
+
+}
+
+class MyClass
+{
+    // Comment
+}
+
+$var = new class {};
+
+// Comment
+$var = new class {};
+
+$var = new class {
+    // Comment
+};
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 namespace Test\\test\\test;
@@ -588,7 +612,6 @@ class ResponseMethodFailure
         // Another comment
         BarBaz
 {
-
 }
 
 // 424 : Method failure
@@ -601,7 +624,6 @@ class ResponseMethodFailure
         // Another comment
         BarBaz
 {
-
 }
 
 // 424 : Method failure
@@ -614,7 +636,6 @@ class ResponseMethodFailure
         // Another comment
         BarBaz
 {
-
 }
 
 // 424 : Method failure
@@ -624,9 +645,31 @@ class ResponseMethodFailure extends ResponseNotAcceptable implements
     // Another comment
     BarBaz
 {
+}
 
+class MyClass
+{
+}
+
+// Comment
+class MyClass
+{
 }
 
+class MyClass
+{
+    // Comment
+}
+
+$var = new class {};
+
+// Comment
+$var = new class {};
+
+$var = new class {
+    // Comment
+};
+
 `;
 
 exports[`clone.php - php-verify: clone.php 1`] = `
@@ -1915,6 +1958,21 @@ interface IFoo extends
     MyOtherClass,
     // Comment
     OtherClass {}
+
+interface MyInterface
+{
+}
+
+// Comment
+interface MyInterface
+{
+
+}
+
+interface MyInterface
+{
+    // Comment
+}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -1932,7 +1990,6 @@ interface IFoo extends
     // Comment
     MyClass
 {
-
 }
 
 // Comment
@@ -1944,7 +2001,20 @@ interface IFoo extends
     // Comment
     OtherClass
 {
+}
 
+interface MyInterface
+{
+}
+
+// Comment
+interface MyInterface
+{
+}
+
+interface MyInterface
+{
+    // Comment
 }
 
 `;
@@ -2408,6 +2478,21 @@ class ImplementingClass {
         /* Comment */ C/* Comment */::/* Comment */ testFunction /* Comment */ as  /* Comment */ private /* Comment */ testVisibility /* Comment */;
     }
 }
+
+trait MyTrait
+{
+}
+
+// Comment
+trait MyTrait
+{
+
+}
+
+trait MyTrait
+{
+    // Comment
+}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
@@ -2426,7 +2511,6 @@ trait tFoo
     /* Comment */
     extends ReflectionMethod
 {
-
 }
 
 // Comment
@@ -2434,7 +2518,6 @@ trait tFoo
     // Comment
     extends ReflectionMethod
 {
-
 }
 
 class ImplementingClass
@@ -2450,6 +2533,20 @@ class ImplementingClass
     }
 }
 
+trait MyTrait
+{
+}
+
+// Comment
+trait MyTrait
+{
+}
+
+trait MyTrait
+{
+    // Comment
+}
+
 `;
 
 exports[`try.php - php-verify: try.php 1`] = `
diff --git a/tests/comments/class.php b/tests/comments/class.php
index 210e6f2..a82ac7d 100644
--- a/tests/comments/class.php
+++ b/tests/comments/class.php
@@ -110,3 +110,27 @@ class ResponseMethodFailure extends ResponseNotAcceptable implements
 {
 
 }
+
+class MyClass
+{
+}
+
+// Comment
+class MyClass
+{
+
+}
+
+class MyClass
+{
+    // Comment
+}
+
+$var = new class {};
+
+// Comment
+$var = new class {};
+
+$var = new class {
+    // Comment
+};
diff --git a/tests/comments/interface.php b/tests/comments/interface.php
index ed54d52..cae4e4f 100644
--- a/tests/comments/interface.php
+++ b/tests/comments/interface.php
@@ -18,3 +18,18 @@ interface IFoo extends
     MyOtherClass,
     // Comment
     OtherClass {}
+
+interface MyInterface
+{
+}
+
+// Comment
+interface MyInterface
+{
+
+}
+
+interface MyInterface
+{
+    // Comment
+}
diff --git a/tests/comments/trait.php b/tests/comments/trait.php
index f37d322..b032cb7 100644
--- a/tests/comments/trait.php
+++ b/tests/comments/trait.php
@@ -23,3 +23,18 @@ class ImplementingClass {
         /* Comment */ C/* Comment */::/* Comment */ testFunction /* Comment */ as  /* Comment */ private /* Comment */ testVisibility /* Comment */;
     }
 }
+
+trait MyTrait
+{
+}
+
+// Comment
+trait MyTrait
+{
+
+}
+
+trait MyTrait
+{
+    // Comment
+}
diff --git a/tests/trailing_whitespace/__snapshots__/jsfmt.spec.js.snap b/tests/trailing_whitespace/__snapshots__/jsfmt.spec.js.snap
index 2a8fb51..685c407 100644
--- a/tests/trailing_whitespace/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/trailing_whitespace/__snapshots__/jsfmt.spec.js.snap
@@ -33,7 +33,6 @@ class test{
 <?php
 class test
 {
-
 } // some comment
 ?>