Skip to content

Commit 79578a9

Browse files
authored
feat: update parser and add arrow function support (#1277)
1 parent 691d11b commit 79578a9

10 files changed

Lines changed: 57 additions & 12 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": "^6.0.1",
16-
"php-parser": "glayzzle/php-parser#b3b2055c24bafc1abd66cb9612b93862db4abd22"
16+
"php-parser": "glayzzle/php-parser#4e9d584c7bd478175777e643e8d9d57d1863023a"
1717
},
1818
"devDependencies": {
1919
"@babel/preset-env": "^7.7.5",

src/printer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,13 @@ function printNode(path, options, print) {
17341734
case "closure":
17351735
case "method":
17361736
return printFunction(path, options, print);
1737+
case "arrowfunc":
1738+
return concat([
1739+
"fn",
1740+
printArgumentsList(path, options, print),
1741+
" => ",
1742+
path.call(print, "body")
1743+
]);
17371744
case "parameter": {
17381745
const name = concat([
17391746
node.nullable ? "?" : "",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`arrowfunc.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
<?php
10+
$var = fn() => "something";
11+
$var = fn($arg) => "something";
12+
$var = fn(&$arg) => "something";
13+
$var = fn($arg, $arg, $arg) => "something";
14+
// $var = fn(): ?string => "something";
15+
16+
call(fn($arg) => $arg);
17+
18+
=====================================output=====================================
19+
<?php
20+
$var = fn() => "something";
21+
$var = fn($arg) => "something";
22+
$var = fn(&$arg) => "something";
23+
$var = fn($arg, $arg, $arg) => "something";
24+
// $var = fn(): ?string => "something";
25+
26+
call(fn($arg) => $arg);
27+
28+
================================================================================
29+
`;

tests/arrowfunc/arrowfunc.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$var = fn() => "something";
3+
$var = fn($arg) => "something";
4+
$var = fn(&$arg) => "something";
5+
$var = fn($arg, $arg, $arg) => "something";
6+
// $var = fn(): ?string => "something";
7+
8+
call(fn($arg) => $arg);

tests/arrowfunc/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"]);

tests/bin/__snapshots__/jsfmt.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ return $var > $someOtherReallyReallyLongVariable ? true : false;
224224
return $someOtherReallyReallyLongVariable > $someOtherReallyReallyLongVariable ? true : false;
225225
return $someOtherReallyReallyReallyReallyReallyReallyLongVariable > $someOtherReallyReallyReallyReallyReallyReallyLongVariable ? true : false;
226226
227-
fn(
227+
func(
228228
$bifornCringerMoshedPerplexSawder,
229229
$askTrovenaBeenaDependsRowans,
230230
$glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl &&
@@ -680,7 +680,7 @@ return $someOtherReallyReallyReallyReallyReallyReallyLongVariable >
680680
? true
681681
: false;
682682
683-
fn(
683+
func(
684684
$bifornCringerMoshedPerplexSawder,
685685
$askTrovenaBeenaDependsRowans,
686686
$glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl &&

tests/bin/bin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function f()
216216
return $someOtherReallyReallyLongVariable > $someOtherReallyReallyLongVariable ? true : false;
217217
return $someOtherReallyReallyReallyReallyReallyReallyLongVariable > $someOtherReallyReallyReallyReallyReallyReallyLongVariable ? true : false;
218218

219-
fn(
219+
func(
220220
$bifornCringerMoshedPerplexSawder,
221221
$askTrovenaBeenaDependsRowans,
222222
$glimseGlyphsHazardNoopsTieTie === averredBathersBoxroomBuggyNurl &&

tests/comments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,9 +3375,9 @@ function foo(/* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */) {}
33753375
function foo($a): /* 5 */ ?string /* 6 */ {}
33763376
function foo(/* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */): /* 5 */ ?string /* 6 */ {}
33773377
function emptyFn(/*Comment */) {}
3378-
function fn( // Comment
3378+
function func( // Comment
33793379
) {}
3380-
function fn(
3380+
function func(
33813381
// Comment
33823382
) {}
33833383
function foo(
@@ -3425,10 +3425,10 @@ function foo(/* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */): /* 5 */ ?string /* 6 */
34253425
function emptyFn(/*Comment */)
34263426
{
34273427
}
3428-
function fn()
3428+
function func()
34293429
{
34303430
} // Comment
3431-
function fn()
3431+
function func()
34323432
{
34333433
}
34343434
// Comment

tests/comments/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ function foo(/* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */) {}
44
function foo($a): /* 5 */ ?string /* 6 */ {}
55
function foo(/* 1 */ $a /* 2 */ = /* 3 */ 1 /* 4 */): /* 5 */ ?string /* 6 */ {}
66
function emptyFn(/*Comment */) {}
7-
function fn( // Comment
7+
function func( // Comment
88
) {}
9-
function fn(
9+
function func(
1010
// Comment
1111
) {}
1212
function foo(

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4089,9 +4089,9 @@ performance-now@^2.1.0:
40894089
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
40904090
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=
40914091

4092-
php-parser@glayzzle/php-parser#b3b2055c24bafc1abd66cb9612b93862db4abd22:
4092+
php-parser@glayzzle/php-parser#4e9d584c7bd478175777e643e8d9d57d1863023a:
40934093
version "3.0.0-prerelease.9"
4094-
resolved "https://codeload.github.com/glayzzle/php-parser/tar.gz/b3b2055c24bafc1abd66cb9612b93862db4abd22"
4094+
resolved "https://codeload.github.com/glayzzle/php-parser/tar.gz/4e9d584c7bd478175777e643e8d9d57d1863023a"
40954095

40964096
pify@^2.0.0:
40974097
version "2.3.0"

0 commit comments

Comments
 (0)