Skip to content

Commit 58ba9a9

Browse files
authored
feat: add support for named arguments and nullsafepropertylookup (php8) (#1656)
* feat(php8): nullsafepropertylookup * feat(php8): named arguments
1 parent eb46d55 commit 58ba9a9

13 files changed

Lines changed: 183 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"dependencies": {
1414
"linguist-languages": "^7.5.1",
1515
"mem": "^8.0.0",
16-
"php-parser": "3.0.2"
16+
"php-parser": "czosel/php-parser#177699bf5f19b2ac4a95049fb19000fbc4ffa0fd"
1717
},
1818
"devDependencies": {
1919
"@babel/preset-env": "^7.7.5",

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
{ value: "7.2" },
2323
{ value: "7.3" },
2424
{ value: "7.4" },
25+
{ value: "8.0" },
2526
],
2627
},
2728
trailingCommaPHP: {

src/printer.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@ function genericPrint(path, options, print) {
116116
return concat(parts);
117117
}
118118

119-
function printPropertyLookup(path, options, print) {
120-
return concat(["->", path.call(print, "offset")]);
119+
function printPropertyLookup(path, options, print, nullsafe = false) {
120+
return concat([nullsafe ? "?" : "", "->", path.call(print, "offset")]);
121+
}
122+
123+
function printNullsafePropertyLookup(path, options, print) {
124+
return printPropertyLookup(path, options, print, true);
121125
}
122126

123127
function printStaticLookup(path, options, print) {
@@ -243,6 +247,8 @@ function printMemberChain(path, options, print) {
243247

244248
if (node.kind === "propertylookup") {
245249
printedMemberish = printPropertyLookup(path, options, print);
250+
} else if (node.kind === "nullsafepropertylookup") {
251+
printedMemberish = printNullsafePropertyLookup(path, options, print);
246252
} else if (node.kind === "staticlookup") {
247253
printedMemberish = printStaticLookup(path, options, print);
248254
} else {
@@ -842,6 +848,8 @@ function printLookupNodes(path, options, print) {
842848
switch (node.kind) {
843849
case "propertylookup":
844850
return printPropertyLookup(path, options, print);
851+
case "nullsafepropertylookup":
852+
return printNullsafePropertyLookup(path, options, print);
845853
case "staticlookup":
846854
return printStaticLookup(path, options, print);
847855
case "offsetlookup":
@@ -2175,6 +2183,7 @@ function printNode(path, options, print) {
21752183
: path.call(print, "what"),
21762184
]);
21772185
case "propertylookup":
2186+
case "nullsafepropertylookup":
21782187
case "staticlookup":
21792188
case "offsetlookup": {
21802189
const parent = path.getParentNode();
@@ -2920,7 +2929,8 @@ function printNode(path, options, print) {
29202929
return node.comments
29212930
? comments.printComments(path.getValue().comments, options)
29222931
: "";
2923-
2932+
case "namedargument":
2933+
return concat([node.name, ": ", path.call(print, "value")]);
29242934
case "error":
29252935
default:
29262936
// istanbul ignore next
12 KB
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`namedarguments.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
phpVersion: "8.0"
7+
printWidth: 80
8+
| printWidth
9+
=====================================input======================================
10+
<?php
11+
func(a: $a);
12+
func(array: $arr);
13+
func(a: $a, b: $b, c: $c);
14+
func(a: $a, ...$b);
15+
$foo -> bar (arg1: $arg1, arg2: $arg2,$arg3, $arg4 , $arg5 ) ;
16+
Foo :: bar( arg1:$arg1,arg2:$arg2 ,$arg3, $arg4 , $arg5 ) ;
17+
18+
$db->Execute($sql, config: [
19+
$foo,
20+
$bar,
21+
$foobar,
22+
$somewhatLongParameter,
23+
$somewhatLongParameterX,
24+
$somewhatLongParameterXYZ
25+
]);
26+
27+
$app->get('/hello/{name}', callback: function ($name) use ($app) {
28+
return 'Hello ' . $app->escape($name);
29+
});
30+
31+
$this->something->method($argument, val: $this->more->stuff(
32+
$this->even->more->things->complicatedMethod()
33+
));
34+
35+
=====================================output=====================================
36+
<?php
37+
func(a: $a);
38+
func(array: $arr);
39+
func(a: $a, b: $b, c: $c);
40+
func(a: $a, ...$b);
41+
$foo->bar(arg1: $arg1, arg2: $arg2, $arg3, $arg4, $arg5);
42+
Foo::bar(arg1: $arg1, arg2: $arg2, $arg3, $arg4, $arg5);
43+
44+
$db->Execute(
45+
$sql,
46+
config: [
47+
$foo,
48+
$bar,
49+
$foobar,
50+
$somewhatLongParameter,
51+
$somewhatLongParameterX,
52+
$somewhatLongParameterXYZ,
53+
],
54+
);
55+
56+
$app->get(
57+
"/hello/{name}",
58+
callback: function ($name) use ($app) {
59+
return "Hello " . $app->escape($name);
60+
},
61+
);
62+
63+
$this->something->method(
64+
$argument,
65+
val: $this->more->stuff($this->even->more->things->complicatedMethod()),
66+
);
67+
68+
================================================================================
69+
`;

tests/namedarguments/jsfmt.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname, ["php"], { phpVersion: "8.0" });
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
func(a: $a);
3+
func(array: $arr);
4+
func(a: $a, b: $b, c: $c);
5+
func(a: $a, ...$b);
6+
$foo -> bar (arg1: $arg1, arg2: $arg2,$arg3, $arg4 , $arg5 ) ;
7+
Foo :: bar( arg1:$arg1,arg2:$arg2 ,$arg3, $arg4 , $arg5 ) ;
8+
9+
$db->Execute($sql, config: [
10+
$foo,
11+
$bar,
12+
$foobar,
13+
$somewhatLongParameter,
14+
$somewhatLongParameterX,
15+
$somewhatLongParameterXYZ
16+
]);
17+
18+
$app->get('/hello/{name}', callback: function ($name) use ($app) {
19+
return 'Hello ' . $app->escape($name);
20+
});
21+
22+
$this->something->method($argument, val: $this->more->stuff(
23+
$this->even->more->things->complicatedMethod()
24+
));
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`offsets.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
phpVersion: "8.0"
7+
printWidth: 80
8+
| printWidth
9+
=====================================input======================================
10+
<?php
11+
12+
// identifier
13+
$obj?->foo;
14+
// variable
15+
$obj?->$var;
16+
// variable variable
17+
$obj?->$$var;
18+
// literal with identifier
19+
$obj?->{foo};
20+
// literal with variable
21+
$obj?->{$var};
22+
// literal with call
23+
$obj?->{call()};
24+
// encapsed (offset type)
25+
$obj?->foo_{'test' . 'bar'};
26+
// variable with literal with call
27+
$obj?->\${call()};
28+
29+
=====================================output=====================================
30+
<?php
31+
32+
// identifier
33+
$obj?->foo;
34+
// variable
35+
$obj?->$var;
36+
// variable variable
37+
$obj?->$$var;
38+
// literal with identifier
39+
$obj?->{foo};
40+
// literal with variable
41+
$obj?->{$var};
42+
// literal with call
43+
$obj?->{call()};
44+
// encapsed (offset type)
45+
$obj?->foo_["test" . "bar"];
46+
// variable with literal with call
47+
$obj?->\${call()};
48+
49+
================================================================================
50+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname, ["php"], { phpVersion: "8.0" });
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
// identifier
4+
$obj?->foo;
5+
// variable
6+
$obj?->$var;
7+
// variable variable
8+
$obj?->$$var;
9+
// literal with identifier
10+
$obj?->{foo};
11+
// literal with variable
12+
$obj?->{$var};
13+
// literal with call
14+
$obj?->{call()};
15+
// encapsed (offset type)
16+
$obj?->foo_{'test' . 'bar'};
17+
// variable with literal with call
18+
$obj?->${call()};

0 commit comments

Comments
 (0)