DEV Community

VANSH ARORA
VANSH ARORA

Posted on

Finding Dead Code Across Multi-File Codebases with AST Call-Graph Reachability

Traditional linters like ESLint do a great job finding unused variables within a single file. But when a function is exported, file-scoped linters give up, assuming some other module might import it.

To detect truly dead functions across an entire project, TokenCap performs global call-graph reachability analysis in src/graph/deadCode.js.

How Reachability is Computed

  1. Symbol Catalog: Tree-sitter parses all functions, methods, and exported declarations.
  2. Call Edge Mapping: Every invocation (call_expression) is resolved to its target symbol.
  3. Root Traversal: Entry points (CLI binaries, route handlers, public package exports) are marked as root nodes.
  4. Reachability Sweep: Any internal function with zero incoming edges from any reachability root is flagged as dead code.

CLI Inspection

tokencap ask impact --dead-code
Enter fullscreen mode Exit fullscreen mode

Output:

Inspecting 94 files across 4 clusters...
Found 6 uncalled internal functions:
  - src/utils/formatters.js:84: formatLegacyCurrency (0 callers)
  - src/services/oldAuth.js:112: hashWithMd5 (0 callers)
  - src/middleware/cors.js:45: legacyCorsFallback (0 callers)
Potential token savings: 1,480 tokens pruned from context snapshots.
Enter fullscreen mode Exit fullscreen mode

By identifying and pruning dead code before passing context to an AI agent, you save token budget and prevent models from utilizing deprecated functions.

Documentation available at tokencap.vansharora.app

Top comments (0)