@daben/dead-code-checker
v1.0.1
Published
CLI tool to check for dead code in JavaScript and TypeScript files
Downloads
8
Maintainers
Readme
Dead Code Checker
一个基于ESLint的命令行工具,用于检查JavaScript和TypeScript项目中的死代码。
功能特点
- 检测未使用的变量、导入和函数
- 检测不可达代码
- 检测无用的条件表达式
- 检测空代码块
- 支持JavaScript和TypeScript文件(.js, .jsx, .ts, .tsx)
- 递归分析指定目录下的所有符合条件的文件
- 生成详细的分析报告,包含问题位置的精确坐标
- 支持多种输出格式(文本和JSON)
安装
npm install -g dead-code-checker或者在项目中本地安装:
npm install --save-dev dead-code-checker使用方法
命令行使用
# 检查指定目录
dcc ./src
# 生成JSON格式的报告并输出到文件
dcc ./src --format json --output report.json
# 忽略特定文件或目录
dcc ./src --ignore "**/*.test.js,**/__tests__/**"命令行选项
<directory>: 要检查的目录路径(必需)-o, --output <file>: 输出报告的文件路径(可选)-f, --format <format>: 输出格式,支持text或json(默认:text)-i, --ignore <patterns>: 忽略的文件模式,用逗号分隔(可选)-v, --version: 显示版本号-h, --help: 显示帮助信息
报告示例
文本格式
死代码检查报告
总结:
检查的文件数: 3
发现的问题数: 19
src/analyzer.js
[未使用的变量或参数] src/analyzer.js:106
examples/test-files/example.js
[未使用的变量或参数] examples/test-files/example.js:4
[未使用的变量或参数] examples/test-files/example.js:5
[未使用的变量或参数] examples/test-files/example.js:8
[未使用的变量或参数] examples/test-files/example.js:11
[不可达代码] examples/test-files/example.js:20
[常量条件] examples/test-files/example.js:24
[未使用的变量或参数] examples/test-files/example.js:25
[未使用的变量或参数] examples/test-files/example.js:28
[未使用的变量或参数] examples/test-files/example.js:34
[未使用的变量或参数] examples/test-files/example.js:37
examples/test-files/a.jsx
[未使用的变量或参数] examples/test-files/a.jsx:36
[未使用的表达式] examples/test-files/a.jsx:132
[未使用的变量或参数] examples/test-files/a.jsx:196
[未使用的变量或参数] examples/test-files/a.jsx:256
[未使用的变量或参数] examples/test-files/a.jsx:285
[未使用的变量或参数] examples/test-files/a.jsx:288
[常量条件] examples/test-files/a.jsx:301
[不可达代码] examples/test-files/a.jsx:336JSON格式
{
"summary": {
"totalFiles": 3,
"totalIssues": 5
},
"files": [
{
"filePath": "/path/to/project/src/components/Button.jsx",
"issues": [
{
"ruleId": "unused-imports/no-unused-vars",
"message": "'onClick' is defined but never used.",
"location": {
"startLine": 5,
"endLine": 5,
"startColumn": 12,
"endColumn": 19
},
"severity": "warning"
},
{
"ruleId": "no-unused-expressions",
"message": "Expected an assignment or function call and instead saw an expression.",
"location": {
"startLine": 15,
"endLine": 15,
"startColumn": 3,
"endColumn": 28
},
"severity": "warning"
}
]
},
{
"filePath": "/path/to/project/src/utils/helpers.js",
"issues": [
{
"ruleId": "no-unreachable",
"message": "Unreachable code.",
"location": {
"startLine": 8,
"endLine": 10,
"startColumn": 3,
"endColumn": 4
},
"severity": "error"
},
{
"ruleId": "unused-imports/no-unused-imports",
"message": "'lodash' import is unused.",
"location": {
"startLine": 1,
"endLine": 1,
"startColumn": 1,
"endColumn": 24
},
"severity": "warning"
}
]
}
]
}许可证
MIT
