Skip to content

Commit

Permalink
Support empty list insertion for attributes
Browse files Browse the repository at this point in the history
(cherry picked from commit a2608f0)
  • Loading branch information
nikic committed Sep 3, 2022
1 parent ff24d1d commit b30e7e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
12 changes: 11 additions & 1 deletion lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ protected function pArray(
$result .= $this->p($delayedAddNode, true);
$first = false;
}
$result .= $extraRight;
$result .= $extraRight === "\n" ? $this->nl : $extraRight;
}

return $result;
Expand Down Expand Up @@ -1454,6 +1454,16 @@ protected function initializeEmptyListInsertionMap() {
'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
Expand Down
40 changes: 16 additions & 24 deletions test/code/formatPreservation/attributes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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'), []),
]);
Expand All @@ -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

Expand All @@ -176,4 +168,4 @@ class X {};
B,
C,
]
class X {};
class X {};
2 changes: 1 addition & 1 deletion test/code/formatPreservation/emptyListInsertion.test
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ Foo

new class
($a, $b)
extends Foo {};
extends Foo {};

0 comments on commit b30e7e7

Please sign in to comment.