Skip to content

Commit 3cbf52d

Browse files
feat: support new w/o parens in php 8.4
1 parent 96d2757 commit 3cbf52d

File tree

5 files changed

+104
-31
lines changed

5 files changed

+104
-31
lines changed

src/needs-parens.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.mjs";
1+
import {
2+
getPrecedence,
3+
shouldFlatten,
4+
isBitwiseOperator,
5+
isMinVersion,
6+
} from "./util.mjs";
27

38
function needsParens(path, options) {
49
const { parent } = path;
@@ -128,13 +133,16 @@ function needsParens(path, options) {
128133
}
129134
case "clone":
130135
case "new": {
136+
const requiresParens =
137+
node.kind === "clone" ||
138+
(node.kind === "new" && !isMinVersion(options.phpVersion, "8.4"));
131139
switch (parent.kind) {
132140
case "propertylookup":
133141
case "nullsafepropertylookup":
134142
case "staticlookup":
135143
case "offsetlookup":
136144
case "call":
137-
return key === "what";
145+
return key === "what" && requiresParens;
138146
default:
139147
return false;
140148
}

src/parser.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import engine from "php-parser";
2+
import options from "./options.mjs";
23

34
function parse(text, opts) {
45
const inMarkdown = opts && opts.parentParser === "markdown";
@@ -10,10 +11,15 @@ function parse(text, opts) {
1011
// Todo https://github.com/glayzzle/php-parser/issues/170
1112
text = text.replace(/\?>\n<\?/g, "?>\n___PSEUDO_INLINE_PLACEHOLDER___<?");
1213

14+
const latestSupportedPhpVersion = Math.max(
15+
...options.phpVersion.choices.map((c) => parseFloat(c.value))
16+
);
17+
1318
// initialize a new parser instance
1419
const parser = new engine({
1520
parser: {
1621
extractDoc: true,
22+
version: `${latestSupportedPhpVersion}`,
1723
},
1824
ast: {
1925
withPositions: true,

tests/new/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,12 @@ abstract class A
654654
}
655655
}
656656
657-
$class = (new Foo())->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
658-
$class = (new Foo([
657+
$class = new Foo()->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
658+
$class = new Foo([
659659
"VeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" =>
660660
"VeryVeryVeryVeryVeryVeryVeryVeryVeryLongValue",
661-
]))->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
662-
$class = (new PendingDispatch(new $this->class(...func_get_args())))->chain(
661+
])->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
662+
$class = new PendingDispatch(new $this->class(...func_get_args()))->chain(
663663
$this->chain,
664664
);
665665
$dumper = in_array(PHP_SAPI, ["cli", "phpdbg"])
@@ -676,7 +676,7 @@ $class = new static(
676676
$response = new \\Illuminate\\Http\\JsonResponse(
677677
new JsonResponseTestJsonSerializeObject(),
678678
);
679-
$result = (new Pipeline(new \\Illuminate\\Container\\Container()))
679+
$result = new Pipeline(new \\Illuminate\\Container\\Container())
680680
->send("foo")
681681
->through([new PipelineTestPipeOne()])
682682
->then(function ($piped) {

tests/parens/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,11 +4865,11 @@ $var = $var[0][1]::foo();
48654865
$var = $var[0]->foo()->baz;
48664866
$var = $var[0]->foo()->baz;
48674867
4868-
$var = (new Foo())->bar;
4869-
$var = (new Foo())::bar;
4870-
$var = (new Foo())->bar();
4871-
$var = (new Foo())::bar();
4872-
$var = (new Foo())[1];
4868+
$var = new Foo()->bar;
4869+
$var = new Foo()::bar;
4870+
$var = new Foo()->bar();
4871+
$var = new Foo()::bar();
4872+
$var = new Foo()[1];
48734873
48744874
$var = $var->bar()();
48754875
$var = $var->bar()();
@@ -4979,6 +4979,9 @@ printWidth: 80
49794979
$var = new Foo();
49804980
$var = (new Foo());
49814981
$var = (new Foo())->c();
4982+
new Foo->prop;
4983+
new Foo->method();
4984+
new Foo->$var;
49824985
$var = (new class {
49834986
public function log($msg)
49844987
{
@@ -4994,9 +4997,17 @@ $var = ((((new foo())->bar())->foo())[0])[1];
49944997
$var = (((new foo())->bar())->foo())->baz();
49954998
$var = (new $foo())->bar;
49964999
$var = (new $bar->y)->x;
5000+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
5001+
$asdf =
5002+
new SortOfLongClassName()->withALongMethodName()
5003+
->andAnother()->toPushItPast80Chars();
5004+
49975005
$var = (new foo)[0];
49985006
$var = (new foo)[0]['string'];
49995007
5008+
$var = (new Foo)::foo;
5009+
$var = (new Foo)::$foo;
5010+
50005011
$var = new $a->b;
50015012
$var = new $a->b();
50025013
$var = (new $a)->b();
@@ -5028,6 +5039,9 @@ new Translator(
50285039
$var = new Foo();
50295040
$var = new Foo();
50305041
$var = (new Foo())->c();
5042+
(new Foo())->prop;
5043+
(new Foo())->method();
5044+
(new Foo())->$var;
50315045
$var = new class {
50325046
public function log($msg)
50335047
{
@@ -5043,8 +5057,18 @@ $var = (new foo())->bar()->foo()[0][1];
50435057
$var = (new foo())->bar()->foo()->baz();
50445058
$var = (new $foo())->bar;
50455059
$var = (new $bar->y())->x;
5060+
(new SortOfLongClassName())
5061+
->withALongMethodName()
5062+
->andAnother()
5063+
->toPushItPast80Chars();
5064+
$asdf = (new SortOfLongClassName())
5065+
->withALongMethodName()
5066+
->andAnother()
5067+
->toPushItPast80Chars();
50465068
$var = (new foo())[0];
50475069
$var = (new foo())[0]["string"];
5070+
$var = (new Foo())::foo;
5071+
$var = (new Foo())::$foo;
50485072
$var = new $a->b();
50495073
$var = new $a->b();
50505074
$var = (new $a())->b();
@@ -5098,6 +5122,9 @@ printWidth: 80
50985122
$var = new Foo();
50995123
$var = (new Foo());
51005124
$var = (new Foo())->c();
5125+
new Foo->prop;
5126+
new Foo->method();
5127+
new Foo->$var;
51015128
$var = (new class {
51025129
public function log($msg)
51035130
{
@@ -5113,9 +5140,17 @@ $var = ((((new foo())->bar())->foo())[0])[1];
51135140
$var = (((new foo())->bar())->foo())->baz();
51145141
$var = (new $foo())->bar;
51155142
$var = (new $bar->y)->x;
5143+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
5144+
$asdf =
5145+
new SortOfLongClassName()->withALongMethodName()
5146+
->andAnother()->toPushItPast80Chars();
5147+
51165148
$var = (new foo)[0];
51175149
$var = (new foo)[0]['string'];
51185150
5151+
$var = (new Foo)::foo;
5152+
$var = (new Foo)::$foo;
5153+
51195154
$var = new $a->b;
51205155
$var = new $a->b();
51215156
$var = (new $a)->b();
@@ -5146,33 +5181,46 @@ new Translator(
51465181
<?php
51475182
$var = new Foo();
51485183
$var = new Foo();
5149-
$var = (new Foo())->c();
5184+
$var = new Foo()->c();
5185+
new Foo()->prop;
5186+
new Foo()->method();
5187+
new Foo()->$var;
51505188
$var = new class {
51515189
public function log($msg)
51525190
{
51535191
echo $msg;
51545192
}
51555193
};
5156-
$var = (new foo())->bar();
5157-
$var = (new foo())->bar()->foo();
5158-
$var = (new foo())->bar()->foo();
5159-
$var = (new foo())->bar()->foo();
5160-
$var = (new foo())->bar()->foo()[0];
5161-
$var = (new foo())->bar()->foo()[0][1];
5162-
$var = (new foo())->bar()->foo()->baz();
5163-
$var = (new $foo())->bar;
5164-
$var = (new $bar->y())->x;
5165-
$var = (new foo())[0];
5166-
$var = (new foo())[0]["string"];
5194+
$var = new foo()->bar();
5195+
$var = new foo()->bar()->foo();
5196+
$var = new foo()->bar()->foo();
5197+
$var = new foo()->bar()->foo();
5198+
$var = new foo()->bar()->foo()[0];
5199+
$var = new foo()->bar()->foo()[0][1];
5200+
$var = new foo()->bar()->foo()->baz();
5201+
$var = new $foo()->bar;
5202+
$var = new $bar->y()->x;
5203+
new SortOfLongClassName()
5204+
->withALongMethodName()
5205+
->andAnother()
5206+
->toPushItPast80Chars();
5207+
$asdf = new SortOfLongClassName()
5208+
->withALongMethodName()
5209+
->andAnother()
5210+
->toPushItPast80Chars();
5211+
$var = new foo()[0];
5212+
$var = new foo()[0]["string"];
5213+
$var = new Foo()::foo;
5214+
$var = new Foo()::$foo;
51675215
$var = new $a->b();
51685216
$var = new $a->b();
5169-
$var = (new $a())->b();
5170-
$var = (new $a())->b();
5171-
(new class {})->foo;
5172-
(new class {})->foo();
5173-
(new class {})();
5174-
(new class {})["foo"];
5175-
$var = (new class {})->foo;
5217+
$var = new $a()->b();
5218+
$var = new $a()->b();
5219+
new class {}->foo;
5220+
new class {}->foo();
5221+
new class {}();
5222+
new class {}["foo"];
5223+
$var = new class {}->foo;
51765224
51775225
51785226
================================================================================

tests/parens/new.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
$var = new Foo();
3131
$var = (new Foo());
3232
$var = (new Foo())->c();
33+
new Foo->prop;
34+
new Foo->method();
35+
new Foo->$var;
3336
$var = (new class {
3437
public function log($msg)
3538
{
@@ -45,9 +48,17 @@ public function log($msg)
4548
$var = (((new foo())->bar())->foo())->baz();
4649
$var = (new $foo())->bar;
4750
$var = (new $bar->y)->x;
51+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
52+
$asdf =
53+
new SortOfLongClassName()->withALongMethodName()
54+
->andAnother()->toPushItPast80Chars();
55+
4856
$var = (new foo)[0];
4957
$var = (new foo)[0]['string'];
5058

59+
$var = (new Foo)::foo;
60+
$var = (new Foo)::$foo;
61+
5162
$var = new $a->b;
5263
$var = new $a->b();
5364
$var = (new $a)->b();

0 commit comments

Comments
 (0)