zig fmt: add comma on last switch prong

This commit is contained in:
Andrew Kelley 2018-05-16 00:27:18 -04:00
parent 492a214d4c
commit ee5f9ffad0
3 changed files with 46 additions and 2 deletions

View File

@ -1406,7 +1406,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
},
State.SwitchCaseCommaOrEnd => |list_state| {
switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {
switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
*list_state.ptr = end;
continue;

View File

@ -1,3 +1,34 @@
test "zig fmt: add comma on last switch prong" {
try testTransform(
\\test "aoeu" {
\\switch (self.init_arg_expr) {
\\ InitArg.Type => |t| { },
\\ InitArg.None,
\\ InitArg.Enum => { }
\\}
\\ switch (self.init_arg_expr) {
\\ InitArg.Type => |t| { },
\\ InitArg.None,
\\ InitArg.Enum => { }//line comment
\\ }
\\}
,
\\test "aoeu" {
\\ switch (self.init_arg_expr) {
\\ InitArg.Type => |t| {},
\\ InitArg.None,
\\ InitArg.Enum => {},
\\ }
\\ switch (self.init_arg_expr) {
\\ InitArg.Type => |t| {},
\\ InitArg.None,
\\ InitArg.Enum => {}, //line comment
\\ }
\\}
\\
);
}
test "zig fmt: same-line doc comment on variable declaration" {
try testTransform(
\\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space

View File

@ -831,7 +831,20 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
}
try renderExpression(allocator, stream, tree, indent, switch_case.expr);
try renderToken(tree, stream, switch_case.lastToken() + 1, indent, true);
{
// Handle missing comma after last switch case
var index = switch_case.lastToken() + 1;
switch (tree.tokens.at(index).id) {
Token.Id.RBrace => {
try stream.write(",");
},
Token.Id.LineComment => {
try stream.write(", ");
try renderToken(tree, stream, index, indent, true);
},
else => try renderToken(tree, stream, index, indent, true),
}
}
},
ast.Node.Id.SwitchElse => {
const switch_else = @fieldParentPtr(ast.Node.SwitchElse, "base", base);