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

fix(match): Support empty lines and comments in match expressions (closes #1956) #2201

Merged
merged 4 commits into from
Aug 16, 2023
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
41 changes: 37 additions & 4 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const {
normalizeMagicMethodName,
getNextNonSpaceNonCommentCharacterIndex,
isNextLineEmpty,
isPreviousLineEmpty,
} = require("./util");

function isMinVersion(actualVersion, requiredVersion) {
Expand Down Expand Up @@ -2886,27 +2887,59 @@ function printNode(path, options, print) {
return path.call(print, "name");
}
case "match": {
const lastArmIndex = node.arms.length - 1;
const arms = path.map((armPath, armIdx) => {
const armNode = armPath.getValue();

const maybeLeadingComment = comments.hasLeadingComment(armNode)
? [comments.printComments(armNode.leadingComments, options), hardline]
: [];
const maybeTrailingComma =
armIdx < lastArmIndex || options.trailingCommaPHP ? "," : "";
const maybeTrailingComment = comments.hasTrailingComment(armNode)
? [
" ",
comments.printComments(
armNode.comments.filter((c) => c.trailing),
options
),
]
: [];

const conds =
armPath.getValue().conds === null
armNode.conds === null
? "default"
: armPath.map(
(condPath, condIdx) =>
[",", line, print(condPath)].slice(condIdx === 0 ? 2 : 0),
"conds"
);
const body = armPath.call(print, "body");
const maybeEmptyLineBetweenArms =
armIdx > 0 &&
isPreviousLineEmpty(options.originalText, armNode, options)
? hardline
: "";

return [
",",
"",
hardline,
group([group([conds, indent(line)]), "=> ", body]),
maybeEmptyLineBetweenArms,
...maybeLeadingComment,
group([
group([conds, indent(line)]),
"=> ",
body,
maybeTrailingComma,
...maybeTrailingComment,
]),
].slice(armIdx > 0 ? 0 : 1);
}, "arms");
return group([
"match (",
group([softline, indent(path.call(print, "cond")), softline]),
") {",
group(indent([...arms, options.trailingCommaPHP ? ifBreak(",") : ""])),
group(indent([...arms])),
" ",
softline,
"}",
Expand Down
51 changes: 51 additions & 0 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4824,6 +4824,57 @@ list(
================================================================================
`;

exports[`match.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php

$withComments = match($v) {

// first comment

'a' => 1,



/*
* second comment
*/



'b' => 2,
// leading comment ...
'c' => 3, /* ... and trailing comment */
'd' // fourth comment
=> 4,
default => null // final comment
};

=====================================output=====================================
<?php

$withComments = match ($v) {
// first comment
"a" => 1,

/*
* second comment
*/
"b" => 2,
// leading comment ...
"c" => 3, /* ... and trailing comment */
"d" // fourth comment
=> 4,
default => null, // final comment
};

================================================================================
`;

exports[`method.php 1`] = `
====================================options=====================================
parsers: ["php"]
Expand Down
23 changes: 23 additions & 0 deletions tests/comments/match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$withComments = match($v) {

// first comment

'a' => 1,



/*
* second comment
*/



'b' => 2,
// leading comment ...
'c' => 3, /* ... and trailing comment */
'd' // fourth comment
=> 4,
default => null // final comment
};
48 changes: 48 additions & 0 deletions tests/match/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ $extraLongMatch = match($a) {
'cd' => [],
default => [],
};

// Whitespace in match expressions is handled like in function/method arguments:
// - none above the first arm or below the last arm
// - empty lines are preserved between arms
// - multiple empty lines are collapsed into one
match ($a) {

'a' => 1,


'b' => 2

};

=====================================output=====================================
<?php

Expand Down Expand Up @@ -124,6 +138,16 @@ $extraLongMatch = match ($a) {
default => []
};

// Whitespace in match expressions is handled like in function/method arguments:
// - none above the first arm or below the last arm
// - empty lines are preserved between arms
// - multiple empty lines are collapsed into one
match ($a) {
"a" => 1,

"b" => 2
};

================================================================================
`;

Expand Down Expand Up @@ -179,6 +203,20 @@ $extraLongMatch = match($a) {
'cd' => [],
default => [],
};

// Whitespace in match expressions is handled like in function/method arguments:
// - none above the first arm or below the last arm
// - empty lines are preserved between arms
// - multiple empty lines are collapsed into one
match ($a) {

'a' => 1,


'b' => 2

};

=====================================output=====================================
<?php

Expand Down Expand Up @@ -251,5 +289,15 @@ $extraLongMatch = match ($a) {
default => [],
};

// Whitespace in match expressions is handled like in function/method arguments:
// - none above the first arm or below the last arm
// - empty lines are preserved between arms
// - multiple empty lines are collapsed into one
match ($a) {
"a" => 1,

"b" => 2,
};

================================================================================
`;
15 changes: 14 additions & 1 deletion tests/match/match.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@
=> 'some really long value in the return part of the match statement',
'cd' => [],
default => [],
};
};

// Whitespace in match expressions is handled like in function/method arguments:
// - none above the first arm or below the last arm
// - empty lines are preserved between arms
// - multiple empty lines are collapsed into one
match ($a) {

'a' => 1,


'b' => 2

};
Loading