Commit 10e11b60e5

yujiri8 <yujiri@disroot.org>
2022-09-02 20:12:20
zig fmt: don't delete container doc comments
Fixes #12617
1 parent 4a08c6d
Changed files (2)
lib/std/zig/parser_test.zig
@@ -5057,6 +5057,21 @@ test "zig fmt: make single-line if no trailing comma" {
     );
 }
 
+test "zig fmt: preserve container doc comment in container without trailing comma" {
+    try testTransform(
+        \\const A = enum(u32) {
+        \\//! comment
+        \\_ };
+        \\
+    ,
+        \\const A = enum(u32) {
+        \\    //! comment
+        \\    _,
+        \\};
+        \\
+    );
+}
+
 test "zig fmt: make single-line if no trailing comma" {
     try testCanonical(
         \\// Test trailing comma syntax
lib/std/zig/render.zig
@@ -1933,12 +1933,15 @@ fn renderContainerDecl(
             break :one_line;
         }
 
-        // 2. A member of the container has a doc comment.
+        // 2. The container has a container comment.
+        if (token_tags[lbrace + 1] == .container_doc_comment) break :one_line;
+
+        // 3. A member of the container has a doc comment.
         for (token_tags[lbrace + 1 .. rbrace - 1]) |tag| {
             if (tag == .doc_comment) break :one_line;
         }
 
-        // 3. The container has non-field members.
+        // 4. The container has non-field members.
         for (container_decl.ast.members) |member| {
             if (!node_tags[member].isContainerField()) break :one_line;
         }
@@ -2358,7 +2361,7 @@ fn renderSpace(ais: *Ais, tree: Ast, token_index: Ast.TokenIndex, lexeme_len: us
     }
 }
 
-/// Returns true if there exists a comment between any of the tokens from
+/// Returns true if there exists a line comment between any of the tokens from
 /// `start_token` to `end_token`. This is used to determine if e.g. a
 /// fn_proto should be wrapped and have a trailing comma inserted even if
 /// there is none in the source.