// hako_source_checker.hako — HakoSourceCheckerBox // Purpose: Lint/structure checks for .hako sources (Phase 21.3) // Rules (MVP): // HC001: Forbid top-level assignment inside static box (before any method) // HC002: Forbid include "..." lines (using+alias only) // HC003: Using must be quoted (using "pkg.name" as Alias) // HC004: Encourage JsonFragBox helpers for JSON scans (warn when substring/indexOf used with seg/inst_json) using selfhost.shared.common.string_helpers as Str using selfhost.shared.json.utils.json_frag as JsonFragBox static box HakoSourceCheckerBox { // Public: check a file path. Returns 0 on success; >0 on issues. check_file(path) { local f = new FileBox() if f.open(path) == 0 { print("[lint/error] cannot open: " + path); return 2 } local text = f.read(); f.close() return me.check_source(text, path) } // Public: check raw source check_source(text, path) { local issues = new ArrayBox() me._rule_include_forbidden(text, path, issues) me._rule_using_quoted(text, path, issues) me._rule_static_top_assign(text, path, issues) me._rule_jsonfrag_usage(text, path, issues) local n = issues.size() if n > 0 { do { local i=0; while i=n { break }; local cj=text.substring(j,j+1); if cj=="\n" { break }; if cj=="=" { seen_eq=1; break }; off=off+1 } } while 0 if seen_eq == 1 { out.push("[HC001] top-level assignment in static box (use lazy init in method): " + path + ":" + Str.int_to_str(line)) } } } } i=i+1 } } while 0 } // HC004: encourage JsonFragBox for JSON scans _rule_jsonfrag_usage(text, path, out) { // If the file manipulates mir_call/inst_json/seg and uses indexOf/substring heavily, warn. local suspicious = 0 if text.indexOf("\"mir_call\"") >= 0 || text.indexOf("inst_json") >= 0 || text.indexOf(" seg") >= 0 { if text.indexOf(".indexOf(") >= 0 || text.indexOf(".substring(") >= 0 { suspicious = 1 } } if suspicious == 1 && text.indexOf("JsonFragBox.") < 0 { out.push("[HC004] JSON scan likely brittle; prefer JsonFragBox helpers: " + path) } } // helpers _ltrim(s) { return me._ltrim_chars(s, " \t") } _ltrim_chars(s, cs) { local n = Str.len(s) local head = 0 do { local i=0; while i Str.len(s) { return 0 } if s.substring(i, i+k) == kw { return 1 } return 0 } _is_ident_start(c) { // ASCII alpha or _ if c >= "A" && c <= "Z" { return 1 } if c >= "a" && c <= "z" { return 1 } if c == "_" { return 1 } return 0 } _is_line_head(s, i) { // true if all chars before i on same line are spaces/tabs do { local r=0; while r<=i { if i==0 { return 1 }; local j=i - 1 - r; local cj=s.substring(j,j+1); if cj=="\n" { return 1 }; if cj!=" " && cj!="\t" { return 0 }; if j==0 { return 1 }; r=r+1 } } while 0 return 1 } _line_start(s, i) { do { local r=0; while r<=i { local j=i-r; if j==0 { return 0 }; local cj=s.substring(j-1,j); if cj=="\n" { return j }; r=r+1 } } while 0 return 0 } } static box HakoSourceCheckerMain { method main(args) { if args == null || args.size() < 1 { print("[lint/error] require at least one path argument") return 2 } local fail = 0 do { local i=0; while i