ts-remove-unused-imports
v1.0.2
Published
Tool to remove unused imports in TypeScript and TSX files
Maintainers
Readme
ts-remove-unused-imports
A TypeScript utility to remove unused imports from .ts and .tsx files in your project.
Installation
You can install ts-remove-unused-imports as a development dependency in your project:
npm install --save-dev ts-remove-unused-importsUsage
You can use ts-remove-unused-imports by specifying a directory path where you want to remove unused imports. This utility uses TypeScript and ts-morph under the hood to parse and manipulate TypeScript source files.
Adding to package.json
You can add ts-remove-unused-imports as an npm script in your package.json:
{
"scripts": {
"remove-unused-imports": "ts-remove-unused-imports path/to/your/directory"
}
}Replace path/to/your/directory with the directory path where you want to remove unused imports.
How It Works
Initialization:
ts-remove-unused-importsinitializes a TypeScript Project usingts-morphwith yourtsconfig.jsonsettings.File Processing: It traverses through
.tsand.tsxfiles in the specified directory and its subdirectories.Import Removal: For each file, it identifies and removes unused import statements:
- Named imports are removed if none of their identifiers are used within the file.
- Default and namespace imports are removed similarly if they are unused.
Saving Changes: After removing unused imports, it saves the modified files back to disk.
Example
You can run ts-remove-unused-imports without installing it globally using npx:
npx ts-remove-unused-imports path/to/your/directoryThis command will remove unused imports in the path/to/your/directory and its subdirectories.
Contributing
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or fixes.
License
This project is licensed under the ISC License - see the LICENSE file for details.
Repository
Find this project on GitHub.
