npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

code-review-analyzer

v1.10.4

Published

Automated code review tool that analyzes Git branch differences, detects code quality issues, and suggests test cases

Readme

Code Review Analyzer

一个自动化的代码审查工具,用于分析 Git 分支差异、检测代码质量问题、识别安全漏洞、分析代码复杂度,并提供测试建议。

English | 简体中文

特性

  1. Git 差异分析

    • 分析两个分支之间的代码差异
    • 识别添加、修改和删除的文件和代码
  2. 变更影响分析

    • 分析变更如何影响现有功能
    • 检测接口变更、依赖关系修改和潜在的破坏性变更
  3. 代码质量检查

    • 使用 ESLint 识别代码质量问题
    • 检测代码重复和复杂性问题
  4. 代码复杂度分析

    • 计算循环复杂度和认知复杂度
    • 识别过于复杂的函数和方法
  5. 安全漏洞检测

    • 发现代码变更中的潜在安全问题
    • 检测硬编码的密钥、SQL 注入漏洞、XSS 问题等
  6. 依赖漏洞检查

    • 检查项目依赖中的已知漏洞
    • 提供修复建议和漏洞详情
  7. 重要变更标记

    • 标记超过可配置阈值的变更
    • 突出显示核心逻辑文件的变更
    • 识别需要特别注意的关键模式
  8. 测试建议生成

    • 基于代码变更生成测试用例建议
    • 提供 Jest 或 Mocha 的测试代码模板
  9. 交互式 HTML 报告

    • 生成可过滤、可排序的 HTML 报告
    • 提供问题的详细信息和代码片段
    • 显示问题的状态(新增或修改)
  10. AI 辅助分析

  • 支持多种 AI 提供商(OpenAI、阿里云通义千问、百度文心一言、DeepSeek)
  • 智能检测安全漏洞和代码质量问题
  • 提供代码优化建议

要求

  • Node.js >= 14.0.0
  • Git

安装

# 从 npm 安装
npm install -g code-review-analyzer

# 或者从 GitHub 克隆并安装
git clone https://github.com/yourusername/code-review-analyzer.git
cd code-review-analyzer
npm install
npm link

使用方法

基本用法

# 使用默认配置比较当前分支与 master 分支
code-review

# 比较特定分支
code-review --source feature/login --target develop

# 生成 HTML 报告
code-review --output html

# 使用自定义配置文件
code-review --config ./my-config.js

高级用法

# 使用合并基础比较模式
code-review --diff-mode merge-base

# 源分支到目标分支的比较方向
code-review --direction source-to-target

# 禁用依赖分析
code-review --disable-analyzers dependency

# 不忽略空白字符差异
code-review --ignore-whitespace false

# 设置重要变更的阈值
code-review --threshold 30

# 指定报告输出目录
code-review --output html --output-dir ./reports

# 不自动打开 HTML 报告
code-review --output html --no-open-browser

# 启用详细日志
code-review --verbose

命令行选项

| 选项 | 简写 | 描述 | 默认值 | 示例 | |------|------|------|--------|------| | --source | -s | 源分支名称 | 当前分支 | --source feature-branch | | --target | -t | 目标分支名称 | master | --target main | | --config | -c | 配置文件路径 (.js 或 .json) | 自动查找 | --config ./code-review.config.js | | --output | -o | 输出格式 (console, html, json) | console | --output html | | --verbose | -v | 启用详细输出 | false | --verbose | | --threshold | - | 显著变更的阈值 | 50 | --threshold 30 | | --output-dir | - | 保存 HTML/JSON 输出的目录 | ./reports | --output-dir ./review-reports | | --no-open-browser | - | 不自动打开 HTML 报告 | false | --no-open-browser | | --direction | - | 比较方向 | target-to-source | --direction source-to-target | | --diff-mode | - | 差异比较模式 | merge-base | --diff-mode direct | | --ignore-whitespace | - | 忽略空白字符差异 | true | --ignore-whitespace false | | --ignore-blank-lines | - | 忽略空行差异 | true | --ignore-blank-lines false | | --disable-analyzers | - | 禁用特定分析器 | - | --disable-analyzers dependency,security | | --ai-provider | - | AI 提供商 (auto, openai, aliyun, baidu, deepseek) | auto | --ai-provider aliyun | | --ai-model | - | AI 模型名称 | - | --ai-model qwen-max | | --disable-ai | - | 禁用 AI 分析 | false | --disable-ai |

配置文件

配置文件格式

代码审查工具支持两种格式的配置文件:

  1. JavaScript 格式(推荐)code-review.config.js
  2. JSON 格式(兼容)code-review.json

工具会按照以下顺序查找配置文件:

  1. 命令行指定的配置文件
  2. 项目根目录下的 code-review.config.js
  3. 项目根目录下的 .code-review.config.js
  4. 项目 config 目录下的 code-review.config.js
  5. 项目根目录下的 code-review.json
  6. 项目根目录下的 .code-review.json
  7. 项目 config 目录下的 code-review.json

配置项说明

分析器配置 (analyzers)

| 配置项 | 类型 | 描述 | 默认值 | 示例 | |--------|------|------|--------|------| | analyzers.impact.enabled | 布尔值 | 启用/禁用影响分析 | true | enabled: true | | analyzers.impact.severity | 对象 | 影响严重程度阈值 | { high: 70, medium: 40, low: 10 } | severity: { high: 80, medium: 50, low: 20 } | | analyzers.quality.enabled | 布尔值 | 启用/禁用代码质量分析 | true | enabled: true | | analyzers.quality.minDuplicateLines | 数字 | 最小重复行数 | 4 | minDuplicateLines: 5 | | analyzers.quality.complexityThreshold | 数字 | 复杂度阈值 | 30 | complexityThreshold: 25 | | analyzers.security.enabled | 布尔值 | 启用/禁用安全分析 | true | enabled: true | | analyzers.dependency.enabled | 布尔值 | 启用/禁用依赖分析 | true | enabled: false | | analyzers.complexity.enabled | 布尔值 | 启用/禁用复杂度分析 | true | enabled: true | | analyzers.ai.enabled | 布尔值 | 启用/禁用 AI 分析 | true | enabled: true | | analyzers.ai.provider | 字符串 | AI 提供商 | auto | provider: 'openai' | | analyzers.ai.openai.apiKeyEnvVar | 字符串 | OpenAI API 密钥环境变量 | OPENAI_API_KEY | apiKeyEnvVar: 'MY_OPENAI_KEY' | | analyzers.ai.aliyun.accessKeyIdEnvVar | 字符串 | 阿里云 AccessKey ID 环境变量 | ALIYUN_ACCESS_KEY_ID | accessKeyIdEnvVar: 'MY_ALIYUN_ID' | | analyzers.ai.baidu.apiKeyEnvVar | 字符串 | 百度 API Key 环境变量 | BAIDU_API_KEY | apiKeyEnvVar: 'MY_BAIDU_KEY' | | analyzers.ai.deepseek.apiKeyEnvVar | 字符串 | DeepSeek API 密钥环境变量 | DEEPSEEK_API_KEY | apiKeyEnvVar: 'MY_DEEPSEEK_KEY' |

报告配置 (reporting)

| 配置项 | 类型 | 描述 | 默认值 | 示例 | |--------|------|------|--------|------| | reporting.defaultFormat | 字符串 | 默认输出格式 | console | defaultFormat: 'html' | | reporting.openBrowser | 布尔值 | 自动打开 HTML 报告 | true | openBrowser: false | | reporting.language | 字符串 | 报告语言 | zh-CN | language: 'en-US' | | reporting.colorScheme | 字符串 | 报告颜色方案 | default | colorScheme: 'dark' | | reporting.showLineNumbers | 布尔值 | 显示行号 | true | showLineNumbers: true | | reporting.showCodeSnippets | 布尔值 | 显示代码片段 | true | showCodeSnippets: true |

Git 配置 (git)

| 配置项 | 类型 | 描述 | 默认值 | 示例 | |--------|------|------|--------|------| | git.defaultTargetBranch | 字符串 | 默认目标分支 | master | defaultTargetBranch: 'main' | | git.diffMode | 字符串 | 差异比较模式 | merge-base | diffMode: 'direct' | | git.ignoreWhitespace | 布尔值 | 忽略空白字符差异 | true | ignoreWhitespace: false | | git.ignoreBlankLines | 布尔值 | 忽略空行差异 | true | ignoreBlankLines: false | | git.ignorePatterns | 字符串数组 | 忽略的文件模式 | ['package-lock.json', 'yarn.lock', 'dist/', 'build/'] | ignorePatterns: ['package.json', 'node_modules/**'] |

忽略规则 (ignoreRules)

| 配置项 | 类型 | 描述 | 默认值 | 示例 | |--------|------|------|--------|------| | ignoreRules.duplicateCode | 对象数组 | 重复代码忽略规则 | [] | duplicateCode: [{ pattern: 'defaults\\.[\\w\\.\\[\\]\\'\\"]+\\s*=\\s*[^;]+;\\s*' }] |

阈值配置 (thresholds)

| 配置项 | 类型 | 描述 | 默认值 | 示例 | |--------|------|------|--------|------| | thresholds.significantChanges | 数字 | 显著变更的阈值 | 50 | significantChanges: 30 | | thresholds.complexFunction | 数字 | 复杂函数的阈值 | 30 | complexFunction: 25 | | thresholds.deepNesting | 数字 | 深嵌套的阈值 | 5 | deepNesting: 4 |

JavaScript 格式配置文件示例

// code-review.config.js
module.exports = {
  analyzers: {
    impact: {
      enabled: true,
      severity: { high: 70, medium: 40, low: 10 }
    },
    quality: {
      enabled: true,
      minDuplicateLines: 4,
      complexityThreshold: 30
    },
    security: { enabled: true },
    dependency: { enabled: false },
    ai: {
      enabled: true,
      provider: 'auto', // 'auto', 'openai', 'aliyun', 'baidu', 'deepseek'
      openai: {
        apiKeyEnvVar: "OPENAI_API_KEY",
        model: "gpt-4o"
      },
      aliyun: {
        accessKeyIdEnvVar: "ALIYUN_ACCESS_KEY_ID",
        accessKeySecretEnvVar: "ALIYUN_ACCESS_KEY_SECRET",
        model: "qwen-max"
      },
      baidu: {
        apiKeyEnvVar: "BAIDU_API_KEY",
        secretKeyEnvVar: "BAIDU_SECRET_KEY",
        model: "ernie-bot-4"
      },
      deepseek: {
        apiKeyEnvVar: "DEEPSEEK_API_KEY",
        model: "deepseek-chat"
      }
    }
  },
  reporting: {
    defaultFormat: 'html',
    openBrowser: true,
    language: 'zh-CN'
  },
  git: {
    defaultTargetBranch: 'master',
    diffMode: 'merge-base',
    ignoreWhitespace: true,
    ignoreBlankLines: true,
    ignorePatterns: [
      'package.json',
      'package-lock.json',
      'node_modules/**',
      'dist/**'
    ]
  },
  ignoreRules: {
    duplicateCode: [
      { pattern: 'defaults\\.[\\w\\.\\[\\]\\'\\"]+\\s*=\\s*[^;]+;\\s*' },
      { pattern: 'meta:\\s*{[\\s\\S]*?title:[\\s\\S]*?}' }
    ]
  },
  thresholds: {
    significantChanges: 50,
    complexFunction: 30,
    deepNesting: 5
  }
};

输出格式

工具支持多种输出格式:

  • HTML:交互式 HTML 报告(默认)
    • 显示概要统计和图表
    • 提供详细的问题信息和代码片段
    • 包含测试建议和代码示例
    • 支持按严重程度、状态等过滤和排序
  • JSON:机器可读的 JSON 格式
    • 适用于集成到其他工具或系统

报告示例

HTML 报告包含以下部分:

  1. 分支对比:显示分支差异的摘要信息
  2. 问题统计:按严重程度统计问题数量
  3. 问题列表:可过滤、可排序的问题列表
  4. 重要变更:重要代码变更的列表
  5. 测试建议:测试建议的列表

常见问题

1. 为什么我的 package.json 文件总是被检测?

默认情况下,工具会分析所有变更的文件,包括 package.json。如果您不希望分析 package.json 文件,可以通过以下方式解决:

// 在配置文件中添加忽略规则
git: {
  ignorePatterns: ['package.json', 'package-lock.json']
}

或者使用命令行:

code-review --disable-analyzers dependency

2. 为什么工具报告了重复代码,但实际上不是重复的?

工具的重复代码检测可能会对某些模式过于敏感。您可以通过添加忽略规则来解决这个问题:

ignoreRules: {
  duplicateCode: [
    { pattern: 'defaults\\.[\\w\\.\\[\\]\\'\\"]+\\s*=\\s*[^;]+;\\s*' }
  ]
}

3. Git 比较和 GitHub 合并显示不同的结果?

这是因为 Git 比较和 GitHub 合并使用不同的比较方法。您可以使用 --diff-mode merge-base 选项使工具的比较行为更接近 GitHub 合并:

code-review --diff-mode merge-base

4. 如何在 CI/CD 流程中使用?

您可以在 CI/CD 流程中使用以下命令:

# 在 CI/CD 中使用
code-review --source $CI_COMMIT_BRANCH --target main --output json --no-open-browser

然后解析 JSON 输出,根据结果决定是否继续流程。

5. npm audit 命令报错:"requires an existing lockfile"

如果您遇到以下错误:

npm ERR! code ENOLOCK
npm ERR! audit This command requires an existing lockfile.

这是因为 npm audit 命令需要一个锁定文件(package-lock.json 或 npm-shrinkwrap.json)才能运行。您可以通过以下方式解决:

# 创建 package-lock.json 文件
npm i --package-lock-only

或者禁用依赖分析:

code-review --disable-analyzers dependency

6. 如何使用不同的 AI 提供商?

工具支持多种 AI 提供商,您可以通过以下方式使用:

  1. 通过命令行指定:
# 使用 OpenAI
code-review --ai-provider openai

# 使用阿里云通义千问
code-review --ai-provider aliyun

# 使用百度文心一言
code-review --ai-provider baidu

# 使用 DeepSeek
code-review --ai-provider deepseek

# 指定模型
code-review --ai-provider openai --ai-model gpt-4
  1. 通过环境变量设置密钥:
# OpenAI
export OPENAI_API_KEY=your_openai_api_key

# 阿里云
export ALIYUN_ACCESS_KEY_ID=your_aliyun_access_key_id
export ALIYUN_ACCESS_KEY_SECRET=your_aliyun_access_key_secret

# 百度
export BAIDU_API_KEY=your_baidu_api_key
export BAIDU_SECRET_KEY=your_baidu_secret_key

# DeepSeek
export DEEPSEEK_API_KEY=your_deepseek_api_key
  1. 在配置文件中设置:
module.exports = {
  analyzers: {
    ai: {
      enabled: true,
      provider: 'aliyun',
      aliyun: {
        accessKeyId: 'your_aliyun_access_key_id',
        accessKeySecret: 'your_aliyun_access_key_secret'
      }
    }
  }
};

贡献

欢迎贡献!请随时提交 Pull Request。

许可证

本项目采用 MIT 许可证。