40 lines
1.6 KiB
Plaintext
40 lines
1.6 KiB
Plaintext
|
|
using selfhost.shared.common.string_helpers as Str
|
|||
|
|
|
|||
|
|
static box RuleGlobalAssignBox {
|
|||
|
|
apply(text, path, out) {
|
|||
|
|
// HC010: global mutable state 禁止(top-levelの識別子= を雑に検出)
|
|||
|
|
local lines = text.split("\n")
|
|||
|
|
local in_box = 0; local in_method = 0
|
|||
|
|
do { 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 + ":" + Str.int_to_str(i+1))
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
i = i + 1 } } while 0
|
|||
|
|
}
|
|||
|
|
_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 < n { local ch=s.substring(i,i+1); if ch!=" "&&ch!="\t" { head=i; break }; if i==n-1 { head=n }; i = i + 1 } } while 0
|
|||
|
|
return s.substring(head)
|
|||
|
|
}
|
|||
|
|
_looks_assign(t) {
|
|||
|
|
// very naive: identifier start followed by '=' somewhere (and not 'static box' or 'method')
|
|||
|
|
if Str.len(t) < 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 } }
|