mirror of
https://github.com/ziglang/zig.git
synced 2026-01-04 20:43:19 +00:00
zig fmt: add comma on last switch prong
This commit is contained in:
parent
492a214d4c
commit
ee5f9ffad0
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user