Fix formatting preservation for match

This commit is contained in:
Nikita Popov 2022-09-03 21:02:34 +02:00
parent c585a2d766
commit a772853307
2 changed files with 9 additions and 5 deletions

View File

@ -826,8 +826,10 @@ abstract class PrettyPrinterAbstract {
// We go multiline if the original code was multiline, // We go multiline if the original code was multiline,
// or if it's an array item with a comment above it. // or if it's an array item with a comment above it.
// Match always uses multiline formatting.
if ($insertStr === ', ' && if ($insertStr === ', ' &&
($this->isMultiline($origNodes) || $arrItem->getComments()) ($this->isMultiline($origNodes) || $arrItem->getComments() ||
$parentNodeClass === Expr\Match_::class)
) { ) {
$insertStr = ','; $insertStr = ',';
$insertNewline = true; $insertNewline = true;
@ -1378,7 +1380,7 @@ abstract class PrettyPrinterAbstract {
Stmt\Global_::class . '->vars' => ', ', Stmt\Global_::class . '->vars' => ', ',
Stmt\GroupUse::class . '->uses' => ', ', Stmt\GroupUse::class . '->uses' => ', ',
Stmt\Interface_::class . '->extends' => ', ', Stmt\Interface_::class . '->extends' => ', ',
//Expr\Match_::class . '->arms' => ', ', Expr\Match_::class . '->arms' => ', ',
Stmt\Property::class . '->props' => ', ', Stmt\Property::class . '->props' => ', ',
Stmt\StaticVar::class . '->vars' => ', ', Stmt\StaticVar::class . '->vars' => ', ',
Stmt\TraitUse::class . '->traits' => ', ', Stmt\TraitUse::class . '->traits' => ', ',

View File

@ -1,7 +1,8 @@
Matches Matches
----- -----
<?php <?php
$value = match (1) { $value = match (1)
{
1 1
=> =>
'one' 'one'
@ -10,11 +11,12 @@ $value = match (1) {
$stmts[0]->expr->expr->arms[] = new Node\MatchArm(null, new Scalar\String_('two')); $stmts[0]->expr->expr->arms[] = new Node\MatchArm(null, new Scalar\String_('two'));
----- -----
<?php <?php
$value = match (1) { $value = match (1)
{
1 1
=> =>
'one', 'one',
default => 'two', default => 'two'
}; };
----- -----
<?php <?php