@chiriwu-wjx/code-quality-plugin
v1.0.1
Published
Webpack plugin for code quality analysis and reporting
Downloads
9
Maintainers
Readme
Code Quality Plugin
一个用于 Webpack 的代码质量分析插件,可以检测代码质量问题、计算复杂度指标并生成详细的质量报告。
功能特性
- 🔍 代码质量分析
- 📊 圈复杂度计算
- 📈 可维护性指数
- 📋 Halstead 复杂度指标
- 🚨 问题检测和报告
- 📄 JSON 格式报告输出
- 🎨 彩色控制台输出
- ⚙️ 可配置的规则和阈值
检测规则
- 行长度检查: 检测超过 120 字符的行
- 圈复杂度检查: 检测过高的圈复杂度
- 未使用变量: 检测声明但未使用的变量
- 魔法数字: 检测代码中的魔法数字
- 可维护性指数: 计算代码的可维护性
- Halstead 体积: 计算代码的 Halstead 复杂度
安装
npm install @chiriwu-wjx/code-quality-plugin --save-dev使用方法
基本用法
// webpack.config.js
const CodeQualityPlugin = require("@chiriwu-wjx/code-quality-plugin");
module.exports = {
// ... 其他配置
plugins: [new CodeQualityPlugin()]
};带配置的用法
// webpack.config.js
const CodeQualityPlugin = require("@chiriwu-wjx/code-quality-plugin");
module.exports = {
// ... 其他配置
plugins: [
new CodeQualityPlugin({
outputPath: "./reports/code-quality.json",
failOnError: true,
failOnWarning: false,
verbose: true,
includePatterns: ["**/*.{js,ts,jsx,tsx}"],
excludePatterns: ["**/node_modules/**", "**/dist/**"],
maxComplexity: 8,
maxMaintainabilityIndex: 70
})
]
};配置选项
| 选项 | 类型 | 默认值 | 描述 |
| ------------------------- | ---------- | ----------------------------------------------------- | ---------------------- |
| outputPath | string | ./code-quality-report.json | 报告输出路径 |
| failOnError | boolean | false | 发现错误时是否终止构建 |
| failOnWarning | boolean | false | 发现警告时是否终止构建 |
| verbose | boolean | true | 是否显示详细输出 |
| includePatterns | string[] | ['**/*.{js,ts,jsx,tsx}'] | 包含的文件模式 |
| excludePatterns | string[] | ['**/node_modules/**', '**/dist/**', '**/build/**'] | 排除的文件模式 |
| maxComplexity | number | 10 | 最大圈复杂度 |
| maxMaintainabilityIndex | number | 65 | 最小可维护性指数 |
输出示例
控制台输出
🔍 Code Quality Analysis Report
============================================================
📁 Files Analyzed: 15
📄 Total Lines: 1250
🔄 Cyclomatic Complexity: 45
📊 Maintainability Index: 78.5
📈 Halstead Volume: 234.7
🚨 Issues Summary:
Errors: 2
Warnings: 8
Info: 12
📋 Detailed Issues:
ERROR src/components/ComplexComponent.js:1:1 - Cyclomatic complexity (15) exceeds maximum (10)
WARNING src/utils/helper.js:45:1 - Line is too long (max 120 characters)
INFO src/constants.js:12:1 - Magic number detected: 1000
============================================================JSON 报告格式
{
"timestamp": "2024-01-15T10:30:00.000Z",
"metrics": {
"totalFiles": 15,
"totalLines": 1250,
"issues": [...],
"errorCount": 2,
"warningCount": 8,
"infoCount": 12,
"maintainabilityIndex": 78.5,
"cyclomaticComplexity": 45,
"halsteadVolume": 234.7
},
"summary": {
"status": "failed",
"totalIssues": 22,
"errorCount": 2,
"warningCount": 8,
"infoCount": 12
}
}开发
# 安装依赖
pnpm install
# 构建
pnpm build
# 开发模式(监听文件变化)
pnpm dev
# 运行测试
pnpm test
# 代码检查
pnpm lint许可证
MIT
