对所有js文件进行校验
安装pre-commit插件的依赖
npm i --save-dev pre-commit
1
package.json
{
"name": "nodetest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "eslint . --fix",//对所有文件进行校验,及补缺
"lint:create": "eslint --init" //创建。eslintrc.js
},
"pre-commit": [
"lint" //在git commit 时运行npm run lint 进行校验,若有错误不能提交
],
"author": "shuah",
"license": "ISC",
"devDependencies": {
"eslint": "^5.16.0",
"pre-commit": "^1.2.2"
},
"dependencies": {
"chalk": "^2.4.2"
}
}
.eslintrc.js 文件
eslint 部分校验规则点这里
module.exports = {
"env": {
"browser": false,
"node": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"no-console": "off" ,
// 所有引号使用双引号
"quotes":[
"error",
"double"
],
// 数组和对象键值对最后一个逗号, never参数:不能带末尾的逗号, always参数:必须带末尾的逗号,
// always-multiline:多行模式必须带逗号,单行模式不能带逗号
"comma-dangle": [1, "always-multiline"],
// 禁用 alert、confirm 和 prompt
"no-alert": 0,
// 控制逗号前后的空格
"comma-spacing": [2, {
"before": false,
"after": true
}],
// 控制逗号在行尾出现还是在行首出现 (默认行尾)
// http://eslint.org/docs/rules/comma-style
"comma-style": [2, "last"],
}
};
.eslintignore 文件
不用校验的文件
node_modules
dist/
test
build/