Skip to content

Commit

Permalink
Fix formatting preservation for alternative elseif/else syntax
Browse files Browse the repository at this point in the history
Test taken from PR nikic#797.

(cherry picked from commit 9b46dff)
  • Loading branch information
nikic committed Mar 5, 2023
1 parent 570e980 commit cad49f8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
6 changes: 4 additions & 2 deletions grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,8 @@ new_elseif_list:
;

new_elseif:
T_ELSEIF '(' expr ')' ':' inner_statement_list { $$ = Stmt\ElseIf_[$3, $6]; }
T_ELSEIF '(' expr ')' ':' inner_statement_list
{ $$ = Stmt\ElseIf_[$3, $6]; $this->fixupAlternativeElse($$); }
;

else_single:
Expand All @@ -528,7 +529,8 @@ else_single:

new_else_single:
/* empty */ { $$ = null; }
| T_ELSE ':' inner_statement_list { $$ = Stmt\Else_[$3]; }
| T_ELSE ':' inner_statement_list
{ $$ = Stmt\Else_[$3]; $this->fixupAlternativeElse($$); }
;

foreach_variable:
Expand Down
4 changes: 2 additions & 2 deletions lib/PhpParser/Parser/Php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ protected function initReduceCallbacks() {
$this->semStack[$stackPos-(2-1)][] = $this->semStack[$stackPos-(2-2)]; $this->semValue = $this->semStack[$stackPos-(2-1)];
},
266 => function ($stackPos) {
$this->semValue = new Stmt\ElseIf_($this->semStack[$stackPos-(6-3)], $this->semStack[$stackPos-(6-6)], $this->startAttributeStack[$stackPos-(6-1)] + $this->endAttributes);
$this->semValue = new Stmt\ElseIf_($this->semStack[$stackPos-(6-3)], $this->semStack[$stackPos-(6-6)], $this->startAttributeStack[$stackPos-(6-1)] + $this->endAttributes); $this->fixupAlternativeElse($this->semValue);
},
267 => function ($stackPos) {
$this->semValue = null;
Expand All @@ -1838,7 +1838,7 @@ protected function initReduceCallbacks() {
$this->semValue = null;
},
270 => function ($stackPos) {
$this->semValue = new Stmt\Else_($this->semStack[$stackPos-(3-3)], $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes);
$this->semValue = new Stmt\Else_($this->semStack[$stackPos-(3-3)], $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes); $this->fixupAlternativeElse($this->semValue);
},
271 => function ($stackPos) {
$this->semValue = array($this->semStack[$stackPos-(1-1)], false);
Expand Down
21 changes: 21 additions & 0 deletions lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\UseUse;
Expand Down Expand Up @@ -876,6 +879,24 @@ protected function createCommentNopAttributes(array $comments) {
return $attributes;
}

/** @param ElseIf_|Else_ $node */
protected function fixupAlternativeElse($node) {
// Make sure a trailing nop statement carrying comments is part of the node.
$numStmts = \count($node->stmts);
if ($numStmts !== 0 && $node->stmts[$numStmts - 1] instanceof Nop) {
$nopAttrs = $node->stmts[$numStmts - 1]->getAttributes();
if (isset($nopAttrs['endLine'])) {
$node->setAttribute('endLine', $nopAttrs['endLine']);
}
if (isset($nopAttrs['endFilePos'])) {
$node->setAttribute('endFilePos', $nopAttrs['endFilePos']);
}
if (isset($nopAttrs['endTokenPos'])) {
$node->setAttribute('endTokenPos', $nopAttrs['endTokenPos']);
}
}
}

protected function checkClassModifier($a, $b, $modifierPos) {
try {
Class_::verifyClassModifier($a, $b);
Expand Down
2 changes: 1 addition & 1 deletion test/code/formatPreservation/comments.test
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ class Test {
public function test() {
// some code
}
}
}
26 changes: 25 additions & 1 deletion test/code/prettyPrinter/comments.test
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,28 @@ function test()
function test()
{
// empty
}
}
-----
<?php

function noDuplicateComment()
{
if (true):
// TEST 1
elseif (true):
// TEST 2
else:
// TEST 3
endif;
}
-----
function noDuplicateComment()
{
if (true) {
// TEST 1
} elseif (true) {
// TEST 2
} else {
// TEST 3
}
}

0 comments on commit cad49f8

Please sign in to comment.