Safely Pruning Dead Code and Dangling Imports with AST Transforms
Deleting obsolete code manually is tedious. You delete the function body, then delete the export statement, run tests, see an unused import warning, and delete the import statement. In TokenCap, we automated this workfl
Deleting obsolete code manually is tedious. You delete the function body, then delete the export statement, run tests, see an unused import warning, and delete the import statement.
In TokenCap, we automated this workflow via tokencap refactor rm-dead.
How Atomic Pruning Works
- Reachability Confirmation: The target symbol is verified against the call graph to confirm it has zero active callers.
- AST Node Removal: Tree-sitter coordinates locate the exact declaration statement node, removing it without disrupting neighboring indentation.
- Import Sweep: TokenCap inspects the file imports. If an imported identifier was only used inside the pruned function, its specifier is removed from the import statement automatically.
- Formatting Cleanup: Empty lines and commas are reconciled cleanly.
CLI Example
# Preview dead code removal safely
tokencap refactor rm-dead src/utils/formatters.js:formatLegacyCurrency --dry-run
Output:
Target: formatLegacyCurrency (src/utils/formatters.js:84-112)
Callers: 0 verified
Action:
- Remove function declaration (lines 84-112)
- Prune unused import: import { oldCurrencyMap } from "./currencyConfig"
Dry run complete: 1 function and 1 unused import pruned. 0 syntax errors.
When you are satisfied with the dry run, omit --dry-run to apply changes directly to disk.
Explore the refactoring tools at tokencap.vansharora.app
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.