Skip to content

Commit

Permalink
feat(match): Print trailing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonrcarter committed Aug 14, 2023
1 parent c770afb commit 353eee8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
27 changes: 24 additions & 3 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2887,11 +2887,25 @@ 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 ? "," : "";
let maybeTrailingComment = comments.hasTrailingComment(armNode)

Check failure on line 2899 in src/printer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14.x)

'maybeTrailingComment' is never reassigned. Use 'const' instead

Check failure on line 2899 in src/printer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14.x)

'maybeTrailingComment' is never reassigned. Use 'const' instead

Check failure on line 2899 in src/printer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'maybeTrailingComment' is never reassigned. Use 'const' instead

Check failure on line 2899 in src/printer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'maybeTrailingComment' is never reassigned. Use 'const' instead

Check failure on line 2899 in src/printer.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 14.x)

'maybeTrailingComment' is never reassigned. Use 'const' instead

Check failure on line 2899 in src/printer.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 14.x)

'maybeTrailingComment' is never reassigned. Use 'const' instead

Check failure on line 2899 in src/printer.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'maybeTrailingComment' is never reassigned. Use 'const' instead
? [
" ",
comments.printComments(
armNode.comments.filter((c) => c.trailing),
options
),
]
: [];

const conds =
armNode.conds === null
? "default"
Expand All @@ -2906,19 +2920,26 @@ function printNode(path, options, print) {
isPreviousLineEmpty(options.originalText, armNode, options)
? hardline
: "";

return [
",",
"",
hardline,
maybeEmptyLineBetweenArms,
...maybeLeadingComment,
group([group([conds, indent(line)]), "=> ", body]),
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 ? "," : ""])),
group(indent([...arms])),
" ",
softline,
"}",
Expand Down
12 changes: 10 additions & 2 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4847,7 +4847,11 @@ $withComments = match($v) {
'b' => 2,
default => null
// leading comment ...
'c' => 3, /* ... and trailing comment */
'd' // fourth comment
=> 4,
default => null // final comment
};
=====================================output=====================================
Expand All @@ -4861,7 +4865,11 @@ $withComments = match ($v) {
* second comment
*/
"b" => 2,
default => null,
// leading comment ...
"c" => 3, /* ... and trailing comment */
"d" // fourth comment
=> 4,
default => null, // final comment
};
================================================================================
Expand Down
6 changes: 5 additions & 1 deletion tests/comments/match.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@


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

0 comments on commit 353eee8

Please sign in to comment.