🦀 ClawHub
Code Refactor
by @yuyonghao-123
Analyze code quality issues, detect code smells, and automatically apply refactoring suggestions with preview, apply, and rollback support.
TERMINAL
clawhub install yuyonghao-code-refactor📖 About This Skill
Code Refactor Skill
智能代码重构助手,用于分析代码质量问题并自动应用重构建议。
功能特性
1. 代码分析 (CodeAnalyzer)
2. 重构引擎 (RefactoringEngine)
3. 变更应用 (ChangeApplier)
4. 测试验证 (TestValidator)
检测的代码异味
1. 长函数 (>50行) 2. 过多参数 (>4个) 3. 重复代码 4. 深层嵌套 (>3层) 5. 魔法数字 6. 未使用变量/导入 7. 复杂条件表达式
使用方法
const { CodeRefactor } = require('./src/index');const refactor = new CodeRefactor();
// 分析代码
const analysis = refactor.analyze('path/to/file.js');
// 查看问题
console.log(analysis.issues);
// 自动重构
const result = refactor.refactor('path/to/file.js', {
dryRun: true // 预览模式
});
// 应用变更
if (result.canApply) {
refactor.applyChanges(result.changes);
}
CLI 使用
# 分析代码
node src/index.js analyze file.js重构代码(dry-run)
node src/index.js refactor file.js --dry-run应用重构
node src/index.js refactor file.js --apply
配置选项
{
maxFunctionLength: 50, // 函数最大行数
maxParameters: 4, // 参数最大数量
maxNestingDepth: 3, // 最大嵌套深度
minDuplicateLines: 5, // 最小重复行数
complexityThreshold: 10 // 复杂度阈值
}
测试
npm test