安装依赖
yarn add husky@^3.1.0 -D
yarn add lint-staged@^9.2.3 prettier@^1.18.2
修改package.json
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{js,vue,jsx,ts,tsx,html}": [
"prettier --write",
"eslint --fix",
"git add"
],
"src/**/*.{json,css,scss,md}": [
"prettier --write",
"git add"
]
},
增加.prettierignore 文件
{
"semi": false,
"trailingComma": true,
"singleQuote": true,
"printWidth": 120
}
增加.eslintignore 文件
public/**/*.js
node_modules
package.json
新建 .eslintrc.js文件
module.exports = {
// 此项是用来告诉eslint找当前配置文件不能往父级查找
root: true,
// 全局环境
env: {
node: true
},
// 指定如何解析语法。可以为空,但若不为空,只能配该值
parser: 'vue-eslint-parser',
// 优先级低于parse的语法解析配置
parserOptions: {
// 指定ESlint的解析器
parser: '@typescript-eslint/parser',
// 允许使用ES语法
ecmaVersion: 2020,
// 允许使用import
sourceType: 'module',
// 允许解析JSX
ecmaFeatures: {
jsx: true
}
},
/**
* "off" 或 0 - 关闭规则
* "warn" 或 1 - 开启规则,使用警告级别的错误:warn (不会导致程序退出)
* "error" 或 2 - 开启规则,使用错误级别的错误:error (当被触发的时候,程序会退出)
*/
extends: ['plugin:vue/vue3-essential', 'plugin:@typescript-eslint/recommended', 'prettier'],
rules: {
'vue/no-multiple-template-root': 'off',
'array-bracket-spacing': 'off', // 是否允许非空数组里面有多余的空格
'arrow-parens': 'off', // 箭头函数用小括号括起来
'block-spacing': 'off', // =>的前/后括号
'accessor-pairs': 'off', // 在对象中使用getter/setter
'block-scoped-var': 'off', // 块语句中使用var
'callback-return': 'warn', // 避免多次调用回调什么的
camelcase: 'off', // 强制驼峰法命名
'comma-dangle': 'off', // 对象字面量项尾不能有逗号
'comma-spacing': 'off', // 逗号前后的空格
'comma-style': 'off', // 逗号风格,换行时在行首还是行尾
complexity: ['off', 11], // 循环复杂度
'computed-property-spacing': ['off', 'never'], // 是否允许计算后的键名什么的
'consistent-return': 'off', // return 后面是否允许省略
'consistent-this': 'off', // this别名
'constructor-super': 'off', // 非派生类不能调用super,派生类必须调用super
'default-case': 'error', // switch语句最后必须有default
eqeqeq: 'error', // 要求使用 === 和 !==
'func-names': 'off', // 函数表达式必须有名字
'func-style': ['off', 'declaration'], // 函数风格,规定只能使用函数声明/函数表达式
'generator-star-spacing': 'off', // 生成器函数*的前后空格
'guard-for-in': 'off', // for in循环要用if语句过滤
'handle-callback-err': 'off', // nodejs 处理错误
'id-length': 'off', // 变量名长度
indent: ['error', 2], // 缩进风格
'init-declarations': 'off', // 声明时必须赋初始值
'max-len': ['off', 80, 4], // 字符串最大长度
'max-nested-callbacks': ['off', 'error'], // 回调嵌套深度
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 是否使用 console
'no-const-assign': 'error', // 禁止修改const声明的变量
'no-constant-condition': 'error', // 禁止在条件中使用常量表达式 if(true) if('warn')
'no-continue': 'off', // 禁止使用continue
'no-control-regex': 'error', // 禁止在正则表达式中使用控制字符
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 是否使用 debugger
'no-delete-var': 'error', // 不能对var声明的变量使用delete操作符
'no-div-regex': 'warn', // 不能使用看起来像除法的正则表达式/=foo/
'no-dupe-keys': 'error', // 在创建对象字面量时不允许键重复 {a:'warn',a:'warn'}
'no-dupe-args': 'error', // 函数参数不能重复
'no-duplicate-case': 'error', // switch中的case标签不能重复
'no-else-return': 'off', // 如果if语句里面有return,后面不能跟else语句
'no-empty': 'off', // 块语句中的内容不能为空
'no-invalid-regexp': 'error', // 禁止无效的正则表达式
'no-mixed-spaces-and-tabs': ['error', false], // 禁止混用tab和空格
'linebreak-style': ['off', 'windows'], // 换行风格
'no-multiple-empty-lines': ['warn', { max: 3 }], // 空行最多不能超过'error'行
'no-path-concat': 'off', // node中不能使用__dirname或__filename做路径拼接
'no-plusplus': 'off', // 禁止使用++,--
'no-process-env': 'off', // 禁止使用process.env
'no-process-exit': 'off', // 禁止使用process.exit()
'no-proto': 'error', // 禁止使用__proto__属性
'no-redeclare': 'error', // 禁止重复声明变量
'no-regex-spaces': 'error', // 禁止在正则表达式字面量中使用多个空格 /foo bar/
'no-restricted-modules': 'off', // 如果禁用了指定模块,使用就会报错
'no-return-assign': 'off', // return 语句中不能有赋值表达式
'no-script-url': 'off', // 禁止使用javascript:void('off')
'no-self-compare': 'error', // 不能比较自身
'no-sequences': 'off', // 禁止使用逗号运算符
'no-shadow': 'error', // 外部作用域中的变量不能与它所包含的作用域中的变量或参数同名
'no-shadow-restricted-names': 'error', // 严格模式中规定的限制标识符不能作为声明时的变量名使用
'no-spaced-func': 'error', // 函数调用时 函数名与()之间不能有空格
'no-sparse-arrays': 'error', // 禁止稀疏数组, ['warn',,'error']
'no-sync': 'off', // nodejs 禁止同步方法
'no-ternary': 'off', // 禁止使用三目运算符
'no-trailing-spaces': 'warn', // 一行结束后面不要有空格
'no-undef': 'warn', // 不能有未定义的变量
'no-unexpected-multiline': 'error', // 避免多行表达式
'no-unused-expressions': 'off', // 禁止无用的表达式
'no-unused-vars': 'off', // 不能有声明后未被使用的变量或参数
'no-var': 'error', // 禁用var,用let和const代替
'no-warning-comments': ['warn', { terms: ['todo', 'fixme', 'xxx'], location: 'start' }], // 不能有警告备注
'no-with': 'error', // 禁用with
'object-curly-spacing': ['off', 'never'], // 大括号内是否允许不必要的空格
'prefer-const': 'off', // 首选const
radix: 'error', // parseInt必须指定第二个参数
strict: 'error', // 使用严格模式
'use-isnan': 'error', // 禁止比较时使用NaN,只能用isNaN()
'no-empty-label': 'off',
'valid-typeof': 'error', // 必须使用合法的typeof的值
'vars-on-top': 'error', // var必须放在作用域顶部
'vue/require-v-for-key': 'off', // vue的for循环是否必须有key
'wrap-iife': ['error', 'inside'], // 立即执行函数表达式的小括号风格
'wrap-regex': 'off', // 正则表达式字面量用小括号包起来
yoda: ['error', 'never'], // 禁止尤达条件
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unused-vars': 'off'
}
}
vscode设置中添加
"eslint.alwaysShowStatus": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.formatOnSave": true,
"eslint.validate": [
"javascript",
"javascriptreact",
"html",
],
"diffEditor.ignoreTrimWhitespace": false,
"[javascript]": {
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint"
},