static box RuleGlobalAssignBox { apply(text, path, out) { // HC010: global mutable state 禁止(top-levelの識別子= を雑に検出) local lines = me._split_lines(text) local in_box = 0; local in_method = 0 local i = 0; while i < lines.size() { 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 { out.push("[HC010] global assignment (top-level in box is forbidden): " + path + ":" + me._itoa(i+1)) } } i = i + 1 } } _ltrim(s) { return me._ltrim_chars(s, " \t") } _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 } _ltrim_chars(s, cs) { local n=s.length(); local head=0 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 } return s.substring(head) } _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 } _looks_assign(t) { // very naive: identifier start followed by '=' somewhere (and not 'static box' or 'method') if t.length() < 3 { return 0 } 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 } }