运行一个脚本,看看你的项目的代码质量吧

2017-09-20 18:18:45 +08:00
 xcatliu

代码质量有很多指标:

  1. 源代码行数
  2. 代码重复率
  3. 圈复杂度
  4. 报错量( Bug 数)占比
  5. 测试覆盖率
  6. 开发约束(代码块行数等)

我做了一个脚本可以测出上面的 1, 2, 3

大家都来试一试吧!

Installation

npm install -g cqc

Usage

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

Multiple patterns

cqc src/**/*.js src/**/*.jsx

--ignore-pattern

cqc src/**/*.js --ignore-pattern src/vendor/**/*.js
cqc src/**/*.js --ignore-pattern src/vendor/**/*.js,src/third-party/**/*.js

--ignore-file

cqc src/**/*.js --ignore-file .gitignore
cqc src/**/*.js --ignore-file .gitignore,.eslintignore

--format

cqc src/**/*.js --format json

Output:

{
    "numberOfFiles": 8,
    "sloc": {
        "source": 357
    },
    "jscpd": {
        "percentage": "5.62"
    },
    "complexity": {
        "max": 15,
        "gt5Count": 3,
        "gt10Count": 1,
        "gt20Count": 0
    }
}
15206 次点击
所在节点    程序员
86 条回复
Sapp
2017-09-20 23:42:36 +08:00
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

测了一下打包后的。
orange666
2017-09-21 08:45:04 +08:00
新手一枚,看到就试试,怎么用的这玩意。
imlinhanchao
2017-09-21 09:02:46 +08:00
记得 Coding 自带这个功能,而且不仅支持前端代码。 @Bazingawang
iugo
2017-09-21 09:18:04 +08:00
不知道 pattern 该怎么写.

`cqc src/**/*.js src/**/*.jsx` 却只找到一个 jsx 文件...
polun
2017-09-21 09:18:16 +08:00
报错,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)
iugo
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
```
jjplay
2017-09-21 09:30:03 +08:00
cqc bin/cqc.js 会不会更棒!
xcatliu
2017-09-21 09:32:23 +08:00
@iugo 是不是在 windows 环境下?
iugo
2017-09-21 09:38:05 +08:00
@xcatliu macOS 10.12.6, VS Code 终端
iugo
2017-09-21 09:39:00 +08:00
@xcatliu Node v8.4.0 npm v5.4.2
ma125125t
2017-09-21 09:41:01 +08:00
有个问题我不明白,我的 mac 安装任何软件都需要 sudo 权限,否则就会提示 permission denied,无论用 npm,还是其他安装工具,这是为什么呢?
xcatliu
2017-09-21 09:44:44 +08:00
@iugo 试一下加上双引号呢? cqc "src/**/*.js" "src/**/*.jsx"
xcatliu
2017-09-21 09:48:23 +08:00
@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
xcatliu
2017-09-21 09:49:38 +08:00
@binux 重复率 0.19% 是怎么做到的,厉害。
xcatliu
2017-09-21 09:49:54 +08:00
@momocraft cqc 就是 Code Quality Checker
xcatliu
2017-09-21 09:50:19 +08:00
@Sapp 测打包后的没什么意义吧
page470075640
2017-09-21 09:50:36 +08:00
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
xcatliu
2017-09-21 09:51:28 +08:00
@orange666 需要 node 环境。运行 npm i cqc -g 可以安装 cqc 命令,然后运行 cqc src/**/*.js 可以测所有 js 文件
xcatliu
2017-09-21 09:52:10 +08:00
@imlinhanchao 要支持其他语言的代码,应该需要一个一个去适配
iugo
2017-09-21 09:53:04 +08:00
@xcatliu Ok.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/392337

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX