← 返回
未分类

Code Refactor

Analyze code quality issues, detect code smells, and automatically apply refactoring suggestions with preview, apply, and rollback support.
分析代码质量问题,检测代码异味,自动提供重构建议并支持预览、应用和回滚。
yuyonghao-123 yuyonghao-123 来源
未分类 clawhub v0.1.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 366
下载
💾 0
安装
1
版本
#latest

概述

Code Refactor Skill

智能代码重构助手,用于分析代码质量问题并自动应用重构建议。

功能特性

1. 代码分析 (CodeAnalyzer)

  • 检测代码异味 (Code Smells)
  • 计算复杂度指标(圈复杂度、认知复杂度)
  • 识别重复代码
  • 检查代码规范

2. 重构引擎 (RefactoringEngine)

  • 提取函数/方法
  • 重命名变量/函数
  • 简化条件表达式
  • 消除重复代码
  • 优化导入/依赖

3. 变更应用 (ChangeApplier)

  • 应用重构变更
  • 生成 diff 预览
  • 支持 dry-run 模式
  • 回滚功能

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

版本历史

共 1 个版本

  • v0.1.0 当前
    2026-05-03 09:23 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 679 📥 327,936
dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 72 📥 182,007
dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 195 📥 67,724