Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/rules/_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
39 changes: 39 additions & 0 deletions src/rules/mark_comment.zig
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading