Support empty list insertion for attributes

This commit is contained in:
Nikita Popov 2022-09-03 10:52:01 +02:00
parent 9dca6f1d37
commit a2608f0b74
3 changed files with 28 additions and 26 deletions

View File

@ -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

View File

@ -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 {};
class X {};

View File

@ -107,4 +107,4 @@ Foo
new class
($a, $b)
extends Foo {};
extends Foo {};