CROP
ProjectsParts Services

Dead Code Detection Setup

Date: 2025-11-11 Status: ✅ Complete Target: Hono Backend Microservices (6 services)

Dead Code Detection Setup

Date: 2025-11-11 Status: ✅ Complete Target: Hono Backend Microservices (6 services)


Executive Summary

Integrated comprehensive dead code detection system for backend microservices monorepo:

  • Biome - unused imports/variables/parameters detection
  • TypeScript - strict noUnused flags
  • Knip - unused exports/files/dependencies analysis
  • Custom Hono scripts - unmounted routes, orphaned repositories validation
  • CI/CD integration - automated via GitHub Actions and pre-commit hooks

Implementation Results

Phase 1: Biome Enhancement

  • Added unused code detection rules (noUnusedImports, noUnusedVariables, noUnusedFunctionParameters)
  • Auto-fixed 510 files
  • Configured overrides for intentional patterns (singletons, tests)

Phase 2: TypeScript Strict Flags

  • Enabled noUnusedLocals and noUnusedParameters
  • 0 unused variables found (codebase already clean)

Phase 3: Knip Setup

  • Configured for Hono monorepo with 6 services
  • Findings:
    • 16 unused files (legacy v3 versions)
    • 23 unused dependencies
    • 45 unused exports

Phase 4: Hono-Specific Checks

Created 4 validation scripts:

  1. check-unmounted-routes.ts - validates all routes are mounted
  2. check-orphaned-repositories.ts - validates all repos used in DI
  3. check-collections-usage.ts - checks MongoDB collections usage
  4. check-env-vars.ts - checks ENV variables usage (has false positives)

Phase 5: CI/CD Integration

  • Updated pre-commit hooks with Hono checks
  • Created GitHub Actions workflow (runs on PRs + weekly schedule)
  • Added npm scripts: dead-code, check:hono, check:all

Quick Start

Run All Checks

bun run check:all              # Full suite
bun run lint                   # Biome only
bun run dead-code              # Knip only
bun run check:hono             # Hono patterns only

Auto-Fix Issues

bun run lint:fix               # Biome auto-fix
bunx knip --fix                # Knip auto-fix (experimental)

Generate Reports

bun run dead-code:json         # Generate JSON report
cat knip-report.json | bun -e "const data = await Bun.stdin.json(); console.log('Files:', data.files.length);"

Files Created/Modified

Created (12 files):

  • knip.json - Knip configuration
  • scripts/check-unmounted-routes.ts
  • scripts/check-orphaned-repositories.ts
  • scripts/check-collections-usage.ts
  • scripts/check-env-vars.ts
  • scripts/analyze-dead-code.ts
  • scripts/scan-all-cyrillic.ts
  • .github/workflows/dead-code-check.yml
  • .husky/pre-commit (updated)
  • knip-report.json

Modified (4 files):

  • biome.json - added unused detection rules
  • packages/shared-config/tsconfig.json - added noUnused flags
  • package.json - added dead code scripts
  • .husky/pre-commit - added Hono checks

Known Issues & False Positives

1. Singleton Classes

Files: *-client.ts (MongoDB, Stripe, Elasticsearch) Biome Warning: noStaticOnlyClass Decision: Intentional pattern, ignored via override

2. ENV Variables Check

Issue: Reports 103 "unused" variables Cause: Doesn't detect centralized config files (src/config/env.ts) Status: Non-blocking in CI, needs improvement

3. Test Patterns

  • Non-null assertions in tests (safe, test-only)
  • console.log in scripts (allowed by override)

Maintenance Schedule

FrequencyActionToolDuration
Pre-commitLint + Hono checksHusky10-30s
Pull RequestFull CIGitHub Actions3-5min
WeeklyReview Knip reportManual15min
MonthlyDependency auditKnip30min
QuarterlyComprehensive cleanupAll tools2-4hrs

Next Steps

Immediate

  1. Review Knip findings (16 unused files)
  2. Remove unused dependencies (23 total)
  3. Fix remaining Biome warnings (175)

Short Term

  1. Improve check-env-vars.ts (reduce false positives)
  2. Enable MongoDB collections check in CI
  3. Address noExplicitAny warnings (133 in payment service)

Long Term

  1. Add route usage metrics (Prometheus)
  2. Detect routes with 0 hits in 30 days
  3. Set up quarterly cleanup review

Version: 1.0.0 Implementation Time: ~5 hours Codebase Health: 9/10

On this page