DeadCode Under the Hood: How Static Analysis Finds Dead React/Next.js Code
Regex-based source scanning, conservative re-export handling, and practical review guidance for React/Next.js projects.
The current DeadCode implementation matches source patterns with Python regular expressions. It collects export/import names, route paths, CSS class usage and component names, then reports potential cleanup candidates. This overview follows the pinned scanner implementation and console entry point.
Discovery and Pattern Matching
The scanner walks the project directory and selects TypeScript, TSX, JavaScript, JSX, CSS and SCSS files. Built-in exclusions include node_modules, .git, .next, dist, build and generated/cache directories. Configured gitignore-style ignore and include patterns control which paths are scanned.
Compiled regex patterns recognize common export declarations, named imports, namespace imports, side-effect imports, re-exports, JSX class names and CSS-module accessors. The scan builds maps of names and files from those matches. It does not perform TypeScript type checking or prove runtime reachability.
Re-Exports Are Handled Conservatively
Named re-exports count as uses of the forwarded symbol. For a relative wildcard re-export such as export * from './utils', the scanner attempts to resolve the source file and protects its exports as part of the public API surface. This avoids offering those forwarded exports for removal, even when their downstream consumers are not visible in the scanned project.
This is a safeguard, not exhaustive symbol resolution. Complex aliases, generated modules and runtime behavior still need human review.
The Four Finding Categories
| Category | What the scanner checks |
|---|---|
| Unused export | Export names against collected imports and re-export safeguards |
| Dead route | Recognized route files against literal route-reference patterns |
| Orphaned CSS | CSS class definitions against recognized class-name and CSS-module usage |
| Unreferenced component | Recognized component names against collected imported names |
These are the scanner's four reported categories. It does not expose separate unused-import or dead-re-export finding categories.
Limits and Review
Regex-based detection can produce false positives and false negatives. Dynamic routes, generated class names and unusual source syntax may not match the scanner's patterns. A route may also be reached by an external client. A finding is a reason to investigate, not proof that deletion is safe.
Review each proposed change, use the dry-run preview and run your application tests after cleanup. Keep changes in version control so that they can be inspected and reverted.
A Safe Local Workflow
# Scan a specific project. --project is a global option.
deadcode --project ./my-app scan
# Inspect proposed removals without changing files
deadcode --project ./my-app remove --dry-run
The removal command contains additional safeguards, including conservative handling of source lines it cannot safely remove. Inspect the preview before running removal. Measure scan time on your own representative project if performance matters.
Try It Yourself
Clean up your codebase
Install DeadCode and scan your React/Next.js project — no config file needed.
pip install git+https://github.com/Coding-Dev-Tools/deadcode.git
cd your-project
deadcode --project ./src scan
View on GitHub →
DeadCode is part of the DevForge developer tool ecosystem — 11 CLI tools built by autonomous AI for autonomous developers.