2025-11-08 02:59:54 +09:00
|
|
|
// tools/hako_check/rules/rule_duplicate_method.hako — HC013: Duplicate Method
|
|
|
|
|
// Detect methods with the same name and arity defined multiple times in the same box.
|
|
|
|
|
|
|
|
|
|
static box RuleDuplicateMethodBox {
|
|
|
|
|
method apply_ir(ir, path, out) {
|
|
|
|
|
local boxes = ir.get("boxes")
|
|
|
|
|
if boxes == null { return 0 }
|
|
|
|
|
|
|
|
|
|
// Check each box for duplicate methods
|
|
|
|
|
local bi = 0
|
|
|
|
|
while bi < boxes.size() {
|
|
|
|
|
local box_info = boxes.get(bi)
|
|
|
|
|
if box_info == null { bi = bi + 1; continue }
|
|
|
|
|
|
|
|
|
|
local box_name = box_info.get("name")
|
|
|
|
|
if box_name == null { bi = bi + 1; continue }
|
|
|
|
|
|
|
|
|
|
local methods = box_info.get("methods")
|
|
|
|
|
if methods == null { bi = bi + 1; continue }
|
|
|
|
|
|
|
|
|
|
// Track seen methods: {name/arity -> first_span}
|
|
|
|
|
local seen = new MapBox()
|
|
|
|
|
|
|
|
|
|
local mi = 0
|
|
|
|
|
while mi < methods.size() {
|
|
|
|
|
local method = methods.get(mi)
|
|
|
|
|
if method == null { mi = mi + 1; continue }
|
|
|
|
|
|
|
|
|
|
local name = method.get("name")
|
|
|
|
|
local arity = method.get("arity")
|
|
|
|
|
if name == null || arity == null { mi = mi + 1; continue }
|
|
|
|
|
|
|
|
|
|
// Create signature: name/arity
|
|
|
|
|
local sig = name + "/" + me._itoa(arity)
|
|
|
|
|
|
|
|
|
|
// Check if already seen
|
2025-11-08 12:39:23 +09:00
|
|
|
if seen.has(sig) == 1 {
|
|
|
|
|
// Duplicate detected!
|
|
|
|
|
local span = method.get("span")
|
|
|
|
|
local line = (span != null) ? span : 1
|
|
|
|
|
out.push("[HC013] duplicate method definition: " + box_name + "." + sig + " at line " + me._itoa(line))
|
2025-11-08 02:59:54 +09:00
|
|
|
} else {
|
2025-11-08 12:39:23 +09:00
|
|
|
// First occurrence - store it
|
2025-11-08 02:59:54 +09:00
|
|
|
local span = method.get("span")
|
|
|
|
|
seen.set(sig, span)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mi = mi + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bi = bi + 1
|
|
|
|
|
}
|
|
|
|
|
return out.size()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static box RuleDuplicateMethodMain { method main(args) { return 0 } }
|