Implement HC018: Top-level local declaration detection
## Overview
Detects top-level `local` declarations (outside of methods/boxes), which are cleanup omissions in Hakorune code.
## Implementation Details
- **Rule**: `rule_top_level_local.hako` following box principles
- **Detection Method**: Text-based scanning with context tracking
- Tracks box/method entry/exit via brace depth
- Identifies `local` statements outside method scope
- Filters out comments (lines starting with `//`)
- **Integration**: Added to cli.hako in text-based rules section
## Technical Approach
- **Context Tracking**: Maintains `in_box` and `in_method` flags
- **Brace Depth Counter**: Tracks `{` and `}` to determine scope boundaries
- **Line-by-line Analysis**: Checks each line for `local ` prefix when not in method
- **Comment Filtering**: Ignores commented-out local declarations
## Test Cases
- **ok.hako**: All `local` declarations inside methods → no warnings
- Helper.calculate() and Helper.process() both referenced from Main.main()
- Avoids HC011 (unreachable method) warnings
- **ng.hako**: Top-level `local global_temp` outside any method → HC018 warning
## Test Results
```
[TEST/OK] HC011_dead_methods
[TEST/OK] HC012_dead_static_box
[TEST/OK] HC013_duplicate_method
[TEST/OK] HC014_missing_entrypoint
[TEST/OK] HC016_unused_alias
[TEST/OK] HC017_non_ascii_quotes
[TEST/OK] HC018_top_level_local ← NEW
[TEST/SUMMARY] all green
```
## Diagnostic Format
```
[HC018] top-level local declaration (not allowed): <path>:<line>
```
## Architecture
- Box-first design: RuleTopLevelLocalBox with single responsibility
- Helper methods: _trim(), _is_comment(), _split_lines(), _itoa()
- Clean separation of concerns: parsing, context tracking, reporting
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -12,6 +12,7 @@ using tools.hako_check.rules.rule_non_ascii_quotes as RuleNonAsciiQuotesBox
|
||||
using tools.hako_check.rules.rule_dead_static_box as RuleDeadStaticBoxBox
|
||||
using tools.hako_check.rules.rule_duplicate_method as RuleDuplicateMethodBox
|
||||
using tools.hako_check.rules.rule_missing_entrypoint as RuleMissingEntrypointBox
|
||||
using tools.hako_check.rules.rule_top_level_local as RuleTopLevelLocalBox
|
||||
using tools.hako_check.render.graphviz as GraphvizRenderBox
|
||||
using tools.hako_parser.parser_core as HakoParserCoreBox
|
||||
|
||||
@ -79,6 +80,7 @@ static box HakoAnalyzerBox {
|
||||
// HC017 must inspect original text prior to sanitize
|
||||
RuleNonAsciiQuotesBox.apply(text_raw, p, out)
|
||||
RuleJsonfragUsageBox.apply(text, p, out)
|
||||
RuleTopLevelLocalBox.apply(text, p, out)
|
||||
// rules that need IR (enable dead code detection)
|
||||
local before_n = out.size()
|
||||
RuleDeadMethodsBox.apply_ir(ir, p, out)
|
||||
|
||||
Reference in New Issue
Block a user