Skip to content

Commit e43159c

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

File tree

7 files changed

+136
-31
lines changed

7 files changed

+136
-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) {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`new84.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
<?php
10+
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+
21+
=====================================output=====================================
22+
<?php
23+
24+
(new Foo())->prop;
25+
(new Foo())->method();
26+
(new Foo())->$var;
27+
28+
(new SortOfLongClassName())
29+
->withALongMethodName()
30+
->andAnother()
31+
->toPushItPast80Chars();
32+
33+
$asdf = (new SortOfLongClassName())
34+
->withALongMethodName()
35+
->andAnother()
36+
->toPushItPast80Chars();
37+
38+
================================================================================
39+
`;
40+
41+
exports[`new84.php 2`] = `
42+
====================================options=====================================
43+
parsers: ["php"]
44+
phpVersion: "8.4"
45+
printWidth: 80
46+
| printWidth
47+
=====================================input======================================
48+
<?php
49+
50+
new Foo->prop;
51+
new Foo->method();
52+
new Foo->$var;
53+
54+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
55+
56+
$asdf =
57+
new SortOfLongClassName()->withALongMethodName()
58+
->andAnother()->toPushItPast80Chars();
59+
60+
=====================================output=====================================
61+
<?php
62+
63+
new Foo()->prop;
64+
new Foo()->method();
65+
new Foo()->$var;
66+
67+
new SortOfLongClassName()
68+
->withALongMethodName()
69+
->andAnother()
70+
->toPushItPast80Chars();
71+
72+
$asdf = new SortOfLongClassName()
73+
->withALongMethodName()
74+
->andAnother()
75+
->toPushItPast80Chars();
76+
77+
================================================================================
78+
`;

tests/new84/jsfmt.spec.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run_spec(import.meta, ["php"]);
2+
run_spec(import.meta, ["php"], { phpVersion: "8.4" });

tests/new84/new84.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
new Foo->prop;
4+
new Foo->method();
5+
new Foo->$var;
6+
7+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
8+
9+
$asdf =
10+
new SortOfLongClassName()->withALongMethodName()
11+
->andAnother()->toPushItPast80Chars();

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)