diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index ae5da868..97211550 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -920,7 +920,7 @@ abstract class PrettyPrinterAbstract { $result .= $this->p($delayedAddNode, true); $first = false; } - $result .= $extraRight; + $result .= $extraRight === "\n" ? $this->nl : $extraRight; } return $result; @@ -1448,6 +1448,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; ----- 42; +new #[A] class {}; +#[A] function() {}; +#[A] fn() + => 42; -----