aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsaac Freund <ifreund@ifreund.xyz>2021-02-24 12:29:17 +0100
committerIsaac Freund <ifreund@ifreund.xyz>2021-02-24 12:29:17 +0100
commit15c7c6ab970830a87b6aa502a369fb2b29b933c5 (patch)
treed169f38dd855e286559b3f2c9883e2cb61ca3247
parent1b8eca030e9dac51361d80428f8276a09f31a8c2 (diff)
downloadzig-15c7c6ab970830a87b6aa502a369fb2b29b933c5.tar.gz
zig-15c7c6ab970830a87b6aa502a369fb2b29b933c5.zip
zig fmt: handle comments in switch case value list
-rw-r--r--lib/std/zig/parser_test.zig31
-rw-r--r--lib/std/zig/render.zig4
2 files changed, 34 insertions, 1 deletions
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index c0d8a012c9..5ac0d62026 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -4202,6 +4202,37 @@ test "zig fmt: respect extra newline between switch items" {
);
}
+test "zig fmt: insert trailing comma if there are comments between switch values" {
+ try testTransform(
+ \\const a = switch (b) {
+ \\ .c => {},
+ \\
+ \\ .d, // foobar
+ \\ .e
+ \\ => f,
+ \\
+ \\ .g, .h
+ \\ // comment
+ \\ => i,
+ \\};
+ \\
+ ,
+ \\const a = switch (b) {
+ \\ .c => {},
+ \\
+ \\ .d, // foobar
+ \\ .e,
+ \\ => f,
+ \\
+ \\ .g,
+ \\ .h,
+ \\ // comment
+ \\ => i,
+ \\};
+ \\
+ );
+}
+
test "zig fmt: error for invalid bit range" {
try testError(
\\var x: []align(0:0:0)u8 = bar;
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig
index 0331ca2c70..604ee4f312 100644
--- a/lib/std/zig/render.zig
+++ b/lib/std/zig/render.zig
@@ -1533,7 +1533,9 @@ fn renderSwitchCase(
} else if (switch_case.ast.values.len == 1) {
// render on one line and drop the trailing comma if any
try renderExpression(gpa, ais, tree, switch_case.ast.values[0], .space);
- } else if (trailing_comma) {
+ } else if (trailing_comma or
+ hasComment(tree, tree.firstToken(switch_case.ast.values[0]), switch_case.ast.arrow_token))
+ {
// Render each value on a new line
try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma);
} else {