Dead Code Analysis
Vibesweep's dead code detection uses advanced AST (Abstract Syntax Tree) analysis to find code that's never executed or referenced in your project.
What We Detect
Unused Variables
const API_KEY = process.env.KEY; // ✓ Used
const UNUSED_CONFIG = { limit: 100 }; // ✗ Never referenced
Unreachable Code
function processData(data) {
if (!data) {
return null;
}
return data;
// Dead code detected - after return
console.log('Processing complete');
}
Unused Functions
// ✗ Function never called anywhere
function calculateLegacyScore(value) {
return value * 0.75;
}
// ✓ Function is exported/used
export function calculateScore(value) {
return value * 0.85;
}
Unused Imports
// ✗ Lodash imported but never used
import _ from 'lodash';
import { useState } from 'react'; // ✓ Used in component
How It Works
- AST Parsing: We parse your code into an Abstract Syntax Tree
- Reference Tracking: Build a graph of all references and dependencies
- Usage Analysis: Identify which code paths are actually executed
- Export Analysis: Check if unused code is exported for external use
- Safe Detection: Never flag code that might be used dynamically
Configuration
Configure dead code detection in .vibesweeprc.json
:
{
"deadCode": {
"enabled": true,
"includeExports": false,
"ignorePatterns": [
"__tests__/**",
"*.test.js"
],
"whitelist": [
"CONFIG_*", // Keep all CONFIG_ prefixed variables
"legacy*" // Keep legacy functions
]
}
}
Best Practices
1. Regular Cleanup
Run dead code analysis weekly to prevent accumulation:
npx vibesweep analyze . --only-dead-code
2. CI Integration
Add to your CI pipeline to catch dead code before merge:
- name: Check for dead code
run: npx vibesweep analyze . --fail-on-dead-code
3. Gradual Removal
For large codebases, remove dead code incrementally:
# Start with the safest removals
npx vibesweep fix . --only-imports --dry-run
Common False Positives
Dynamic Imports
// May be flagged incorrectly
const module = await import(`./${moduleName}.js`);
Framework Magic
// Next.js API routes - whitelist these
export default function handler(req, res) {
res.json({ status: 'ok' });
}
Test Utilities
// Test helpers might appear unused
export function mockAPI() {
return { data: 'test' };
}
Pro Features
With Vibesweep Pro, get enhanced dead code detection:
- Cross-file analysis: Track usage across your entire monorepo
- Dynamic usage detection: Smarter detection of runtime usage
- Safe auto-removal: Automatically remove dead code with confidence
- Historical tracking: See dead code trends over time
Related
- Safe Fix (Beta) - Automatically remove dead code
- CLI: analyze - Run dead code analysis
- Configuration - Customize detection rules