Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update parser #1270

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2505,16 +2505,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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand this correctly that this differentiation is needed because of PHP 7.4 array unpacking and no key is available when unpacking something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new parser version, entry objects are created for every array entry, not just for associative arrays anymore (see old behavior here). Also, unpack and byRef were added to the entry node for a generic way to describe the context of a specific entry.

So with the new version, we have to account for entrys that don't have a key (non-associative arrays).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, this explains my follow-up question as well (why we didn't previously differentiate beetween kinds of array entries). 😁

What I discovered (but I need to create an issue over at the parser for that): It's possible to do [...&$var] which should cause a parser error instead.

? 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 @@ -2547,9 +2552,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