Skip to content

fix(health): use whole-file NLOC for health file metrics#387

Open
joptimus wants to merge 1 commit into
repowise-dev:mainfrom
joptimus:fix/health-whole-file-nloc
Open

fix(health): use whole-file NLOC for health file metrics#387
joptimus wants to merge 1 commit into
repowise-dev:mainfrom
joptimus:fix/health-whole-file-nloc

Conversation

@joptimus
Copy link
Copy Markdown
Contributor

@joptimus joptimus commented Jun 5, 2026

Summary

  • Fixes health_file_metrics.nloc storing the sum of detected function-body NLOC instead of whole-file NLOC.
  • Adds FileComplexity.file_nloc. On successful parses it counts source lines with at least one non-comment token, so comment-only lines are excluded while code with trailing comments still counts. Parser failures and unsupported languages fall back to a plain non-blank line count.
  • Updates HealthAnalyzer._evaluate_file to use fcx.file_nloc. Per-function nloc is unchanged for biomarkers that need function-level measurements.

Context

Fixes #375.

Route-style files and modules with top-level logic could previously store very small NLOC values because top-level callbacks, imports, wiring, and config were outside detected function bodies. That made downstream health signals like churn risk and effort buckets look much worse than they should.

Test plan

  • Added coverage for JS and Python comment-only lines, trailing comments, multiline strings, route-style JS files, unsupported-language fallback, and the health metric using file_nloc.
  • uv run pytest tests/unit/health -q - 270 passed

…unction-body sum

health_file_metrics.nloc previously stored the sum of per-function body
NLOC, which undercount files with significant top-level code or logic
spread across middleware chains. Consumers (churn risk, repo score
weighting, effort buckets) already assumed a whole-file value.

FileComplexity gains a file_nloc field populated by walk_file:
- successful parse: count lines with at least one non-comment token
  (tree traversal skips comment subtrees, so comment-only and
  block-comment-only lines are excluded; code lines with trailing
  comments still count)
- unsupported language / parse failure: plain non-blank line fallback
  so file_nloc is never zero on readable source

_evaluate_file in engine.py now reads fcx.file_nloc directly instead
of summing FunctionComplexity.nloc values. Per-function nloc is
unchanged — biomarkers that need it (large_method, brain_method, etc.)
still use the function-level field.
@repowise-bot
Copy link
Copy Markdown

repowise-bot Bot commented Jun 5, 2026

✅ Health: 7.0 (unchanged)
1 file moved · 2 hotspots · 5 hidden couplings · 2 with fix history

⚠️ Change risk: moderate (riskier than 46% of this repo's commits · raw 8.5/10)
This change's risk is driven by:

  • large diff (many lines added)
  • scattered, high-entropy change

🩹 Review priority (files here with the most recent bug-fix history — defects cluster, so review these first)

File Score Δ Why
.../complexity/walker.py 3.0 → 2.5 ▼ -0.5 🔻 introduced nested complexity, complex method · ✅ resolved function hotspot

💡 .../complexity/walker.py: Flatten the control flow. Pull early-return guards to the top, extract the deepest branch into a helper, and consider replacing nested conditionals with a strategy table or dispatch dict.

🔥 Hotspots touched (2)
  • .../health/engine.py — 9 commits/90d, 3 dependents · primary owner: Raghav Chamadiya (98%)
  • .../complexity/walker.py — 6 commits/90d, 2 dependents · primary owner: Raghav Chamadiya (90%)
🔗 Hidden coupling (2 files)
  • .../health/engine.py co-changes with these files (not in this PR):
    • .../biomarkers/base.py (4× — 🟢 routine)
    • docs/CODE_HEALTH.md (3× — 🟢 routine)
    • .../biomarkers/README.md (3× — 🟢 routine)
  • .../complexity/walker.py co-changes with these files (not in this PR):
    • .../complexity/languages.py (4× — 🟢 routine)
    • .../health/test_complexity_walker.py (4× — 🟢 routine)

📊 Full report · ⭐ Star Repowise · 📥 Install bot · Last updated 2026-06-05 20:52 UTC
Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug?] Health NLOC may undercount route-style files and inflate churn risk

1 participant