Skip to content

Commit 8192b82

Browse files
authored
feat: update parser (#1270)
* feat: flexible heredoc/nowdoc syntax * feat: update parser This adds support for the spread operator for arrays (PHP 7.4) and fixes an issue with silent / retif precedence. Fixes #1009
1 parent cee7f95 commit 8192b82

7 files changed

Lines changed: 53 additions & 17 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#42c28cdd8e8dbc3423d72d49bd86db8249e01fe5"
16+
"php-parser": "glayzzle/php-parser#b3b2055c24bafc1abd66cb9612b93862db4abd22"
1717
},
1818
"devDependencies": {
1919
"@babel/preset-env": "^7.7.5",

src/printer.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ function printAssignmentRight(
15021502
return indent(concat([hardline, ref, printedRight]));
15031503
}
15041504

1505-
const pureRightNode = rightNode.kind === "cast" ? rightNode.what : rightNode;
1505+
const pureRightNode = rightNode.kind === "cast" ? rightNode.expr : rightNode;
15061506

15071507
const canBreak =
15081508
(pureRightNode.kind === "bin" &&
@@ -2509,16 +2509,21 @@ function printNode(path, options, print) {
25092509
{ shouldBreak }
25102510
);
25112511
}
2512-
case "entry":
2513-
return printAssignment(
2514-
node.key,
2515-
path.call(print, "key"),
2516-
" =>",
2517-
node.value,
2518-
path.call(print, "value"),
2519-
false,
2520-
options
2521-
);
2512+
case "entry": {
2513+
const ref = node.byRef ? "&" : "";
2514+
const unpack = node.unpack ? "..." : "";
2515+
return node.key
2516+
? printAssignment(
2517+
node.key,
2518+
path.call(print, "key"),
2519+
" =>",
2520+
node.value,
2521+
path.call(print, "value"),
2522+
false,
2523+
options
2524+
)
2525+
: concat([ref, unpack, path.call(print, "value")]);
2526+
}
25222527
case "yield": {
25232528
const printedKeyAndValue = concat([
25242529
node.key ? concat([path.call(print, "key"), " => "]) : "",
@@ -2551,9 +2556,9 @@ function printNode(path, options, print) {
25512556
"(",
25522557
node.type,
25532558
") ",
2554-
node.what.comments
2555-
? indent(path.call(print, "what"))
2556-
: path.call(print, "what")
2559+
node.expr.comments
2560+
? indent(path.call(print, "expr"))
2561+
: path.call(print, "expr")
25572562
]);
25582563
case "assignref":
25592564
case "assign": {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,14 @@ $o = [
534534
$obj->props->cardType === $AwesomizerCardEnum->SEEFIRST,
535535
];
536536
537+
// spread
538+
$var = ['banana', 'orange', ...$parts, 'watermelon'];
539+
$var = [...$arr1];
540+
$var = [0, ...$arr1];
541+
$var = array(...$arr1, ...$arr2, 111);
542+
$var = [...$arr1, ...$arr1];
543+
$var = [...getArr(), 'c'];
544+
$var = [...new ArrayIterator(['a', 'b', 'c'])];
537545
538546
=====================================output=====================================
539547
<?php
@@ -843,6 +851,15 @@ $o = [
843851
$obj->props->cardType === $AwesomizerCardEnum->SEEFIRST
844852
];
845853
854+
// spread
855+
$var = ['banana', 'orange', ...$parts, 'watermelon'];
856+
$var = [...$arr1];
857+
$var = [0, ...$arr1];
858+
$var = array(...$arr1, ...$arr2, 111);
859+
$var = [...$arr1, ...$arr1];
860+
$var = [...getArr(), 'c'];
861+
$var = [...new ArrayIterator(['a', 'b', 'c'])];
862+
846863
================================================================================
847864
`;
848865

tests/array/arrays.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,11 @@ function () {
526526
$obj->props->cardType === $AwesomizerCardEnum->SEEFIRST,
527527
];
528528

529+
// spread
530+
$var = ['banana', 'orange', ...$parts, 'watermelon'];
531+
$var = [...$arr1];
532+
$var = [0, ...$arr1];
533+
$var = array(...$arr1, ...$arr2, 111);
534+
$var = [...$arr1, ...$arr1];
535+
$var = [...getArr(), 'c'];
536+
$var = [...new ArrayIterator(['a', 'b', 'c'])];

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,8 @@ $var = (@foo() || @foo());
712712
@$i / 0;
713713
@($i) / 0;
714714
715+
$var = "a" . (@$b ? 'bar' : "baz");
716+
715717
$a = (false && foo());
716718
$b = (true || foo());
717719
$c = (false and foo());
@@ -1092,6 +1094,8 @@ $var = @foo() || @foo();
10921094
@$i / 0;
10931095
@$i / 0;
10941096
1097+
$var = "a" . (@$b ? 'bar' : "baz");
1098+
10951099
$a = false && foo();
10961100
$b = true || foo();
10971101
$c = (false and foo());

tests/parens/bin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@
338338
@$i / 0;
339339
@($i) / 0;
340340

341+
$var = "a" . (@$b ? 'bar' : "baz");
342+
341343
$a = (false && foo());
342344
$b = (true || foo());
343345
$c = (false and foo());

yarn.lock

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

4049-
php-parser@glayzzle/php-parser#42c28cdd8e8dbc3423d72d49bd86db8249e01fe5:
4049+
php-parser@glayzzle/php-parser#b3b2055c24bafc1abd66cb9612b93862db4abd22:
40504050
version "3.0.0-prerelease.9"
4051-
resolved "https://codeload.github.com/glayzzle/php-parser/tar.gz/42c28cdd8e8dbc3423d72d49bd86db8249e01fe5"
4051+
resolved "https://codeload.github.com/glayzzle/php-parser/tar.gz/b3b2055c24bafc1abd66cb9612b93862db4abd22"
40524052

40534053
pify@^2.0.0:
40544054
version "2.3.0"

0 commit comments

Comments
 (0)