|
| 1 | +# Unused Code Detection |
| 2 | + |
| 3 | +This project includes a static analysis tool to detect potentially unused code and dead files. This helps maintain a clean codebase by identifying exports and files that may no longer be needed. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +The unused code detector analyzes: |
| 8 | +- **Unused Files**: TypeScript files that are never imported by other files |
| 9 | +- **Unused Exports**: Functions, classes, variables, and types that are exported but never imported |
| 10 | +- **VS Code Integration**: Smart detection of VS Code extension patterns like `activate`/`deactivate` functions |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### Command Line |
| 15 | + |
| 16 | +Run the detection script from the command line: |
| 17 | + |
| 18 | +```bash |
| 19 | +npm run detect-unused |
| 20 | +``` |
| 21 | + |
| 22 | +### VS Code Command |
| 23 | + |
| 24 | +1. Open the Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`) |
| 25 | +2. Search for "Zephyr IDE: Detect Unused Code" |
| 26 | +3. Run the command |
| 27 | +4. View results in the Output panel (select "Zephyr IDE" from the dropdown) |
| 28 | + |
| 29 | +## Understanding the Results |
| 30 | + |
| 31 | +### Report Sections |
| 32 | + |
| 33 | +**ποΈ Potentially Unused Files** |
| 34 | +- Lists TypeScript files that are never imported |
| 35 | +- Excludes test files and the main extension entry point |
| 36 | +- These files might be safe to remove, but review carefully |
| 37 | + |
| 38 | +**π€ Potentially Unused Exports** |
| 39 | +- Lists exported functions, classes, variables that are never imported |
| 40 | +- Shows the file, export name, type, and line number |
| 41 | +- Includes smart filtering for VS Code extension patterns |
| 42 | + |
| 43 | +**π Summary** |
| 44 | +- Total files analyzed |
| 45 | +- Count of potentially unused files and exports |
| 46 | + |
| 47 | +### Important Notes |
| 48 | + |
| 49 | +β οΈ **This analysis is static and may show false positives** |
| 50 | + |
| 51 | +The tool cannot detect: |
| 52 | +- Dynamic imports using `import()` statements |
| 53 | +- Reflection-based usage |
| 54 | +- External references from other packages |
| 55 | +- Command handlers registered via strings in package.json |
| 56 | +- Functions called via VS Code's command system |
| 57 | + |
| 58 | +### Best Practices |
| 59 | + |
| 60 | +1. **Review before removing**: Always manually verify that flagged code is truly unused |
| 61 | +2. **Check git history**: Look at recent usage patterns before removing old code |
| 62 | +3. **Consider API boundaries**: Some exports might be intended for external use |
| 63 | +4. **Test thoroughly**: After removing code, run all tests and verify functionality |
| 64 | + |
| 65 | +## Technical Details |
| 66 | + |
| 67 | +The detection script: |
| 68 | +1. Scans all `.ts` files in the `src` directory |
| 69 | +2. Parses import/export statements using regex patterns |
| 70 | +3. Builds a dependency graph |
| 71 | +4. Identifies unused files and exports |
| 72 | +5. Applies VS Code extension-specific filtering rules |
| 73 | + |
| 74 | +### Limitations |
| 75 | + |
| 76 | +- Does not use TypeScript compiler API for performance reasons |
| 77 | +- Regex-based parsing may miss complex import/export patterns |
| 78 | +- Cannot detect runtime-only dependencies |
| 79 | +- May not catch all dynamic usage patterns |
| 80 | + |
| 81 | +## Example Output |
| 82 | + |
| 83 | +``` |
| 84 | +π UNUSED CODE DETECTION REPORT |
| 85 | +================================================== |
| 86 | +
|
| 87 | +ποΈ POTENTIALLY UNUSED FILES: |
| 88 | + β
No unused files detected! |
| 89 | +
|
| 90 | +π€ POTENTIALLY UNUSED EXPORTS: |
| 91 | +
|
| 92 | + π utilities/utils.ts: |
| 93 | + πΈ unusedHelper (declaration, line 42) |
| 94 | + πΈ oldFunction (declaration, line 156) |
| 95 | +
|
| 96 | +π SUMMARY: |
| 97 | + π Total files analyzed: 36 |
| 98 | + ποΈ Potentially unused files: 0 |
| 99 | + π€ Potentially unused exports: 2 |
| 100 | +``` |
| 101 | + |
| 102 | +This report suggests reviewing `unusedHelper` and `oldFunction` in the utils file to determine if they can be safely removed. |
0 commit comments