2025-11-07 19:32:44 +09:00
static box RuleGlobalAssignBox {
apply(text, path, out) {
// HC010: global mutable state 禁止( top-levelの識別子= を雑に検出)
2025-11-08 00:46:34 +09:00
local lines = me._split_lines(text)
2025-11-07 19:32:44 +09:00
local in_box = 0; local in_method = 0
2025-11-07 21:04:01 +09:00
local i = 0; while i < lines.size() {
2025-11-07 19:32:44 +09:00
local ln = lines.get(i)
local t = me._ltrim(ln)
if t.indexOf("static box ") == 0 { in_box = 1; in_method = 0 }
if in_box == 1 && t == "}" { in_box = 0; in_method = 0 }
if in_box == 1 && t.indexOf("method ") == 0 { in_method = 1 }
if in_box == 1 && in_method == 0 {
// at top-level inside box: ident =
if me._looks_assign(t) == 1 {
2025-11-08 00:46:34 +09:00
out.push("[HC010] global assignment (top-level in box is forbidden): " + path + ":" + me._itoa(i+1))
2025-11-07 19:32:44 +09:00
}
}
2025-11-07 21:04:01 +09:00
i = i + 1 }
2025-11-07 19:32:44 +09:00
}
_ltrim(s) { return me._ltrim_chars(s, " \t") }
2025-11-08 00:46:34 +09:00
_split_lines(s) {
local arr = new ArrayBox(); if s == null { return arr }
local n = s.length(); local last = 0; local i = 0
while i < n { local ch = s.substring(i,i+1); if ch == "\n" { arr.push(s.substring(last,i)); last = i+1 } i = i + 1 }
arr.push(s.substring(last)); return arr
}
2025-11-07 19:32:44 +09:00
_ltrim_chars(s, cs) {
2025-11-08 00:46:34 +09:00
local n=s.length(); local head=0
2025-11-07 21:04:01 +09:00
local i = 0; while i < n { local ch=s.substring(i,i+1); if ch!=" "&&ch!="\t" { head=i; break }; if i==n-1 { head=n }; i = i + 1 }
2025-11-07 19:32:44 +09:00
return s.substring(head)
}
2025-11-08 00:46:34 +09:00
_itoa(n) { local v=0+n; if v==0 { return "0" } local out=""; local digits="0123456789"; local tmp=""; while v>0 { local d=v%10; tmp=digits.substring(d,d+1)+tmp; v=v/10 } out=tmp; return out }
2025-11-07 19:32:44 +09:00
_looks_assign(t) {
// very naive: identifier start followed by '=' somewhere (and not 'static box' or 'method')
2025-11-08 00:46:34 +09:00
if t.length() < 3 { return 0 }
2025-11-07 19:32:44 +09:00
local c = t.substring(0,1)
if !((c>="A"&&c<="Z")||(c>="a"&&c<="z")||c=="_") { return 0 }
if t.indexOf("static box ") == 0 || t.indexOf("method ") == 0 { return 0 }
if t.indexOf("=") > 0 { return 1 }
return 0
}
}
static box RuleGlobalAssignMain { method main(args) { return 0 } }