From c1c4532cc52458081e3570149d380d0dd19679cd Mon Sep 17 00:00:00 2001 From: Wunka <56411012+Wunka@users.noreply.github.com> Date: Sun, 30 Aug 2026 17:26:02 +0200 Subject: [PATCH] add mark_comment rule --- src/rules/_list.zig | 3 ++- src/rules/mark_comment.zig | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/rules/mark_comment.zig diff --git a/src/rules/_list.zig b/src/rules/_list.zig index 5716652..3dc18af 100644 --- a/src/rules/_list.zig +++ b/src/rules/_list.zig @@ -4,7 +4,8 @@ pub const comment_start = @import("comment_start.zig"); pub const empty_line_at_file_end = @import("empty_line_at_file_end.zig"); pub const if_for_while_braces = @import("if_for_while_braces.zig"); pub const imports = @import("imports.zig"); +pub const mark_comment = @import("mark_comment.zig"); +pub const orelse_unreachable = @import("orelse_unreachable.zig"); pub const tab_indentation = @import("tab_indentation.zig"); pub const trailing_space = @import("trailing_space.zig"); -pub const orelse_unreachable = @import("orelse_unreachable.zig"); pub const unused_private_decls = @import("unused_private_decls.zig"); diff --git a/src/rules/mark_comment.zig b/src/rules/mark_comment.zig new file mode 100644 index 0000000..f3492f0 --- /dev/null +++ b/src/rules/mark_comment.zig @@ -0,0 +1,39 @@ +const std = @import("std"); + +const main = @import("main"); + +const maxLineNumber = 100; + +pub fn check(ctx: main.Context) void { + const ast = ctx.ast orelse return; + + var buffer: [2]std.zig.Ast.Node.Index = undefined; + for (0..ast.nodes.len) |nodeIndex| { + const node: std.zig.Ast.Node.Index = @enumFromInt(nodeIndex); + if (ast.nodeTag(node) != .simple_var_decl) continue; + const varDec = ast.simpleVarDecl(node); + if (varDec.ast.type_node.unwrap() != null) continue; + var name = ast.tokenSlice(varDec.ast.mut_token + 1); + if (name.len != 0 and name[0] == '@') name = name[2 .. name.len - 1]; + + const rhsNode = varDec.ast.init_node.unwrap() orelse continue; + _ = ast.fullContainerDecl(&buffer, rhsNode) orelse continue; + + const firstToken = ast.firstToken(rhsNode); + const lastToken = ast.lastToken(rhsNode); + + const firstTokenlocation = ast.tokenLocation(firstToken, firstToken); + const lastTokenlocation = ast.tokenLocation(firstToken, lastToken); + + if (lastTokenlocation.line - firstTokenlocation.line < maxLineNumber) continue; + + const lineText = ctx.data[firstTokenlocation.line_start..firstTokenlocation.line_end]; + const markComment = std.fmt.allocPrint(main.allocator, "// MARK: {s}", .{name}) catch @panic("OOM"); + defer main.allocator.free(markComment); + if (std.mem.endsWith(u8, lineText, markComment)) continue; + + const msg = std.fmt.allocPrint(main.allocator, "Container over the size of {d} should have a MARK comment like this: \"// MARK: {s}\"", .{maxLineNumber, name}) catch @panic("OOM"); + defer main.allocator.free(msg); + ctx.printError(msg, firstTokenlocation.line_end); + } +}