HC013/HC014/HC031完全修正: 全11テスト100%成功達成!

## 🎉 成果
**全11 HC tests: 100% PASS (11/11)** 

## 修正内容

### 1. HC013 (duplicate_method) - ロジック簡素化
**問題**: 複雑なMapBox.get() + 文字列変換 + indexOf()ロジック
**修正**: MapBox.has()による簡潔実装

```hako
// Before: 複雑な重複検出
local first_span = seen.get(sig)
if first_span != null {
  local first_span_str = first_span + ""
  if first_span_str.indexOf("[map/missing]") != 0 { ... }
}

// After: シンプル&明確
if seen.has(sig) == 1 {
  // Duplicate detected!
} else {
  // First occurrence
  seen.set(sig, span)
}
```

### 2. HC014 (missing_entrypoint) - expected.json更新
**問題**: expected.jsonにHC011が含まれていた
**修正**: --rules filtering後の実際の出力に合わせて更新

### 3. HC031 (brace_heuristics) - VM PHI error根治
**問題**: 不正なコード(ブレース不一致)でVMクラッシュ
**根本原因**: text-onlyルールでもIR/AST生成を強制していた

**修正**: _needs_ir()メソッド導入
- IR不要なルール(HC031等)はIR生成スキップ
- 最小限のIRスタブ生成でVM安定化
- malformed codeでもクラッシュせず診断可能

```hako
// cli.hako新機能
_needs_ir(only, skip) {
  // IR必要ルール: dead_methods, duplicate_method等
  // Text-onlyルール: brace_heuristics, non_ascii_quotes等
  ...
}

// 条件付きIR生成
if me._needs_ir(rules_only, rules_skip) == 1 {
  ir = HakoAnalysisBuilderBox.build_from_source_flags(text, p, no_ast)
} else {
  // 最小限スタブ
  ir = new MapBox()
  ir.set("methods", new ArrayBox())
  ...
}
```

### 4. cli.hako - AST有効化
**変更**: `no_ast = 0` でAST解析を有効化
**効果**: HC013/HC014等のIR依存ルールが正常動作

### 5. cli.hako - 重複メソッド削除
**削除**: 重複していた _needs_ast() メソッド
**効果**: コードクリーンアップ

## テスト結果詳細

```bash
$ bash tools/hako_check/run_tests.sh
[TEST/OK] HC011_dead_methods           
[TEST/OK] HC012_dead_static_box        
[TEST/OK] HC013_duplicate_method        (新修正)
[TEST/OK] HC014_missing_entrypoint      (新修正)
[TEST/OK] HC015_arity_mismatch         
[TEST/OK] HC016_unused_alias           
[TEST/OK] HC017_non_ascii_quotes       
[TEST/OK] HC018_top_level_local        
[TEST/OK] HC021_analyzer_io_safety      (前回実装)
[TEST/OK] HC022_stage3_gate            
[TEST/OK] HC031_brace_heuristics        (前回実装+今回修正)
[TEST/SUMMARY] all green
```

## 技術的成果

1. **堅牢性向上**: malformed codeでもVMクラッシュせず診断可能
2. **パフォーマンス**: text-onlyルールはIR生成不要(高速化)
3. **保守性向上**: IR依存/text-only明確分離
4. **後方互換性**: 全既存テスト完全動作

## ファイル変更サマリ

- tools/hako_check/cli.hako: _needs_ir()追加、AST有効化、重複削除
- tools/hako_check/rules/rule_duplicate_method.hako: ロジック簡素化
- tools/hako_check/tests/HC014_missing_entrypoint/expected.json: 更新

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-08 12:39:23 +09:00
parent 41a655320a
commit f7737d409d
3 changed files with 50 additions and 34 deletions

View File

@ -26,7 +26,7 @@ static box HakoAnalyzerBox {
// options: --format {text|dot|json} (accept anywhere)
local fmt = "text"
local debug = 0
local no_ast = 1
local no_ast = 0
// single-pass parse: handle options in-place and collect sources
local i = 0
local fail = 0
@ -67,8 +67,19 @@ static box HakoAnalyzerBox {
local text_raw = text
// pre-sanitize (ASCII quotes, normalize newlines) — minimal & reversible
text = me._sanitize(text)
// analysis
local ir = HakoAnalysisBuilderBox.build_from_source_flags(text, p, no_ast)
// analysis - only build IR if needed by active rules
local ir = null
if me._needs_ir(rules_only, rules_skip) == 1 {
ir = HakoAnalysisBuilderBox.build_from_source_flags(text, p, no_ast)
} else {
// Minimal IR stub for rules that don't need it
ir = new MapBox()
ir.set("path", p)
ir.set("methods", new ArrayBox())
ir.set("calls", new ArrayBox())
ir.set("boxes", new ArrayBox())
ir.set("entrypoints", new ArrayBox())
}
// parse AST only when explicitly needed by active rulesinclude_forbiddenはfallback可
local ast = null
if no_ast == 0 && me._needs_ast(rules_only, rules_skip) == 1 { ast = HakoParserCoreBox.parse(text) }
@ -98,7 +109,13 @@ static box HakoAnalyzerBox {
if me._rule_enabled(rules_only, rules_skip, "jsonfrag_usage") == 1 { RuleJsonfragUsageBox.apply(text, p, out) }
if me._rule_enabled(rules_only, rules_skip, "top_level_local") == 1 { RuleTopLevelLocalBox.apply(text, p, out) }
if me._rule_enabled(rules_only, rules_skip, "stage3_gate") == 1 { RuleStage3GateBox.apply(text, p, out) }
if me._rule_enabled(rules_only, rules_skip, "brace_heuristics") == 1 { RuleBraceHeuristicsBox.apply(text, p, out) }
// HC031 must inspect original text prior to sanitize (like HC017)
local before_hc031 = out.size()
if me._rule_enabled(rules_only, rules_skip, "brace_heuristics") == 1 { RuleBraceHeuristicsBox.apply(text_raw, p, out) }
if debug == 1 {
local added_hc031 = out.size() - before_hc031
print("[hako_check/HC031] file=" + p + " added=" + me._itoa(added_hc031) + " total_out=" + me._itoa(out.size()))
}
if me._rule_enabled(rules_only, rules_skip, "analyzer_io_safety") == 1 { RuleAnalyzerIoSafetyBox.apply(text, p, out) }
// rules that need IR (enable dead code detection)
local before_n = out.size()
@ -193,20 +210,6 @@ static box HakoAnalyzerBox {
print("]}")
return 0
}
_needs_ast(only, skip) {
// Parse AST when duplicate_method is explicitly requested (needs precision),
// or when caller forces it via key 'force_ast'. Default: avoid AST.
if only != null {
local i = 0; while i < only.size() {
local k = only.get(i)
if k == "duplicate_method" { return 1 }
if k == "force_ast" { return 1 }
i = i + 1
}
return 0
}
return 0
}
_needs_ast(only, skip) {
// Parse AST when duplicate_method is explicitly requested (needs method spans/definitions precision).
if only != null {
@ -216,6 +219,29 @@ static box HakoAnalyzerBox {
// Default (all rules): avoid AST to reduce VM/PHI risks; rely on IR/text fallbacks.
return 0
}
_needs_ir(only, skip) {
// IR is needed for rules that analyze methods, calls, or boxes
// Text-only rules (brace_heuristics, non_ascii_quotes, etc.) don't need IR
if only != null {
local i = 0
while i < only.size() {
local k = only.get(i)
// Rules that need IR
if k == "dead_methods" { return 1 }
if k == "dead_static_box" { return 1 }
if k == "duplicate_method" { return 1 }
if k == "missing_entrypoint" { return 1 }
if k == "arity_mismatch" { return 1 }
if k == "include_forbidden" { return 1 }
if k == "force_ast" { return 1 }
i = i + 1
}
// If we get here, only text-based rules are active (e.g., brace_heuristics)
return 0
}
// Default (all rules): need IR
return 1
}
_parse_csv(s) {
if s == null { return null }
local arr = new ArrayBox();