A code analysis engine.
$ npm install catbus -g
安装如有报错,请尝试强制安装:
$ npm install catbus -gf
catbus支持三种类型的文档:html、js、css。
- 扫描文件
$ catbus /path/to/file.htm
$ catbus /path/to/file.js
$ catbus /path/to/file.css
- 扫描目录
$ catbus /path/to/directory/
- 扫描URL
$ catbus https://financeprod.alipay.com/financing/dcbApp.htm
$ catbus https://www.alipay.com/
$ catbus http://php.net/downloads.php
- require导入扫描规则
$ catbus --require catbus-rule
其中catbus-rule是外部规则模块,通过npm安装$ npm install catbus-rule -g,示例: cabut-html-typos
配置文件
在当前扫描目录下,新增文件catbus-config.js,配置文件代码示例如下:
var config = {
options: {},
rules: []
};
exports.config = config;配置文件中,用户可以配置基础扫描规则的开启和关闭、自定义扫描规则
基础规则配置
在options对象中,设置规则开启/关闭,true为开启,false为关闭。
var config = {
options: {
"html-tag-close": true,
"html-id-duplicate": false,
"html-meta-charset": false,
"html-unsafe-resource": false,
"html-https-warning": false,
"html-hard-code": false,
"html-doctype": false
},
rules: []
};
exports.config = config;编写自定义规则
在rules数组中编写自定义规则,示例如下
var config = {
options: {},
rules: [
{
author: "远尘 <codedancerhua@gmail.com>",
id: "form-table-className",
description: "表单内的表格class必须包含table form-table well",
level: "error",
tagName: "form",
validator: function(reporter, nodes) {
var classes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].childNodes[0].tagName == 'table') {
classes = nodes[i].childNodes[0].getAttribute('class');
if (!classes ||
classes.indexOf('table') == -1 ||
classes.indexOf('form-table') == -1 ||
classes.indexOf('well') == -1) {
reporter[this.level](this.description, nodes[i].line, null, this)
}
}
}
}
}
]
};
exports.config = config;自定义规则API说明:
id:规则标识,命名必须以扫描文档对象类型开头,如html-、css-或js-,必选author:规则作者,必选description:规则描述,必选validator:规则处理函数,必选- 对于html规则,传入三个参数:function(reporter, nodes, rawStr)
- 对于js、css规则,传入两个参数:function(reporter, rawStr)
reporter为报告器,有reporter.error和reporter.warning两个方法nodes为匹配到tagName的节点数组rawStr为扫描文件的原始内容字符串
level:规则等级,可选,默认为errortagName:html匹配节点标签名,可选,未设置时,validator的参数nodes为null
完整的配置文件示例如下:
var config = {
options: {
"html-tag-close": true,
"html-id-duplicate": false,
"html-meta-charset": false,
"html-unsafe-resource": false,
"html-https-warning": false,
"html-hard-code": false,
"html-doctype": false
},
rules: [
{
author: "远尘 <codedancerhua@gmail.com>",
id: "form-table-className",
description: "表单内的表格class必须包含table form-table well",
level: "error",
tagName: "form",
validator: function(reporter, nodes, rawStr) {
var classes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].childNodes[0].tagName == 'table') {
classes = nodes[i].childNodes[0].getAttribute('class');
if (!classes ||
classes.indexOf('table') == -1 ||
classes.indexOf('form-table') == -1 ||
classes.indexOf('well') == -1) {
reporter[this.level](this.description, nodes[i].line, null, this)
}
}
}
}
}
]
};
exports.config = config;