Skip to content

Commit 8d42d88

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

File tree

5 files changed

+103
-31
lines changed

5 files changed

+103
-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: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ $bar = 'MyClassName';
1616
$foo = new $bar;
1717
$foo = new $bar();
1818
19+
new Foo->prop;
20+
new Foo->method();
21+
new Foo->$var;
22+
23+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
24+
25+
$asdf =
26+
new SortOfLongClassName()->withALongMethodName()
27+
->andAnother()->toPushItPast80Chars();
28+
1929
abstract class A
2030
{
2131
public static function create()
@@ -210,6 +220,20 @@ $bar = "MyClassName";
210220
$foo = new $bar();
211221
$foo = new $bar();
212222
223+
(new Foo())->prop;
224+
(new Foo())->method();
225+
(new Foo())->$var;
226+
227+
(new SortOfLongClassName())
228+
->withALongMethodName()
229+
->andAnother()
230+
->toPushItPast80Chars();
231+
232+
$asdf = (new SortOfLongClassName())
233+
->withALongMethodName()
234+
->andAnother()
235+
->toPushItPast80Chars();
236+
213237
abstract class A
214238
{
215239
public static function create()
@@ -451,6 +475,16 @@ $bar = 'MyClassName';
451475
$foo = new $bar;
452476
$foo = new $bar();
453477
478+
new Foo->prop;
479+
new Foo->method();
480+
new Foo->$var;
481+
482+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
483+
484+
$asdf =
485+
new SortOfLongClassName()->withALongMethodName()
486+
->andAnother()->toPushItPast80Chars();
487+
454488
abstract class A
455489
{
456490
public static function create()
@@ -645,6 +679,20 @@ $bar = "MyClassName";
645679
$foo = new $bar();
646680
$foo = new $bar();
647681
682+
new Foo()->prop;
683+
new Foo()->method();
684+
new Foo()->$var;
685+
686+
new SortOfLongClassName()
687+
->withALongMethodName()
688+
->andAnother()
689+
->toPushItPast80Chars();
690+
691+
$asdf = new SortOfLongClassName()
692+
->withALongMethodName()
693+
->andAnother()
694+
->toPushItPast80Chars();
695+
648696
abstract class A
649697
{
650698
public static function create()
@@ -654,12 +702,12 @@ abstract class A
654702
}
655703
}
656704
657-
$class = (new Foo())->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
658-
$class = (new Foo([
705+
$class = new Foo()->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
706+
$class = new Foo([
659707
"VeryVeryVeryVeryVeryVeryVeryVeryVeryLongKey" =>
660708
"VeryVeryVeryVeryVeryVeryVeryVeryVeryLongValue",
661-
]))->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
662-
$class = (new PendingDispatch(new $this->class(...func_get_args())))->chain(
709+
])->veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethod();
710+
$class = new PendingDispatch(new $this->class(...func_get_args()))->chain(
663711
$this->chain,
664712
);
665713
$dumper = in_array(PHP_SAPI, ["cli", "phpdbg"])
@@ -676,7 +724,7 @@ $class = new static(
676724
$response = new \\Illuminate\\Http\\JsonResponse(
677725
new JsonResponseTestJsonSerializeObject(),
678726
);
679-
$result = (new Pipeline(new \\Illuminate\\Container\\Container()))
727+
$result = new Pipeline(new \\Illuminate\\Container\\Container())
680728
->send("foo")
681729
->through([new PipelineTestPipeOne()])
682730
->then(function ($piped) {

tests/new/new.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
$foo = new $bar;
99
$foo = new $bar();
1010

11+
new Foo->prop;
12+
new Foo->method();
13+
new Foo->$var;
14+
15+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
16+
17+
$asdf =
18+
new SortOfLongClassName()->withALongMethodName()
19+
->andAnother()->toPushItPast80Chars();
20+
1121
abstract class A
1222
{
1323
public static function create()

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

Lines changed: 24 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()();
@@ -5146,33 +5146,33 @@ new Translator(
51465146
<?php
51475147
$var = new Foo();
51485148
$var = new Foo();
5149-
$var = (new Foo())->c();
5149+
$var = new Foo()->c();
51505150
$var = new class {
51515151
public function log($msg)
51525152
{
51535153
echo $msg;
51545154
}
51555155
};
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"];
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"];
51675167
$var = new $a->b();
51685168
$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;
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;
51765176
51775177
51785178
================================================================================

0 commit comments

Comments
 (0)