Fix pretty printing of -(-$x) and +(+$x)

Fixes #459.
This commit is contained in:
Nikita Popov 2018-01-25 22:16:11 +01:00
parent 579f4ce846
commit 94c715d97e
3 changed files with 16 additions and 1 deletions

View File

@ -1,7 +1,9 @@
Version 3.1.4-dev
-----------------
Nothing yet.
### Fixed
* Fixed pretty printing of `-(-$x)` and `+(+$x)`. (#459)
Version 3.1.3 (2017-12-26)
--------------------------

View File

@ -374,10 +374,18 @@ class Standard extends PrettyPrinterAbstract
}
protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) {
if ($node->expr instanceof Expr\UnaryMinus) {
// Enforce -(-$expr) instead of --$expr
return '-(' . $this->p($node->expr) . ')';
}
return $this->pPrefixOp('Expr_UnaryMinus', '-', $node->expr);
}
protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) {
if ($node->expr instanceof Expr\UnaryPlus) {
// Enforce +(+$expr) instead of ++$expr
return '+(' . $this->p($node->expr) . ')';
}
return $this->pPrefixOp('Expr_UnaryPlus', '+', $node->expr);
}

View File

@ -38,6 +38,9 @@ yield from ($a and yield from $b);
print ($a and print $b);
-(-$a);
+(+$a);
// The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment
// and incdec only work on variables.
!$a = $b;
@ -70,6 +73,8 @@ $a ** $b ** $c;
yield from $a and yield from $b;
yield from ($a and yield from $b);
print ($a and print $b);
-(-$a);
+(+$a);
// The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment
// and incdec only work on variables.
!($a = $b);