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

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
    }
}
15219 次点击
所在节点    程序员
86 条回复
VtoEXL
2017-09-21 09:59:36 +08:00
不支持.vue 文件
xcatliu
2017-09-21 10:00:43 +08:00
@polun
> Error: EISDIR: illegal operation on a directory, read
看 log 应该是把 dir 当成 file 去 read 了。
已修复,可以试一下新版本还有没有问题 npm i -g cqc@0.1.5
xcatliu
2017-09-21 10:02:04 +08:00
@iugo 加双引号就可以了?估计是 glob 这块有点问题,我看看好不好修复
xcatliu
2017-09-21 10:08:52 +08:00
@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
xcatliu
2017-09-21 10:09:51 +08:00
@ma125125t 可以用 nvm 来安装 node 和 npm,应该就不需要 sudo 了吧
xcatliu
2017-09-21 10:13:00 +08:00
@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
xcatliu
2017-09-21 10:15:26 +08:00
@VtoEXL sloc 暂时还不支持 vue 格式 https://github.com/flosse/sloc/issues/88
Sapp
2017-09-21 10:46:13 +08:00
@xcatliu 因为不支持 vue。只能测打包的。
fds
2017-09-21 10:47:57 +08:00
游戏服务器 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 结果中……
wyk52012
2017-09-21 10:53:15 +08:00
公司有 sonar。。。。
iugo
2017-09-21 11:27:48 +08:00
@fds 游戏服务也可以用 Node 做? 传说中都是用 C++...
xcatliu
2017-09-21 11:41:10 +08:00
@wyk52012 sonar 确实是一个好工具!我这只是一个比较轻量的小工具
xcatliu
2017-09-21 11:43:19 +08:00
@fds Max complexity 145 也是厉害!
dreamwar
2017-09-21 11:43:56 +08:00
哈哈哈,很溜,有点像 360 开机时候提示你打败了全国多少人一样
figofuture
2017-09-21 11:47:10 +08:00
mark 了
SakuraKuma
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
page470075640
2017-09-21 12:58:35 +08:00
@xcatliu 我的那个两百多行的代码 我不晓得如何进一步改进了 还望指正
jedihy
2017-09-21 13:09:40 +08:00
静态代码分析已经有非常成熟的产品了,上学期有门课的老师就是这个领域的大牛,可以试试这个
https://deepscan.io
NCE
2017-09-21 13:21:03 +08:00
要解析一个项目需要多长时间?
NCE
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

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

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

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

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

© 2021 V2EX