Skip to content

Commit

Permalink
feat: update parser (#1270)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
czosel authored Jan 3, 2020
1 parent cee7f95 commit 8192b82
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"linguist-languages": "^7.5.1",
"mem": "^6.0.1",
"php-parser": "glayzzle/php-parser#42c28cdd8e8dbc3423d72d49bd86db8249e01fe5"
"php-parser": "glayzzle/php-parser#b3b2055c24bafc1abd66cb9612b93862db4abd22"
},
"devDependencies": {
"@babel/preset-env": "^7.7.5",
Expand Down
33 changes: 19 additions & 14 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ function printAssignmentRight(
return indent(concat([hardline, ref, printedRight]));
}

const pureRightNode = rightNode.kind === "cast" ? rightNode.what : rightNode;
const pureRightNode = rightNode.kind === "cast" ? rightNode.expr : rightNode;

const canBreak =
(pureRightNode.kind === "bin" &&
Expand Down Expand Up @@ -2509,16 +2509,21 @@ function printNode(path, options, print) {
{ shouldBreak }
);
}
case "entry":
return printAssignment(
node.key,
path.call(print, "key"),
" =>",
node.value,
path.call(print, "value"),
false,
options
);
case "entry": {
const ref = node.byRef ? "&" : "";
const unpack = node.unpack ? "..." : "";
return node.key
? printAssignment(
node.key,
path.call(print, "key"),
" =>",
node.value,
path.call(print, "value"),
false,
options
)
: concat([ref, unpack, path.call(print, "value")]);
}
case "yield": {
const printedKeyAndValue = concat([
node.key ? concat([path.call(print, "key"), " => "]) : "",
Expand Down Expand Up @@ -2551,9 +2556,9 @@ function printNode(path, options, print) {
"(",
node.type,
") ",
node.what.comments
? indent(path.call(print, "what"))
: path.call(print, "what")
node.expr.comments
? indent(path.call(print, "expr"))
: path.call(print, "expr")
]);
case "assignref":
case "assign": {
Expand Down
17 changes: 17 additions & 0 deletions tests/array/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ $o = [
$obj->props->cardType === $AwesomizerCardEnum->SEEFIRST,
];
// spread
$var = ['banana', 'orange', ...$parts, 'watermelon'];
$var = [...$arr1];
$var = [0, ...$arr1];
$var = array(...$arr1, ...$arr2, 111);
$var = [...$arr1, ...$arr1];
$var = [...getArr(), 'c'];
$var = [...new ArrayIterator(['a', 'b', 'c'])];
=====================================output=====================================
<?php
Expand Down Expand Up @@ -843,6 +851,15 @@ $o = [
$obj->props->cardType === $AwesomizerCardEnum->SEEFIRST
];
// spread
$var = ['banana', 'orange', ...$parts, 'watermelon'];
$var = [...$arr1];
$var = [0, ...$arr1];
$var = array(...$arr1, ...$arr2, 111);
$var = [...$arr1, ...$arr1];
$var = [...getArr(), 'c'];
$var = [...new ArrayIterator(['a', 'b', 'c'])];
================================================================================
`;

Expand Down
8 changes: 8 additions & 0 deletions tests/array/arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,11 @@ function () {
$obj->props->cardType === $AwesomizerCardEnum->SEEFIRST,
];

// spread
$var = ['banana', 'orange', ...$parts, 'watermelon'];
$var = [...$arr1];
$var = [0, ...$arr1];
$var = array(...$arr1, ...$arr2, 111);
$var = [...$arr1, ...$arr1];
$var = [...getArr(), 'c'];
$var = [...new ArrayIterator(['a', 'b', 'c'])];
4 changes: 4 additions & 0 deletions tests/parens/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ $var = (@foo() || @foo());
@$i / 0;
@($i) / 0;
$var = "a" . (@$b ? 'bar' : "baz");
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
Expand Down Expand Up @@ -1092,6 +1094,8 @@ $var = @foo() || @foo();
@$i / 0;
@$i / 0;
$var = "a" . (@$b ? 'bar' : "baz");
$a = false && foo();
$b = true || foo();
$c = (false and foo());
Expand Down
2 changes: 2 additions & 0 deletions tests/parens/bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@
@$i / 0;
@($i) / 0;

$var = "a" . (@$b ? 'bar' : "baz");

$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4046,9 +4046,9 @@ performance-now@^2.1.0:
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=

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

pify@^2.0.0:
version "2.3.0"
Expand Down

0 comments on commit 8192b82

Please sign in to comment.