Skip to content

Commit 5a91477

Browse files
authored
added trailing commas to match body when enabled (#1949)
fixes #1948
1 parent e11cb3b commit 5a91477

4 files changed

Lines changed: 82 additions & 3 deletions

File tree

src/printer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,11 @@ function printNode(path, options, print) {
30623062
])
30633063
),
30643064
") {",
3065-
indent(concat(arms)),
3065+
group(
3066+
indent(
3067+
concat([...arms, options.trailingCommaPHP ? ifBreak(",") : ""])
3068+
)
3069+
),
30663070
" ",
30673071
softline,
30683072
"}",

tests/enum/__snapshots__/jsfmt.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ enum Suit implements Colorful
102102
{
103103
return match ($this) {
104104
Suit::Hearts, Suit::Diamonds => "Red",
105-
Suit::Clubs, Suit::Spades => "Black"
105+
Suit::Clubs, Suit::Spades => "Black",
106106
};
107107
}
108108

tests/match/__snapshots__/jsfmt.spec.js.snap

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ exports[`match.php 1`] = `
55
parsers: ["php"]
66
phpVersion: "8.0"
77
printWidth: 80
8+
trailingCommaPHP: false
89
| printWidth
910
=====================================input======================================
1011
<?php
@@ -65,3 +66,70 @@ $nest = match (match ($a) { true => 1, false => 2 }) {
6566
6667
================================================================================
6768
`;
69+
70+
exports[`match.php 2`] = `
71+
====================================options=====================================
72+
parsers: ["php"]
73+
phpVersion: "8.0"
74+
printWidth: 80
75+
trailingCommaPHP: true
76+
| printWidth
77+
=====================================input======================================
78+
<?php
79+
80+
$really_really_really_long_variable_name = match(getValue()) {
81+
true => implode(',',[1,2,3]),
82+
false => $a || 'Empty',
83+
null => null,
84+
default => throw new \\InvalidArgumentException('Unknown Value'),
85+
};
86+
87+
$boolStr = match($v) {true => 'true', false => 'false'};
88+
89+
$boolish = match($v) {
90+
true, 1 => true,
91+
false,0,'',null => false,
92+
default => null
93+
};
94+
95+
$a = match(true) {
96+
test() => 'Good',
97+
test2() => 'Two',
98+
default => 'fail'
99+
};
100+
101+
$nest = match(match($a) {true => 1, false => 2}) {
102+
1 => match($b) {
103+
'ok' => true,
104+
'fail' => false,
105+
default => false
106+
},
107+
2 => 'null'
108+
};
109+
=====================================output=====================================
110+
<?php
111+
112+
$really_really_really_long_variable_name = match (getValue()) {
113+
true => implode(",", [1, 2, 3]),
114+
false => $a || "Empty",
115+
null => null,
116+
default => throw new \\InvalidArgumentException("Unknown Value"),
117+
};
118+
119+
$boolStr = match ($v) { true => "true", false => "false" };
120+
121+
$boolish = match ($v) {
122+
true, 1 => true,
123+
false, 0, "", null => false,
124+
default => null,
125+
};
126+
127+
$a = match (true) { test() => "Good", test2() => "Two", default => "fail" };
128+
129+
$nest = match (match ($a) { true => 1, false => 2 }) {
130+
1 => match ($b) { "ok" => true, "fail" => false, default => false },
131+
2 => "null",
132+
};
133+
134+
================================================================================
135+
`;

tests/match/jsfmt.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
run_spec(__dirname, ["php"], { phpVersion: "8.0" });
1+
run_spec(__dirname, ["php"], {
2+
trailingCommaPHP: false,
3+
phpVersion: "8.0",
4+
});
5+
run_spec(__dirname, ["php"], {
6+
trailingCommaPHP: true,
7+
phpVersion: "8.0",
8+
});

0 commit comments

Comments
 (0)