code-dedup

code-dedup

简单的代码格式化和去重工具,支持中文界面,帮助整理代码风格和查找重复代码

1звезд
0форков
Обновлено 1/24/2026
SKILL.md
readonlyread-only
name
code-dedup
description

简单的代码格式化和去重工具,支持中文界面,帮助整理代码风格和查找重复代码

代码去重和格式化工具

一个简单易用的代码格式化和重复代码检测工具,支持中文界面。

什么时候使用这个技能

当你需要以下功能时使用此工具:

  • 格式化代码文件,统一缩进和代码风格
  • 查找项目中的重复代码块
  • 提交代码前清理代码
  • 保持项目代码风格一致

主要功能

  • 代码格式化:支持 JavaScript、TypeScript、Python、JSON 的基础格式化
  • 重复代码检测:使用哈希比对查找重复代码块
  • 多语言支持:JavaScript、TypeScript、Python、JSON
  • 简洁命令:简单易用的命令行界面

快速开始

基本用法

格式化一个目录:

node src/cli.js format ./src

查找重复代码:

node src/cli.js dedup ./src

格式化并检查重复:

node src/cli.js check ./src

格式化选项

# 自定义缩进大小
node src/cli.js format ./src -i 4

# 使用制表符代替空格
node src/cli.js format ./src -t tab

# 忽略特定目录
node src/cli.js format ./src --ignore "node_modules/**,dist/**"

去重选项

# 设置代码块最小行数
node src/cli.js dedup ./src -m 5

# 比较时保留空格
node src/cli.js dedup ./src --ignore-whitespace

# 比较时保留注释
node src/cli.js dedup ./src --keep-comments

代码中使用

代码格式化器

import { CodeFormatter } from './src/index.js';

const formatter = new CodeFormatter({
  indentSize: 2,      // 缩进大小
  indentType: 'space' // 缩进类型:space 或 tab
});

// 格式化单个文件
await formatter.formatFile('./src/example.js');

// 格式化整个目录
const results = await formatter.formatDirectory('./src');
console.log(`已格式化 ${results.length} 个文件`);

代码去重器

import { SimpleDeduper } from './src/index.js';

const deduper = new SimpleDeduper({
  minLines: 3,              // 代码块最小行数
  ignoreWhitespace: true,   // 忽略空格差异
  ignoreComments: true      // 忽略注释差异
});

// 扫描重复代码
await deduper.scan('./src');

// 获取统计信息
const stats = deduper.getStatistics();
console.log(`发现 ${stats.duplicateGroups} 组重复代码`);

// 生成报告
console.log(deduper.generateReport());

命令说明

format - 格式化代码

code-dedup format <路径> [选项]

选项:
  -i, --indent-size <数字>   缩进大小 [默认: 2]
  -t, --indent-type <类型>   缩进类型: space(空格) 或 tab(制表符) [默认: space]
  --ignore <模式>            忽略的文件模式(逗号分隔)

dedup - 查找重复

code-dedup dedup <路径> [选项]

选项:
  -m, --min-lines <数字>    代码块最小行数 [默认: 3]
  --ignore-whitespace       比较时不忽略空格
  --keep-comments           比较时保留注释
  -i, --ignore <模式>       忽略的文件模式(逗号分隔)

check - 综合检查

code-dedup check <路径> [选项]

选项:
  -i, --ignore <模式>       忽略的文件模式(逗号分隔)

格式化功能

代码格式化器会自动处理:

  • 行尾空格:删除所有行尾的空格
  • 多余空行:最多保留 1 个空行(Python 保留 2 个)
  • 文件末尾:确保文件以换行符结尾
  • 运算符间距:规范化运算符周围的空格
  • 关键字间距:在关键字后添加空格(if/else/for 等)
  • JSON 文件:按指定缩进美化输出

去重原理

  1. 代码分块:将文件拆分成多个代码块
  2. 标准化:移除注释和空格
  3. 哈希比对:计算每个代码块的 MD5 哈希值
  4. 重复检测:找出哈希值相同的代码块并报告

使用示例

示例 1:代码格式化

格式化前:

function calculate(  x,y  ){
return x+y;
}

格式化后:

function calculate(x, y) {
  return x + y;
}

示例 2:查找重复代码

工具会检测到这两个函数是重复的:

// file1.js
function sum(a, b) {
  return a + b;
}

// file2.js
function add(x, y) {
  return x + y;
}

输出:

重复代码组(出现 2 次):
  - file1.js:1-3
  - file2.js:1-3

使用建议

  1. 提交前运行:在提交代码前使用此工具确保代码整洁
  2. 集成到 CI/CD:将其加入持续集成流程
  3. 配置 Git 钩子:在 pre-commit 时自动运行
  4. 人工审核:并非所有重复代码都需要删除(请自行判断)

工具限制

  • 仅提供基础格式化(不能完全替代 Prettier/ESLint)
  • 去重基于哈希值(可能会漏掉语义相似但写法不同的代码)
  • 不自动修改重复代码(仅提供报告)

常见问题

Q: 支持哪些编程语言?
A: 目前支持 JavaScript、TypeScript、Python 和 JSON。

Q: 会自动修改我的代码吗?
A: format 命令会直接修改文件,dedup 命令只检测不修改。

Q: 如何跳过某些文件?
A: 使用 --ignore 选项,如 --ignore "node_modules/**,dist/**"

Q: 最小行数是什么意思?
A: 代码块至少包含多少行才会被检测为重复代码,避免误报短代码。

贡献

欢迎贡献代码和提出建议!

许可证

详见 LICENSE.txt

You Might Also Like

Related Skills

coding-agent

coding-agent

179Kdev-codegen

Run Codex CLI, Claude Code, OpenCode, or Pi Coding Agent via background process for programmatic control.

add-uint-support

add-uint-support

97Kdev-codegen

Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.

at-dispatch-v2

at-dispatch-v2

97Kdev-codegen

Convert PyTorch AT_DISPATCH macros to AT_DISPATCH_V2 format in ATen C++ code. Use when porting AT_DISPATCH_ALL_TYPES_AND*, AT_DISPATCH_FLOATING_TYPES*, or other dispatch macros to the new v2 API. For ATen kernel files, CUDA kernels, and native operator implementations.

skill-writer

skill-writer

97Kdev-codegen

Guide users through creating Agent Skills for Claude Code. Use when the user wants to create, write, author, or design a new Skill, or needs help with SKILL.md files, frontmatter, or skill structure.

Implements JavaScript classes in C++ using JavaScriptCore. Use when creating new JS classes with C++ bindings, prototypes, or constructors.

Creates JavaScript classes using Bun's Zig bindings generator (.classes.ts). Use when implementing new JS APIs in Zig with JSC integration.