代码质量有很多指标:
我做了一个脚本可以测出上面的 1, 2, 3
大家都来试一试吧!
npm install -g cqc
cqc [options] <pattern ...>
Examples:
cqc src/**/*.js
Output:
Number of files:            8
Souce lines of code:        357
Duplicate rate:             5.62%
Max complexity:             15
Complexity > 5  (count):    3
Complexity > 10 (count):    1
Complexity > 20 (count):    0
cqc src/**/*.js src/**/*.jsx
--ignore-patterncqc src/**/*.js --ignore-pattern src/vendor/**/*.js
cqc src/**/*.js --ignore-pattern src/vendor/**/*.js,src/third-party/**/*.js
--ignore-filecqc src/**/*.js --ignore-file .gitignore
cqc src/**/*.js --ignore-file .gitignore,.eslintignore
--formatcqc src/**/*.js --format json
Output:
{
    "numberOfFiles": 8,
    "sloc": {
        "source": 357
    },
    "jscpd": {
        "percentage": "5.62"
    },
    "complexity": {
        "max": 15,
        "gt5Count": 3,
        "gt10Count": 1,
        "gt20Count": 0
    }
}
文档写错了,--ignore-file 应该是 --ignore-path
另外还有一个参数是 --verbose 可以打印出更多信息
更新输出格式为:
Number of files:        9
Source lines of code:   463
Duplicate rate:         15.71%
High complexity rate:   11.11%
Max complexity:         19
增加 --complexity-threshold 参数,默认为 10
|  |      1xcatliu OP 先来贴一个我们团队的项目的: Number of files: 997 Souce lines of code: 94524 Duplicate rate: 12.17% Max complexity: 89 Complexity > 5 (count): 292 Complexity > 10 (count): 76 Complexity > 20 (count): 17 重复代码率有点高,一般在 5% 以内比较正常。 最大圈复杂度有点太大了。。。 | 
|  |      2iluhcm      2017-09-20 18:50:16 +08:00  1 试了一下,Mac 运行报错。 env: node\r: No such file or directory Node:v8.1.3 | 
|  |      3xcatliu OP | 
|      4siteshen      2017-09-20 19:10:35 +08:00  4 看标题还以为支持所有语言,结果打开 GitHub 才发现只支持前端的内容。 发在“程序员”节点,希望标题里有“前端”、“ Javascript ” 之类的字样。 | 
|  |      5xcatliu OP @siteshen 因为 complexity 是用 eslint 实现的,所以暂不支持其他语言的 complexity。重复度检查是给予 jscpd 的,所以支持很多语言。 | 
|  |      6spring5413      2017-09-20 19:31:47 +08:00  1 请问大神,这个检查重复原理是什么 | 
|  |      7vardarling      2017-09-20 19:46:33 +08:00  1 "Souce lines of code" bug +1 😆 | 
|  |      8xcatliu OP @vardarling lines of source code 😂 | 
|  |      9xcatliu OP @vardarling 第一次还没看出来,原来是少了个 r 😂 | 
|  |      10xcatliu OP @vardarling 已修复,谢谢反馈 😀 | 
|  |      11xcatliu OP | 
|  |      12vardarling      2017-09-20 20:06:15 +08:00 @xcatliu 第一次发现我眼睛这么好😂 | 
|      13zhlssg      2017-09-20 20:53:02 +08:00 via iPhone 这个有点厉害 | 
|  |      14mhycy      2017-09-20 22:05:49 +08:00  12 “您的代码质量分已超过 90%程序员,点击按钮一键优化” | 
|  |      16AlloVince      2017-09-20 22:23:50 +08:00  1 Number of files:            342 Source lines of code: 38967 Duplicate rate: 3.29% Max complexity: 27 Complexity > 5 (count): 73 Complexity > 10 (count): 18 Complexity > 20 (count): 2 | 
|  |      18AlloVince      2017-09-20 22:56:40 +08:00  1 @xcatliu 不知道源代码里一些 ES 的语法是怎么处理的,扫描的源码里使用了 decorators,object rest spread 等一些需要 babel 的语法 | 
|  |      19binux      2017-09-20 23:12:50 +08:00  1 Number of files:            95 Source lines of code: 6233 Duplicate rate: 0.19% Max complexity: 45 Complexity > 5 (count): 18 Complexity > 10 (count): 6 Complexity > 20 (count): 3 | 
|  |      20momocraft      2017-09-20 23:17:00 +08:00  1 cqc... 这名字怎么这么谐 | 
|  |      21Sapp      2017-09-20 23:42:36 +08:00  1 Number of files:            1 Souce lines of code: 57915 Duplicate rate: 6.95% Max complexity: 72 Complexity > 5 (count): 1 Complexity > 10 (count): 1 Complexity > 20 (count): 1 测了一下打包后的。 | 
|      22orange666      2017-09-21 08:45:04 +08:00  1 新手一枚,看到就试试,怎么用的这玩意。 | 
|  |      23imlinhanchao      2017-09-21 09:02:46 +08:00  1 记得 Coding 自带这个功能,而且不仅支持前端代码。 @Bazingawang | 
|  |      24iugo      2017-09-21 09:18:04 +08:00 不知道 pattern 该怎么写. `cqc src/**/*.js src/**/*.jsx` 却只找到一个 jsx 文件... | 
|      25polun      2017-09-21 09:18:16 +08:00  1 报错,Linux Node: 6.11.0 fs.js:732 var r = binding.read(fd, buffer, offset, length, position); ^ Error: EISDIR: illegal operation on a directory, read at Error (native) at Object.fs.readSync (fs.js:732:19) at tryReadSync (fs.js:487:20) at Object.fs.readFileSync (fs.js:535:19) at slocResult.fileList.reduce (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/src/SlocChecker/index.js:14:36) at Array.reduce (native) at SlocChecker.check (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/src/SlocChecker/index.js:13:42) at CodeQualityChecker.check (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/src/CodeQualityChecker/index.js:17:45) at Object.<anonymous> (/home/polunzh/.nvm/versions/node/v6.11.0/lib/node_modules/cqc/bin/cqc.js:41:38) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:389:7) | 
|  |      26iugo      2017-09-21 09:24:01 +08:00 ``` cqc src/**/*.jsx src/**/**/*.jsx src/**/**/**/*.jsx src/**/**/**/**/*.jsx src/**/*.js src/**/**/*.js src/**/**/**/*.js src/**/**/**/**/*.js Number of files: 557 Source lines of code: 19665 Duplicate rate: 9.61% Max complexity: 12 Complexity > 5 (count): 10 Complexity > 10 (count): 1 Complexity > 20 (count): 0 ``` | 
|  |      27jjplay      2017-09-21 09:30:03 +08:00  1 cqc bin/cqc.js 会不会更棒! | 
|      31ma125125t      2017-09-21 09:41:01 +08:00  1 有个问题我不明白,我的 mac 安装任何软件都需要 sudo 权限,否则就会提示 permission denied,无论用 npm,还是其他安装工具,这是为什么呢? | 
|  |      33xcatliu OP @AlloVince  > 不知道源代码里一些 ES 的语法是怎么处理的,扫描的源码里使用了 decorators,object rest spread 等一些需要 babel 的语法 代码行数和重复率检查是(基本上)语言无关的,所以可以识别。Complexity 是使用的 eslint 识别的,默认加上了 babel-eslint parser,所以应该可以识别 decorators,object rest spread 语法 详见 https://github.com/xcatliu/cqc/blob/master/src/ComplexityChecker/eslintConfig.js | 
|  |      37page470075640      2017-09-21 09:50:36 +08:00  1 https://github.com/pagewang0/form-validation Number of files: 1 Source lines of code: 207 Duplicate rate: 0.00% Max complexity: 22 Complexity > 5 (count): 1 Complexity > 10 (count): 1 Complexity > 20 (count): 1 | 
|  |      38xcatliu OP @orange666 需要 node 环境。运行 npm i cqc -g 可以安装 cqc 命令,然后运行 cqc src/**/*.js 可以测所有 js 文件 | 
|  |      39xcatliu OP @imlinhanchao 要支持其他语言的代码,应该需要一个一个去适配 | 
|  |      41VtoEXL      2017-09-21 09:59:36 +08:00  1 不支持.vue 文件 | 
|  |      42xcatliu OP @polun  > Error: EISDIR: illegal operation on a directory, read 看 log 应该是把 dir 当成 file 去 read 了。 已修复,可以试一下新版本还有没有问题 npm i -g [email protected] | 
|  |      44xcatliu OP @jjplay cqc 自身的检测结果挺好的哇 Number of files: 8 Source lines of code: 360 Duplicate rate: 0.00% Max complexity: 4 Complexity > 5 (count): 0 Complexity > 10 (count): 0 Complexity > 20 (count): 0 | 
|  |      46xcatliu OP @page470075640 不错,不过 max complexity 有点高了,根据 [这个] 的说法: In general, for method level complexity: < 10 Easy to maintain 11-20 Harder to maintain 21+ Candidates for refactoring/redesign [这个]: https://softwareengineering.stackexchange.com/questions/101830/what-does-the-cyclomatic-complexity-of-my-code-mean | 
|  |      47xcatliu OP @VtoEXL sloc 暂时还不支持 vue 格式 https://github.com/flosse/sloc/issues/88 | 
|      49fds      2017-09-21 10:47:57 +08:00  1 游戏服务器 ts 生成的 js 分析结果: Number of files: 188 Source lines of code: 55663 Duplicate rate: 22.44% Max complexity: 145 Complexity > 5 (count): 97 Complexity > 10 (count): 69 Complexity > 20 (count): 26 辣眼睛……正在翻阅 verbose 结果中…… | 
|  |      50wyk52012      2017-09-21 10:53:15 +08:00  1 公司有 sonar。。。。 | 
|  |      54dreamwar      2017-09-21 11:43:56 +08:00 哈哈哈,很溜,有点像 360 开机时候提示你打败了全国多少人一样 | 
|  |      55figofuture      2017-09-21 11:47:10 +08:00 mark 了 | 
|  |      56SakuraKuma      2017-09-21 12:20:12 +08:00 Number of files:            60 Source lines of code: 16892 Duplicate rate: 4.74% Max complexity: 39 Complexity > 5 (count): 25 Complexity > 10 (count): 10 Complexity > 20 (count): 4 emmmm | 
|  |      57page470075640      2017-09-21 12:58:35 +08:00  1 @xcatliu 我的那个两百多行的代码 我不晓得如何进一步改进了 还望指正 | 
|  |      58jedihy      2017-09-21 13:09:40 +08:00  1 静态代码分析已经有非常成熟的产品了,上学期有门课的老师就是这个领域的大牛,可以试试这个 https://deepscan.io | 
|  |      59NCE      2017-09-21 13:21:03 +08:00  1 要解析一个项目需要多长时间? | 
|  |      60NCE      2017-09-21 13:21:32 +08:00 Number of files:            1644 Source lines of code: 129596 Duplicate rate: 1.17% Max complexity: 0 Complexity > 5 (count): 0 Complexity > 10 (count): 0 Complexity > 20 (count): 0 | 
|  |      62rashawn      2017-09-21 14:05:31 +08:00 Number of files:            286 Source lines of code: 72966 Duplicate rate: 2.91% Max complexity: 158 Complexity > 5 (count): 146 Complexity > 10 (count): 85 Complexity > 20 (count): 31 | 
|  |      63hanzichi      2017-09-21 15:48:06 +08:00  1 cat 胸去 alloyteam 啦? | 
|  |      66page470075640      2017-09-21 22:51:04 +08:00  1 Number of files:            1 Source lines of code: 192 Duplicate rate: 0.00% Max complexity: 19 Complexity > 5 (count): 1 Complexity > 10 (count): 1 Complexity > 20 (count): 0 谢咯 感觉这个 Complexity 是时间复杂度对吧 | 
|  |      67xcatliu OP @page470075640 这里的复杂度不是时间复杂度,是圈复杂度,也叫循环复杂度( Cyclomatic complexity ),就是程序的流程图里面的圈的数量。它决定了程序的可读性和可维护性。 少于 10 易于维护 多于 10 少于 20 难维护 超过 20 就不可维护了 | 
|  |      69jisi724      2017-09-22 02:34:52 +08:00  1 Number of files:        58 Source lines of code: 1471 Duplicate rate: 1.04% High complexity rate: 1.72% Max complexity: 14 | 
|  |      71k9982874      2017-09-22 08:55:53 +08:00 ``` Number of files: 13 Source lines of code: 1083 Duplicate rate: 5.24% High complexity rate: 7.69% Max complexity: 19 ``` | 
|  |      72zoffy      2017-09-22 09:18:54 +08:00 比较好奇重复率的算法怎么写的 | 
|  |      75qichunren      2017-09-22 10:37:52 +08:00 你的这个工具要是再支持一些其它的如 c,c++,ruby,python 之类的就好了。 | 
|  |      76ThomasChan      2017-09-22 11:08:38 +08:00 Number of files:        280 Source lines of code: 57742 Duplicate rate: 7.52% High complexity rate: 26.43% Max complexity: 65 complexity 超过 20 难以维护?。。。。😂😂😂 | 
|  |      77pengfei      2017-09-22 11:17:52 +08:00 /i/h3P3KZWh.png 代码有点少 | 
|  |      78pengfei      2017-09-22 11:18:02 +08:00 | 
|  |      79run2      2017-09-22 14:00:22 +08:00  1 | 
|  |      80xcatliu OP @sobigfish 感谢提醒,这个项目依赖了 sloc, jscpd, eslint 等,他们都是 MIT 协议。 我从开源社区的收获远比我的贡献多,所以我的所有项目几乎都是 MIT 协议,希望能够给开源社区贡献一点力量! | 
|  |      81jianghu521      2017-09-22 19:32:19 +08:00 zsh: command not found: cpc @xcatliu | 
|  |      82xcatliu OP @jianghu521 cqc 不是 cpc 哈 | 
|  |      83Hilong      2017-09-22 19:44:39 +08:00 via Android 不支持 vue 文件。。。 | 
|  |      84jianghu521      2017-09-22 19:45:41 +08:00 @xcatliu 嗯 谢谢 写错了 | 
|  |      85jianghu521      2017-09-22 19:58:22 +08:00 Number of files:            344 Source lines of code: 26728 Duplicate rate: 0.00% Max complexity: 0 Complexity > 5 (count): 0 Complexity > 10 (count): 0 Complexity > 20 (count): 0 @xcatliu 是不是有问题啊 | 
|      86Maic      2017-09-22 23:43:34 +08:00 ➜  git:(master) ✗ node_modules/.bin/cqc back/* Number of files: 8 Source lines of code: 773 Duplicate rate: 1.23% High complexity rate: 0.00% Max complexity: 8 ➜ git:(master) ✗ node_modules/.bin/cqc back/*/* Number of files: 7 Source lines of code: 419 Duplicate rate: 0.00% High complexity rate: 0.00% Max complexity: 5 ➜ git:(master) ✗ node_modules/.bin/cqc back/ Number of files: 0 Source lines of code: 0 Duplicate rate: 0.00% High complexity rate: NaN% Max complexity: 0 ➜ git:(master) ✗ node_modules/.bin/cqc back/* back/*/* Number of files: 15 Source lines of code: 1192 Duplicate rate: 0.84% High complexity rate: 0.00% Max complexity: 8 --- 是我写的规则不对嘛 |