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

[8.1] Add support for Intersection type #1933

Merged
merged 2 commits into from
Mar 5, 2022
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": "^8.0.0",
"php-parser": "3.1.0-beta.4"
"php-parser": "3.1.0-beta.5"
},
"devDependencies": {
"@babel/preset-env": "^7.16.11",
Expand Down
25 changes: 16 additions & 9 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,18 @@ function isStringOnItsOwnLine(node, text, options) {
);
}

function printComposedTypes(path, print, glue) {
return group(
concat(
path.map(
(uPath, i) =>
concat(i === 0 ? [path.call(print)] : [glue, path.call(print)]),
"types"
)
)
);
}

function printNode(path, options, print) {
const node = path.getValue();

Expand Down Expand Up @@ -2930,16 +2942,11 @@ function printNode(path, options, print) {
quote,
]);
}
case "intersectiontype": {
return printComposedTypes(path, print, "&");
}
case "uniontype": {
return group(
concat(
path.map(
(uPath, i) =>
concat(i === 0 ? [path.call(print)] : ["|", path.call(print)]),
"types"
)
)
);
return printComposedTypes(path, print, "|");
}
case "encapsedpart": {
const open =
Expand Down
21 changes: 21 additions & 0 deletions tests/intersection-types/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`intersection.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php
function generateSlug(HasTitle&HasId $post) {
return strtolower($post->getTitle()) . $post->getId();
}
=====================================output=====================================
<?php
function generateSlug(HasTitle&HasId $post)
{
return strtolower($post->getTitle()) . $post->getId();
}

================================================================================
`;
4 changes: 4 additions & 0 deletions tests/intersection-types/intersection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function generateSlug(HasTitle&HasId $post) {
return strtolower($post->getTitle()) . $post->getId();
}
1 change: 1 addition & 0 deletions tests/intersection-types/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, ["php"]);
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3694,10 +3694,10 @@ path-type@^4.0.0:
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==

[email protected].4:
version "3.1.0-beta.4"
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.1.0-beta.4.tgz#a56e3c90e0371e41fb9c1a09f4459b5adad2b03b"
integrity sha512-fLW5tSV8PK0Kizn+Wx1LrhJIFW5eYME4RXoHwIIXfArTBRs7RQJhoU016rXJTPQZIKAXo93ddVyojVvCOETxiA==
[email protected].5:
version "3.1.0-beta.5"
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.1.0-beta.5.tgz#81fd04ede37f3796ba2585b985831caffd62ba50"
integrity sha512-3F3+yThjD7wn0sMuIG5iMQqutmH+RJUAbEyPW5S/greTp5ZArkpEweylQh+do22q9UJlJT1PrLN/AwnzDUti6Q==

picocolors@^1.0.0:
version "1.0.0"
Expand Down