home / skills / montagao / skills / dead-code-detector

dead-code-detector skill

/dead-code-detector

This skill detects and helps remove dead code in TypeScript/JavaScript projects by identifying unused exports, unused dependencies, and unreachable patterns.

npx playbooks add skill montagao/skills --skill dead-code-detector

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
3.2 KB
---
name: dead-code-detector
description: |
  Find and remove dead code in TypeScript/JavaScript projects. Detects unused exports (functions, types, constants never imported elsewhere), unused npm dependencies (packages in package.json never imported), and unreachable code patterns (code after returns, always-false conditions, stale TODOs). Use when: "find dead code", "check for unused exports", "clean up unused code", "find unused dependencies", "audit codebase for dead code", "what code can I delete", or before major refactors.
---

# Dead Code Detector

Detect and eliminate dead code in TypeScript/JavaScript codebases.

## Quick Start

Run the automated scanner:
```bash
node ~/.claude/skills/dead-code-detector/scripts/find-dead-code.js .
```

Output: Console report + `dead-code-report.json` with all findings.

## What Gets Detected

### 1. Unused Exports
Exported symbols never imported elsewhere in the codebase.

```typescript
// src/utils/helpers.ts
export function formatDate() { ... }  // Used
export function oldFormatter() { ... }  // DEAD - never imported
```

**False positive handling:** Automatically skips:
- Entry points (`index.ts`, `page.tsx`, `convex/` functions)
- Common type suffixes (`Props`, `Config`, `Schema`)
- Default exports (may be dynamically imported)

### 2. Unused Dependencies
Packages in `package.json` never imported anywhere.

```json
{
  "dependencies": {
    "lodash": "^4.17.21",  // DEAD - replaced by native methods
    "react": "^18.0.0"     // Used
  }
}
```

**False positive handling:** Automatically skips:
- Dev tools: typescript, eslint, prettier, vitest, jest
- Type packages: @types/*
- Build tools: webpack, vite, rollup, postcss, tailwindcss

### 3. Unreachable Code Patterns
```typescript
// Code after return
function foo() {
  return 1;
  console.log("dead");  // FLAGGED
}

// Always-false conditions
if (false) { ... }  // FLAGGED

// Stale TODOs
// TODO (2021): Fix this  // FLAGGED as stale
```

## Manual Investigation Workflow

For findings that need verification:

### Check if export is truly unused
```bash
rg "symbolName" --type ts
rg "from.*filename" --type ts  # Check imports of file
```

### Check for dynamic imports
```bash
rg "import\(['\"].*moduleName" --type ts
rg "require\(['\"].*moduleName" --type js
```

### Check if dependency is used in config
```bash
rg "packageName" *.config.* package.json
```

## Framework Considerations

Framework-specific exports that look unused but aren't:

| Framework | Safe to Keep |
|-----------|--------------|
| Next.js | `generateStaticParams`, `generateMetadata`, exports from `page.tsx`/`layout.tsx` |
| Remotion | Compositions in `Root.tsx`, `calculateMetadata` |
| Convex | All `convex/` function exports (deployed via API) |
| Express | Route handlers, middleware (registered, not imported) |

## Cleanup Workflow

1. **Run scanner** on project root
2. **Review findings** - verify each is truly dead
3. **Remove code** - delete unused exports and functions
4. **Remove dependencies** - `npm uninstall <package>`
5. **Run tests** - ensure nothing broke
6. **Commit** - "chore: remove dead code"

## Reference

For detailed patterns and edge cases, see [references/patterns.md](references/patterns.md).

Overview

This skill finds and helps remove dead code in TypeScript and JavaScript projects. It scans for unused exports, unused npm dependencies, and common unreachable-code patterns, then produces a concise report you can review and act on.

How this skill works

The scanner parses the codebase and package.json to identify exported symbols that are never imported, packages declared but not referenced, and unreachable code patterns such as code after returns, always-false conditions, and stale TODO comments. It applies sensible exclusions to avoid common false positives (framework entry points, typical dev tooling, type packages, and known framework-specific exports). Results are written to the console and to a JSON report for manual review.

When to use it

  • find dead code
  • check for unused exports
  • clean up unused code
  • find unused dependencies
  • audit codebase for dead code
  • what code can I delete

Best practices

  • Run the scanner and review findings before deleting anything; false positives are possible for dynamic imports or framework hooks.
  • Verify each unused export by searching for dynamic imports, require calls, framework entry points, or route registration.
  • Exclude common dev and type-only packages before removing dependencies (dev tools, @types/*, build tools).
  • Remove dependencies via your package manager and run tests/builds after cleanup to catch regressions.
  • Keep a small commit that removes dead code so changes are easy to revert if an issue appears.

Example use cases

  • Audit a monorepo before a large refactor to minimize leftover cruft and reduce bundle size.
  • Clean up a legacy codebase by finding functions, types, and constants that are no longer referenced.
  • Identify npm packages in package.json that are safe to uninstall because they are not imported anywhere.
  • Catch simple unreachable patterns (dead statements after return, always-false branches, stale TODOs) to improve code clarity.
  • Verify framework-specific exports (Next.js, Convex, Remotion) are preserved when scanning for unused exports.

FAQ

Will the scanner remove code automatically?

No. It produces a report of findings for manual review and safe removal steps. Manual verification is required before deletion.

How does it avoid false positives for framework exports?

It skips known entry points and framework patterns (index files, page/layout exports, convex/ functions, and common type suffixes) and excludes common dev/type packages.