本文目录导读:

- Python - isort + autoflake
- JavaScript/TypeScript - ESLint
- Java - IntelliJ IDEA 插件
- Go - gofmt + staticcheck
- 通用方法 - 使用脚本批量处理
- IDE内置功能
- Git Hooks 自动清理
- CI/CD集成
- 推荐工作流
Python - isort + autoflake
# 安装
pip install isort autoflake
# 清理单个文件
autoflake --in-place --remove-all-unused-imports file.py
isort file.py
# 批量清理所有Python文件
find . -name "*.py" -exec autoflake --in-place --remove-all-unused-imports {} \;
find . -name "*.py" -exec isort {} \;
JavaScript/TypeScript - ESLint
# 安装
npm install -g eslint eslint-plugin-unused-imports
# ESLint配置
// .eslintrc.json
{
"plugins": ["unused-imports"],
"rules": {
"no-unused-vars": "off",
"unused-imports/no-unused-imports": "error"
}
}
# 批量修复
eslint --fix "src/**/*.{js,jsx,ts,tsx}"
Java - IntelliJ IDEA 插件
# 使用IntelliJ IDEA的"Optimize Imports"功能
# 快捷键: Ctrl+Alt+O (Windows) / Cmd+Option+O (Mac)
# 或使用Maven插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<includes>**/*.java</includes>
</configuration>
</plugin>
Go - gofmt + staticcheck
# 安装 go install honnef.co/go/tools/cmd/staticcheck@latest # 清理未使用导入 go fix ./... go fmt ./... staticcheck -checks="U1000" ./...
通用方法 - 使用脚本批量处理
# batch_clean_imports.py
import os
import subprocess
def clean_python_imports(root_dir):
"""清理Python项目中的冗余导入"""
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith('.py'):
filepath = os.path.join(dirpath, filename)
# 使用autoflake清理
subprocess.run([
'autoflake',
'--in-place',
'--remove-all-unused-imports',
filepath
])
# 排序导入
subprocess.run(['isort', filepath])
def clean_js_imports(root_dir):
"""清理JavaScript/TypeScript项目中的冗余导入"""
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith(('.js', '.jsx', '.ts', '.tsx')):
filepath = os.path.join(dirpath, filename)
subprocess.run([
'npx', 'eslint', '--fix',
filepath
])
if __name__ == "__main__":
project_root = "./src"
clean_python_imports(project_root)
IDE内置功能
| IDE | 快捷键/功能 |
|---|---|
| VS Code | Alt+Shift+O (优化导入) |
| IntelliJ IDEA | Ctrl+Alt+O |
| Eclipse | Ctrl+Shift+O |
| Visual Studio | Ctrl+R, Ctrl+G |
| Vim/Neovim | ALEFix (需配置ALE插件) |
Git Hooks 自动清理
# .git/hooks/pre-commit
#!/bin/bash
echo "Running import cleanup..."
# Python
find . -name "*.py" -exec autoflake --in-place --remove-all-unused-imports {} \;
find . -name "*.py" -exec isort {} \;
# JavaScript/TypeScript
npx eslint --fix "src/**/*.{js,jsx,ts,tsx}"
git add -u
CI/CD集成
# .github/workflows/clean-imports.yml
name: Clean Unused Imports
on: [push, pull_request]
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Clean Python imports
run: |
pip install autoflake isort
find . -name "*.py" -exec autoflake --in-place --remove-all-unused-imports {} \;
find . -name "*.py" -exec isort {} \;
- name: Clean JS imports
run: |
npm install -g eslint eslint-plugin-unused-imports
npx eslint --fix "src/**/*.{js,jsx,ts,tsx}"
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
推荐工作流
- 配置自动化工具:在项目中配置所需的工具
- 添加到package.json:
{ "scripts": { "clean-imports": "autoflake --in-place --remove-all-unused-imports src/**/*.py && isort src/**/*.py", "precommit": "npm run clean-imports" } } - 定期执行:在开发流程中定期运行清理命令
选择合适的工具取决于你的技术栈,建议在项目初始化时就配置好这些自动化工具,避免冗余导入积累。