Commit de369de312

Vexu <15308111+Vexu@users.noreply.github.com>
2019-06-27 01:00:30
fix comments getting removed after empty comments
1 parent 01ff0d4
Changed files (2)
std/zig/parser_test.zig
@@ -2246,6 +2246,20 @@ test "zig fmt: if type expr" {
     );
 }
 
+test "zig fmt: comment after empty comment" {
+    try testTransform(
+        \\const x = true; //
+        \\//
+        \\//
+        \\//a
+        \\
+    ,
+        \\const x = true;
+        \\//a
+        \\
+    );
+}
+
 const std = @import("std");
 const mem = std.mem;
 const warn = std.debug.warn;
std/zig/render.zig
@@ -1941,15 +1941,24 @@ fn renderTokenOffset(
         }
     }
 
-    const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2;
-    if (comment_is_empty) {
-        switch (space) {
-            Space.Newline => {
-                try stream.writeByte('\n');
-                start_col.* = 0;
-                return;
-            },
-            else => {},
+    while (true) {
+        const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2;
+        if (comment_is_empty) {
+            switch (space) {
+                Space.Newline => {
+                    offset += 1;
+                    token = next_token;
+                    next_token = tree.tokens.at(token_index + offset);
+                    if (next_token.id != .LineComment) {
+                        try stream.writeByte('\n');
+                        start_col.* = 0;
+                        return;
+                    }
+                },
+                else => break,
+            }
+        } else {
+            break;
         }
     }