## 実装内容
### HC012: Dead Static Box
- 未参照static boxを検出
- IR boxes配列活用、calls情報から参照チェック
- テスト: HC012_dead_static_box/{ok,ng}.hako + expected.json
### HC013: Duplicate Method
- 同名・同arityメソッド重複検出
- Box内でメソッド署名の一意性チェック
- テスト: HC013_duplicate_method/{ok,ng}.hako + expected.json
### 🔥 Critical Bug Fix: parser_core.hako arity計算修正
- **問題**: arityが「カンマの数」を返していた(add(a,b) → 1)
- **修正**: `if any == 1 { arity = arity + 1 }` に変更
- **影響**: 全メソッドのarity計算が正しくなった(HC015等に波及)
### Infrastructure改善
- analysis_consumer.hako: _ensure_array()ヘルパー導入
- MapBox.get().push()問題の根本解決
- uses/methods/calls/boxes全てで安全なpush実現
- run_tests.sh: NYASH_JSON_ONLY=1で出力純度確保
- cli.hako: HC012/HC013統合、デバッグ出力追加
## テスト結果
✅ HC011_dead_methods: OK
✅ HC012_dead_static_box: OK
✅ HC013_duplicate_method: OK
✅ HC016_unused_alias: OK
[TEST/SUMMARY] all green
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
84 lines
2.4 KiB
Plaintext
84 lines
2.4 KiB
Plaintext
// 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.
|
|
|
|
using selfhost.shared.common.string_helpers as Str
|
|
|
|
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
|
|
local first_span = seen.get(sig)
|
|
if first_span != null {
|
|
// Check if it's a [map/missing] error
|
|
local first_span_str = first_span + ""
|
|
if first_span_str.indexOf("[map/missing]") != 0 {
|
|
// 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))
|
|
} else {
|
|
// First occurrence
|
|
local span = method.get("span")
|
|
seen.set(sig, span)
|
|
}
|
|
} else {
|
|
// First occurrence
|
|
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 } }
|