From b30e7e73d57876e842335f9cb96facb6444374e1 Mon Sep 17 00:00:00 2001
From: Nikita Popov <nikita.ppv@gmail.com>
Date: Sat, 3 Sep 2022 10:52:01 +0200
Subject: [PATCH] Support empty list insertion for attributes

(cherry picked from commit a2608f0b742b711d24de914d41c0162e37494ea6)
---
 lib/PhpParser/PrettyPrinterAbstract.php       | 12 +++++-
 test/code/formatPreservation/attributes.test  | 40 ++++++++-----------
 .../emptyListInsertion.test                   |  2 +-
 3 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php
index 2c7fc307..003e86c7 100644
--- a/lib/PhpParser/PrettyPrinterAbstract.php
+++ b/lib/PhpParser/PrettyPrinterAbstract.php
@@ -927,7 +927,7 @@ abstract class PrettyPrinterAbstract
                 $result .= $this->p($delayedAddNode, true);
                 $first = false;
             }
-            $result .= $extraRight;
+            $result .= $extraRight === "\n" ? $this->nl : $extraRight;
         }
 
         return $result;
@@ -1454,6 +1454,16 @@ abstract class PrettyPrinterAbstract
             'Stmt_ClassMethod->params' => ['(', '', ''],
             'Stmt_Interface->extends' => [null, ' extends ', ''],
             'Stmt_Function->params' => ['(', '', ''],
+            'Stmt_Interface->attrGroups' => [null, '', "\n"],
+            'Stmt_Class->attrGroups' => [null, '', "\n"],
+            'Stmt_ClassConst->attrGroups' => [null, '', "\n"],
+            'Stmt_ClassMethod->attrGroups' => [null, '', "\n"],
+            'Stmt_Function->attrGroups' => [null, '', "\n"],
+            'Stmt_Property->attrGroups' => [null, '', "\n"],
+            'Stmt_Trait->attrGroups' => [null, '', "\n"],
+            'Expr_ArrowFunction->attrGroups' => [null, '', ' '],
+            'Expr_Closure->attrGroups' => [null, '', ' '],
+            'Expr_PrintableNewAnonClass->attrGroups' => [\T_NEW, ' ', ''],
 
             /* These cannot be empty to start with:
              * Expr_Isset->vars
diff --git a/test/code/formatPreservation/attributes.test b/test/code/formatPreservation/attributes.test
index 0b87c033..bc448e7d 100644
--- a/test/code/formatPreservation/attributes.test
+++ b/test/code/formatPreservation/attributes.test
@@ -102,7 +102,6 @@ function() {};
 fn()
     => 42;
 -----
-// TODO: Currently we lose formatting for this case.
 $attrGroup = new Node\AttributeGroup([
     new Node\Attribute(new Node\Name('A'), []),
 ]);
@@ -119,39 +118,32 @@ $stmts[6]->expr->attrGroups[] = $attrGroup;
 -----
 <?php
 #[A]
-class X
-{
+class X {
     #[A]
-    public function m()
-    {
-    }
+    public function m() {}
+
     #[A]
-    public $prop;
+    public
+        $prop;
+
     #[A]
-    const X = 42;
+    const
+        X = 42;
 }
 
 #[A]
-trait X
-{
-}
+trait X {}
 
 #[A]
-interface X
-{
-}
+interface X {}
 
 #[A]
-function f()
-{
-}
+function f() {}
 
-new #[A] class
-{
-};
-#[A] function () {
-};
-#[A] fn() => 42;
+new #[A] class {};
+#[A] function() {};
+#[A] fn()
+    => 42;
 -----
 <?php
 
@@ -176,4 +168,4 @@ class X {};
     B,
     C,
 ]
-class X {};
\ No newline at end of file
+class X {};
diff --git a/test/code/formatPreservation/emptyListInsertion.test b/test/code/formatPreservation/emptyListInsertion.test
index ad3137bf..e98e51ef 100644
--- a/test/code/formatPreservation/emptyListInsertion.test
+++ b/test/code/formatPreservation/emptyListInsertion.test
@@ -107,4 +107,4 @@ Foo
 
 new class
 ($a, $b)
-extends Foo {};
\ No newline at end of file
+extends Foo {};