147 Commits

Author SHA1 Message Date
Nikita Popov
d43edfbb31 Support CRLF newlines in pretty printer
Can be enabled using the "newlines" option.
2023-05-21 20:56:56 +02:00
Nikita Popov
74caed6446 Fix labelCharMap for DEL character
This map is supposed to have string keys, not integer keys.
2023-05-20 21:55:53 +02:00
Nikita Popov
91da19147b Support readonly anonymous classes 2023-05-20 21:17:44 +02:00
Nikita Popov
5c267f55c9 Add support for typed constants
RFC: https://wiki.php.net/rfc/typed_class_constants
2023-05-20 21:02:03 +02:00
Nikita Popov
bb4263ea1a Treat del as label character depending on PHP version
In formatting-preserving pretty printing, treat DEL as a label
character based on the target PHP version (the default of 7.1
implying it isn't one).

This avoids failure to round-trip an unchanged input.
2023-03-05 17:03:00 +01:00
Nikita Popov
4ce0de2d12 Use PHP 7.1 as default pretty printer target 2023-03-05 16:56:50 +01:00
Nikita Popov
a3bc900a41 Don't ltrim when preserving formatting
We shouldn't ltrim when printing a whole file, that way we will
not just fail to preserve formatting, but actually change semantics
by dropping meaningful whitespace.
2023-03-05 16:42:30 +01:00
Anthony Ferrara
64484a4979 Add PrettyPrinter interface
Closes #423.
2023-03-04 21:56:58 +01:00
Nikita Popov
a0ed229b31 Revert "Rename PrettyPrinterAbstract to PrettyPrinter"
This reverts commit 2217f14d6e039f1c0572329e6fcc99f6c17178a3.
2023-03-04 21:54:56 +01:00
Nikita Popov
2217f14d6e Rename PrettyPrinterAbstract to PrettyPrinter 2023-03-01 21:25:02 +01:00
Nikita Popov
fcd5934dae Prevent merging of consecutive -/+ more thoroughly
The unary -/+ or --/++ might not be the direct child. Instead
determine this by actually printing the operand and checking
whether it starts with -/+.
2023-02-27 19:07:44 +01:00
Nikita Popov
cc34c2450c Fix logic for new operand parentheses requirement
We need to perform this check recursively.
2023-02-27 19:00:33 +01:00
Nikita Popov
cb60eda774 Support yield precedence
Since PHP 7.0 yield is a proper expression, so print it with
proper precedence. If the pretty printer is configured for
older version (non-default), then always print parentheses.

There is an interesting interaction here, in that => is resolved
in favor of yield if the yield occurs as part of an array (or
outer yield). This kind of bypasses the entire precedence hierarchy
and *only* affects yield expressions. For the sake of simplicity
this is modeled via normal LHS precedence, because this will only
add unnecessary parentheses to a handful of low precedence unary
operators. If desired, a special marker for this purpose could be
added though.
2023-02-26 22:58:53 +01:00
Nikita Popov
1cb460ae38 Handle throw precedence
Now that this is an expression, we also need to handle precedence.
2023-02-26 18:33:24 +01:00
Nikita Popov
3bd38c5b2c Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.

For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.

To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.

This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-26 18:26:20 +01:00
Nikita Popov
e9416a0eae Support fixup for dynamic class const name
This is new in PHP 8.3.
2023-02-26 16:00:35 +01:00
Nikita Popov
7b4a8c1ebd Properly handle static deref LHS
The rules for static and array/object deref are slightly different:
The former does not allow constants.
2023-02-26 15:57:37 +01:00
Nikita Popov
6a88bdb05a Support new variables in fixup 2023-02-26 15:17:07 +01:00
Nikita Popov
1eb6b5653e Properly handle new/instanceof operand restrictions
Fixes #912.
2023-02-26 15:11:36 +01:00
Nikita Popov
d24745ddbc Respect precedence during clone pretty printing
Clone is the prefix operator with the highest operator precedence,
but was not treated as an operator at all.
2023-02-26 14:58:17 +01:00
Nikita Popov
b0edd4c411 Bail out on PHP tags in removed code
If dropping a node would drop PHP tags, bail out of formatting
preservation. This will lose formatting, but at least produce
legal code.

Closes GH-884.
2022-09-21 20:42:21 +02:00
Nikita Popov
4bcdf74b8b Add support for perserving formatting for static modifier change
Closes GH-891.
2022-09-21 18:56:58 +02:00
Nikita Popov
f7b448fa15 Bail out on list insertion of non-Node
PhpStan correctly detected that this is not supported. We could
add support for it, but for now just make sure it doesn't crash.
2022-09-17 21:14:31 +02:00
Nikita Popov
f98341f688 Specify more types 2022-09-17 18:48:56 +02:00
Nikita Popov
48f470eac7 Add missing return types 2022-09-11 18:17:05 +02:00
Nikita Popov
9b5a2c8991 Use PHPStan level 5 2022-09-11 12:40:08 +02:00
Nikita Popov
8dfce13d77 Add phpstan baseline 2022-09-11 12:31:50 +02:00
Nikita Popov
44c6a97705 Fix empty list insertion of multiple attributes 2022-09-04 09:25:36 +02:00
Nikita Popov
a772853307 Fix formatting preservation for match 2022-09-03 21:02:34 +02:00
Nikita Popov
c585a2d766 Switch list insertion maps to use class name
Also highlights that the list insertion entry for Expr\Match
was not used.
2022-09-03 20:56:06 +02:00
Nikita Popov
8ed76726aa Switch modifierChangeMap to use class name
For better refactoring support, prefer class name over node type.
2022-09-03 19:12:25 +02:00
Nikita Popov
5f3ad31501 Fix ArrayItem entries in pretty printer maps
The tests were written in such a way that the regression was not
caught.
2022-09-03 19:04:22 +02:00
Nikita Popov
4917c71a91 Rename Stmt\UseUse to UseItem 2022-09-03 18:59:48 +02:00
Nikita Popov
e1345f0c09 Rename Stmt\PropertyProperty to PropertyItem 2022-09-03 18:55:22 +02:00
Nikita Popov
a44faa6328 Rename Scalar\Encapsed to Scalar\InterpolatedString 2022-09-03 15:14:04 +02:00
Nikita Popov
f4ec6a1e53 Rename Scalar\EncapsedStringPart to InterpolatedStringPart
It is no longer an expression node, which unfortunately does
require a more awkward type for the Encaps node.
2022-09-03 13:25:23 +02:00
Nikita Popov
a2608f0b74 Support empty list insertion for attributes 2022-09-03 10:52:01 +02:00
Nikita Popov
dd63ddbc24 Add php-cs-fixer config and reformat
The formatting in this project has become something of a mess,
because it changed over time. Add a CS fixer config and reformat
to the desired style, which is PSR-12, but with sane brace placement.
2022-08-28 22:57:06 +02:00
Nikita Popov
f62b2bfdec Introduce separate Modifiers class
Use PhpParser\Modifiers::PUBLIC instead of
PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC, etc. Old constants
of course remain available.

Fixes #476.
2022-08-28 21:20:26 +02:00
Nikita Popov
646b490735 Don't force newline after doc string when targeting PHP >= 7.3 2022-07-24 22:15:42 +02:00
Nikita Popov
59145a4443 Treat assignments as unary operators
Assignment expressions can be viewed as unary operators where
the whole "$x =" part is the operator.
2022-07-23 21:53:48 +02:00
Nikita Popov
7bf6348240 Remove inc/dec from precedence map
Inc/dec are primitive expressions that only accept a variable
operand, rather than a general expression operand.
2022-07-23 21:48:10 +02:00
Nikita Popov
a73c8ee03b Add a phpVersion option to the pretty printer
This is currently just used to initialize the default for short
array syntax.

The default target version in 7.0, which also means that the
default for short arrays is flipped to on.
2022-07-23 18:23:39 +02:00
MathiasReker
050342b5df Add visibility modifiers to constants
Closes GH-848.
2022-07-23 16:04:21 +02:00
Nikita Popov
b6d11da541 Add space after "use" during empty list insertion as well 2022-06-05 22:59:08 +02:00
Nikita Popov
aff98bbf16 Use PHP 8.0 token representation
Migrate everything to use PhpToken-compatible token representation,
rather than the legacy array/string representation.
2022-06-05 17:44:42 +02:00
Tomas Votruba
7c445bb608
Remove space before return type in insertionMap (#841)
* remove space before colon in PrettyPrinterAbstract

* update tests
2022-06-04 23:23:07 +02:00
Dominik Peters
6c0b63d9af Change print order of modifiers
Print abstract/final before visibility modifiers, in line with
PSR-12.

Closes #826.
2022-06-04 13:19:07 +02:00
Nikita Popov
9c5eb3ccba Add some missing type annotations 2022-06-04 12:48:12 +02:00
Nikita Popov
b5234eacd0 Tweak coding style 2021-09-12 21:54:03 +02:00