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:
check-unmounted-routes.ts- validates all routes are mountedcheck-orphaned-repositories.ts- validates all repos used in DIcheck-collections-usage.ts- checks MongoDB collections usagecheck-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 onlyAuto-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 configurationscripts/check-unmounted-routes.tsscripts/check-orphaned-repositories.tsscripts/check-collections-usage.tsscripts/check-env-vars.tsscripts/analyze-dead-code.tsscripts/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 rulespackages/shared-config/tsconfig.json- added noUnused flagspackage.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
| Frequency | Action | Tool | Duration |
|---|---|---|---|
| Pre-commit | Lint + Hono checks | Husky | 10-30s |
| Pull Request | Full CI | GitHub Actions | 3-5min |
| Weekly | Review Knip report | Manual | 15min |
| Monthly | Dependency audit | Knip | 30min |
| Quarterly | Comprehensive cleanup | All tools | 2-4hrs |
Next Steps
Immediate
- Review Knip findings (16 unused files)
- Remove unused dependencies (23 total)
- Fix remaining Biome warnings (175)
Short Term
- Improve
check-env-vars.ts(reduce false positives) - Enable MongoDB collections check in CI
- Address
noExplicitAnywarnings (133 in payment service)
Long Term
- Add route usage metrics (Prometheus)
- Detect routes with 0 hits in 30 days
- Set up quarterly cleanup review
Version: 1.0.0 Implementation Time: ~5 hours Codebase Health: 9/10