fix: avoid adding parens to static property in new expression#2453
fix: avoid adding parens to static property in new expression#2453sorafujitani wants to merge 2 commits intoprettier:mainfrom
Conversation
|
I took a closer look, because the need for new Foo::$bar->baz();and new Foo::$bar()->baz();are parsed like this: {
"kind": "expressionstatement",
"expression": {
"kind": "call",
"what": {
"kind": "propertylookup",
"what": {
"kind": "new",
"what": {
"kind": "staticlookup",
"what": {
"kind": "name",
"name": "Foo",
"resolution": "uqn"
},
"offset": {
"kind": "variable",
"name": "bar",
"curly": false
}
},
"arguments": []
},
"offset": {
"kind": "identifier",
"name": "baz"
}
},
"arguments": []
}
}While nikic/PHP-Parser parses new Foo::$bar->baz();like this: and new Foo::$bar()->baz();like this: So I think this needs to be fixed in the parser, which might even include a bit of a refactor since the general AST structure differes quite a bit between the two parsers (?). |
|
Indeed, the parser itself is incorrect in its handling of the new operator. It might be more illustrative to use parentheses like this: The above is identical semantically to |
|
Thanks for clarifying @MasonD! I created an issue over at glayzzle/php-parser#1177. |
Summary
Fixes #2441
When formatting code like
new Yii::$app->class([]), the formatter was incorrectly adding parentheses to producenew Yii::$app()->class([]). This changes the semantics from accessing a static property (Yii::$app) to calling a static method (Yii::$app()).Changes
printer.mjsto skip adding parentheses fornewexpressions when:newis inside a member chain (parent is a lookup node or call)staticlookup(e.g.,Yii::$app)Test plan
tests/parens/new.phpfor Issue Bug: Prettier incorrectly adds parentheses when using new with a static property #2441