Skip to content

Commit 2036567

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

File tree

7 files changed

+99
-34
lines changed

7 files changed

+99
-34
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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ function parse(text, opts) {
1010
// Todo https://github.com/glayzzle/php-parser/issues/170
1111
text = text.replace(/\?>\n<\?/g, "?>\n___PSEUDO_INLINE_PLACEHOLDER___<?");
1212

13+
const parserOpts = Object.assign(
14+
{ extractDoc: true },
15+
// only pass the version if user requested 8.4 syntax; parser is stricter
16+
// about allowed syntax than we are and currenly defaults to support for 8.3
17+
opts && opts.phpVersion === "8.4" ? { version: opts.phpVersion } : {}
18+
);
19+
1320
// initialize a new parser instance
1421
const parser = new engine({
15-
parser: {
16-
extractDoc: true,
17-
},
22+
parser: parserOpts,
1823
ast: {
1924
withPositions: true,
2025
withSource: 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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`new84.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
phpVersion: "8.4"
7+
printWidth: 80
8+
| printWidth
9+
=====================================input======================================
10+
<?php
11+
12+
new Foo->prop;
13+
new Foo->method();
14+
new Foo->$var;
15+
16+
new SortOfLongClassName()->withALongMethodName()->andAnother()->toPushItPast80Chars();
17+
18+
$asdf =
19+
new SortOfLongClassName()->withALongMethodName()
20+
->andAnother()->toPushItPast80Chars();
21+
22+
=====================================output=====================================
23+
<?php
24+
25+
new Foo()->prop;
26+
new Foo()->method();
27+
new Foo()->$var;
28+
29+
new SortOfLongClassName()
30+
->withALongMethodName()
31+
->andAnother()
32+
->toPushItPast80Chars();
33+
34+
$asdf = new SortOfLongClassName()
35+
->withALongMethodName()
36+
->andAnother()
37+
->toPushItPast80Chars();
38+
39+
================================================================================
40+
`;

tests/new84/jsfmt.spec.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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)