1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

FPPP: Fix remove + add at start of list

This commit is contained in:
Nikita Popov 2020-09-22 22:41:02 +02:00
parent 8a97fa157f
commit 88be6127fa
2 changed files with 44 additions and 25 deletions

View File

@ -756,7 +756,7 @@ abstract class PrettyPrinterAbstract
$itemStartPos = $origArrItem->getStartTokenPos(); $itemStartPos = $origArrItem->getStartTokenPos();
$itemEndPos = $origArrItem->getEndTokenPos(); $itemEndPos = $origArrItem->getEndTokenPos();
\assert($itemStartPos >= 0 && $itemEndPos >= 0); \assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos);
$origIndentLevel = $this->indentLevel; $origIndentLevel = $this->indentLevel;
$lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment; $lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
@ -767,16 +767,25 @@ abstract class PrettyPrinterAbstract
$commentStartPos = $origComments ? $origComments[0]->getStartTokenPos() : $itemStartPos; $commentStartPos = $origComments ? $origComments[0]->getStartTokenPos() : $itemStartPos;
\assert($commentStartPos >= 0); \assert($commentStartPos >= 0);
$commentsChanged = $comments !== $origComments; if ($commentStartPos < $pos) {
if ($commentsChanged) { // Comments may be assigned to multiple nodes if they start at the same position.
// Remove old comments // Make sure we don't try to print them multiple times.
$itemStartPos = $commentStartPos; $commentStartPos = $itemStartPos;
}
if ($skipRemovedNode) {
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
// We'd remove the brace of a code block.
// TODO: Preserve formatting.
$this->setIndentLevel($origIndentLevel);
return null;
}
} else {
$result .= $this->origTokens->getTokenCode(
$pos, $commentStartPos, $indentAdjustment);
} }
if (!empty($delayedAdd)) { if (!empty($delayedAdd)) {
$result .= $this->origTokens->getTokenCode(
$pos, $commentStartPos, $indentAdjustment);
/** @var Node $delayedAddNode */ /** @var Node $delayedAddNode */
foreach ($delayedAdd as $delayedAddNode) { foreach ($delayedAdd as $delayedAddNode) {
if ($insertNewline) { if ($insertNewline) {
@ -795,25 +804,16 @@ abstract class PrettyPrinterAbstract
} }
} }
$delayedAdd = [];
}
if ($comments !== $origComments) {
if ($comments) {
$result .= $this->pComments($comments) . $this->nl;
}
} else {
$result .= $this->origTokens->getTokenCode( $result .= $this->origTokens->getTokenCode(
$commentStartPos, $itemStartPos, $indentAdjustment); $commentStartPos, $itemStartPos, $indentAdjustment);
$delayedAdd = [];
} else if (!$skipRemovedNode) {
$result .= $this->origTokens->getTokenCode(
$pos, $itemStartPos, $indentAdjustment);
} else {
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
// We'd remove the brace of a code block.
// TODO: Preserve formatting.
$this->setIndentLevel($origIndentLevel);
return null;
}
}
if ($commentsChanged && $comments) {
// Add new comments
$result .= $this->pComments($comments) . $this->nl;
} }
// If we had to remove anything, we have done so now. // If we had to remove anything, we have done so now.

View File

@ -317,3 +317,22 @@ $stmts[0]->returnType->types[] = new Node\Name('C');
<?php <?php
function test(): A function test(): A
|B|C {} |B|C {}
-----
<?php
function test() {
if ($x) {
$a;
$b;
}
$z;
}
-----
$fnStmts =& $stmts[0]->stmts;
array_splice($fnStmts, 0, 1, $fnStmts[0]->stmts);
-----
<?php
function test() {
$a;
$b;
$z;
}