常用eslint配置

参考Airbnb JavaScript 风格指南

1、 安装npm依赖包

1
npm i babel-eslint eslint eslint-plugin-import eslint-config-airbnb-base eslint-plugin-vue -D

2、 创建.eslintrc.js文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
module.exports = {
env: {
browser: true,
es6: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:vue/essential'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
parser: 'babel-eslint',
ecmaVersion: 2018,
sourceType: 'module',
},
plugins: [
'vue'
],
rules: {
'vue/max-attributes-per-line': [2, {
singleline: 1,
multiline: {
max: 1,
allowFirstLine: true,
},
}],
'vue/html-indent': ['error', 2, {
attribute: 1,
baseIndent: 1,
closeBracket: 0,
alignAttributesVertically: true,
ignores: [],
}],
// 配置参考 https://eslint.vuejs.org/rules/
'vue/attributes-order': ['error', {
order: [
'DEFINITION', // e.g. 'is'
'LIST_RENDERING', // e.g. 'v-for item in items'
'CONDITIONALS', // e.g. 'v-if', 'v-else-if', 'v-else', 'v-show', 'v-cloak'
'RENDER_MODIFIERS', // e.g. 'v-once', 'v-pre'
'GLOBAL', // e.g. 'id'
'UNIQUE', // e.g. 'ref', 'key', 'v-slot', 'slot'
'TWO_WAY_BINDING', // e.g. 'v-model'
'OTHER_DIRECTIVES', // e.g. 'v-custom-directive'
'OTHER_ATTR', // e.g. 'custom-prop="foo"', 'v-bind:prop="foo"', ':prop="foo"'
'EVENTS', // e.g. '@click="functionCall"', 'v-on="event"'
'CONTENT' // e.g. 'v-text', 'v-html'
],
}],
'vue/order-in-components': ['error', {
order: [
'el',
'name',
'parent',
'functional',
['delimiters', 'comments'],
['components', 'directives', 'filters'],
'extends',
'mixins',
'inheritAttrs',
'model',
['props', 'propsData'],
'fetch',
'asyncData',
'data',
'computed',
'watch',
'LIFECYCLE_HOOKS',
'methods',
'head',
['template', 'render'],
'renderError'
],
}],
'vue/no-v-html': 'off',
'vue/array-bracket-spacing': 'error',
'vue/arrow-spacing': 'error',
'vue/block-spacing': 'error',
'vue/camelcase': 'error',
'vue/comma-dangle': 'error',
'vue/component-name-in-template-casing': ['error', 'kebab-case', {
registeredComponentsOnly: false,
ignores: [],
}],
'vue/eqeqeq': 'error',
'vue/key-spacing': 'error',
'vue/match-component-file-name': ['error', {
extensions: ['jsx'],
shouldMatchCase: false,
}],
'vue/no-restricted-syntax': 'error',
'vue/object-curly-spacing': 'error',
'vue/require-direct-export': 'error',
'vue/space-infix-ops': 'error',
'vue/space-unary-ops': 'error',
'vue/v-on-function-call': ['error', 'never'],
'vue/name-property-casing': ['error', 'PascalCase'],
'accessor-pairs': 2, // 强制 getter 和 setter 在对象中成对出现
'arrow-spacing': [2, {
before: true,
after: true,
}], // 强制箭头函数的箭头前后使用一致的空格
'block-spacing': [2, 'always'], // 禁止或强制在代码块中开括号前和闭括号后有空格
'brace-style': [2, '1tbs', {
allowSingleLine: true,
}], // 强制在代码块中使用一致的大括号风格
camelcase: [0, {
properties: 'always',
}], // 强制使用骆驼拼写法命名约定
'comma-dangle': [2, {
arrays: 'never',
objects: 'always',
imports: 'never',
exports: 'never',
functions: 'never',
}], // 要求或禁止末尾逗号
'comma-spacing': [2, {
before: false,
after: true,
}], // 强制在逗号前后使用一致的空格
'comma-style': [2, 'last'], // 强制使用一致的逗号风格
'constructor-super': 2, // 要求在构造函数中有 super() 的调用
curly: [2, 'multi-line'], // 强制所有控制语句使用一致的括号风格
'computed-property-spacing': [2, 'never'],
'dot-location': [2, 'property'], // 强制在点号之前和之后一致的换行
'eol-last': 2, // 要求或禁止文件末尾存在空行
eqeqeq: [0, 'allow-null'], // 要求使用 === 和 !==
'generator-star-spacing': [2, {
before: true,
after: true,
}], // 强制 generator 函数中 * 号周围使用一致的空格
'handle-callback-err': [2, '^(err|error)$'], // 要求回调函数中有容错处理
indent: [2, 2, {
SwitchCase: 1,
}], // 强制使用一致的缩进
'id-length': [2, { min: 2, }], // 避免用一个字母命名,让你的命名可描述
'jsx-quotes': [2, 'prefer-single'], // 强制在 JSX 属性中一致地使用双引号或单引号
'key-spacing': [2, {
beforeColon: false,
afterColon: true,
}], // 强制在对象字面量的属性中键和值之间使用一致的间距
'keyword-spacing': [2, {
before: true,
after: true,
}], // 强制在关键字前后使用一致的空格
'new-cap': [2, {
newIsCap: true,
capIsNew: false,
}], // 要求构造函数首字母大写
'new-parens': 2, // 要求调用无参构造函数时有圆括号
'no-array-constructor': 2, // 禁用 Array 构造函数
'no-implicit-coercion': 'off',
'no-caller': 2, // 禁用 arguments.caller 或 arguments.callee
'no-console': 'off', // 禁用 console
'no-class-assign': 2, // 禁止修改类声明的变量
'no-cond-assign': 2, // 禁止条件表达式中出现赋值操作符
'no-const-assign': 2, // 禁止修改 const 声明的变量
'no-control-regex': 2, // 禁止在正则表达式中使用控制字符
'no-delete-var': 2, // 禁止删除变量
'no-duplicate-imports': 2, // 一个路径只 import 一次
'no-dupe-args': 2, // 禁止 function 定义中出现重名参数
'no-dupe-class-members': 2, // 禁止类成员中出现重复的名称
'no-dupe-keys': 2, // 禁止对象字面量中出现重复的 key
'no-duplicate-case': 2, // 禁止出现重复的 case 标签
'no-empty-character-class': 2, // 禁止在正则表达式中使用空字符集
'no-empty-pattern': 2, // 禁止使用空解构模式
'no-eval': 2, // 禁用 eval()
'no-ex-assign': 2, // 禁止对 catch 子句的参数重新赋值
'no-extend-native': 2, // 禁止扩展原生类型
'no-extra-bind': 2, // 禁止不必要的 .bind() 调用
'no-extra-boolean-cast': 2, // 禁止不必要的布尔转换
'no-extra-parens': [0, 'functions'], // 禁止不必要的括号
'no-else-return': 2, // 如果 if 语句中总是需要用 return 返回, 那后续的 else 就不需要写了。 if 块中包含 return, 它后面的 else if 块中也包含了 return, 这个时候就可以把 return 分到多个 if 语句块中
'no-fallthrough': 2, // 禁止 case 语句落空
'no-floating-decimal': 2, // 禁止数字字面量中使用前导和末尾小数点
'no-func-assign': 2, // 禁止对 function 声明重新赋值
'no-implied-eval': 2, // 禁止使用类似 eval() 的方法
'no-inner-declarations': [2, 'functions'], // 禁止在嵌套的块中出现变量声明或 function 声明
'no-invalid-regexp': 2, // 禁止 RegExp 构造函数中存在无效的正则表达式字符串
'no-irregular-whitespace': 2, // 禁止在字符串和注释之外不规则的空白
'no-iterator': 2, // 禁用 __iterator__ 属性
'no-label-var': 2, // 不允许标签与变量同名
'no-labels': [2, {
allowLoop: false,
allowSwitch: false,
}], // 禁用标签语句
'no-lone-blocks': 2, // 禁用不必要的嵌套块
'no-mixed-spaces-and-tabs': 2, // 禁止空格和 tab 的混合缩进
'no-multi-spaces': 2, // 禁止使用多个空格
'no-multi-str': 2, // 禁止使用多行字符串
'no-mixed-operators': 2, // 用圆括号来混合这些操作符。 只有当标准的算术运算符(+, -, *, & /), 并且它们的优先级显而易见时,可以不用圆括号括起来
'no-multiple-empty-lines': [2, {
max: 3,
maxEOF: 3,
maxBOF: 3,
}], // 禁止出现多行空行(此处设置最多出现连续3个空行)
'no-global-assign': 2, // 禁止对原生对象或只读的全局对象进行赋值,原no-native-reassign已被此替换
'no-unsafe-negation': 2, // 禁止对关系运算符的左操作数使用否定操作符,原no-negated-in-lhs已被此替换
'no-multi-assign': 2, // 不要使用链接变量分配
'no-new-object': 2, // 禁用 Object 的构造函数
'no-new-require': 2, // 禁止调用 require 时使用 new 操作符
'no-new-symbol': 2, // 禁止 Symbolnew 操作符和 new 一起使用
'no-new-wrappers': 2, // 禁止对 String,Number 和 Boolean 使用 new 操作符
'no-new-func': 2, // 不要用函数构造器创建函数(bad var add = new Function(){})
'no-obj-calls': 2, // 禁止把全局对象作为函数调用
'no-octal': 2, // 禁用八进制字面量
'no-octal-escape': 2, // 禁止在字符串中使用八进制转义序列
'no-path-concat': 2, // 禁止对 __dirname 和 __filename 进行字符串连接
'no-proto': 2, // 禁用 __proto__ 属性
'no-param-reassign': 2, // 不要对参数重新赋值
'no-redeclare': 2, // 禁止多次声明同一变量
'no-regex-spaces': 2, //
'no-return-assign': [2, 'except-parens'], // 禁止在 return 语句中使用赋值语句
'no-self-assign': 2, // 禁止自我赋值
'no-self-compare': 2, // 禁止自身比较
'no-sequences': 2, // 禁用逗号操作符
'no-shadow-restricted-names': 2, // 禁止将标识符定义为受限的名字
'func-call-spacing': 2, // 要求或禁止在函数标识符和其调用之间有空格,原no-spaced-func已被此替换
'no-sparse-arrays': 2, // 禁用稀疏数组
'no-this-before-super': 2, // 禁止在构造函数中,在调用 super() 之前使用 this 或 super
'no-throw-literal': 2, // 禁止抛出异常字面量
'no-trailing-spaces': 1, // 禁用行尾空格
'no-undef': 2, // 禁用未声明的变量,除非它们在 /*global */ 注释中被提到
'no-undef-init': 2, // 禁止将变量初始化为 undefined
'no-unexpected-multiline': 2, // 禁止出现令人困惑的多行表达式
'no-unmodified-loop-condition': 2, // 禁用一成不变的循环条件
'no-unneeded-ternary': [2, {
defaultAssignment: false,
}], // 禁止可以在有更简单的可替代的表达式时使用三元操作符
'no-unreachable': 2, // 禁止在return、throw、continue 和 break 语句之后出现不可达代码
'no-unsafe-finally': 2, // 禁止在 finally 语句块中出现控制流语句
'no-unused-vars': [2, {
vars: 'all',
args: 'none',
}], // 禁止出现未使用过的变量
'no-useless-call': 2, // 禁止不必要的 .call() 和 .apply()
'no-useless-computed-key': 2, // 禁止在对象中使用不必要的计算属性
'no-useless-constructor': 2, // 禁用不必要的构造函数
'no-useless-escape': 0, // 禁用不必要的转义字符
'no-whitespace-before-property': 2, // 禁止属性前有空白
'no-with': 2, // 禁用 with 语句
'no-var': 2,
'newline-per-chained-call': [2, { ignoreChainWithDepth: 2, }], // 当出现长的方法链(>2个)时用缩进。用点开头强调该行是一个方法调用,而不是一个新的语句
'one-var': [2, {
initialized: 'never',
}], // 强制函数中的变量要么一起声明要么分开声明
'operator-linebreak': [2, 'after', {
overrides: {
'?': 'before',
':': 'before',
},
}], // 强制操作符使用一致的换行符
'padded-blocks': [0, 'never'], // 要求或禁止块内填充
'prefer-destructuring': [2, { object: true, array: false, }], // 用对象的解构赋值来获取和使用对象某个或多个属性值
'prefer-template': 1, // 用字符串模板而不是字符串拼接来组织可编程字符串
'prefer-rest-params': 1, // 不要使用arguments,用rest语法...代替
quotes: [2, 'single', {
avoidEscape: true,
allowTemplateLiterals: true,
}], // 强制使用一致的反勾号、双引号或单引号
'quote-props': [2, 'as-needed'], // 只对那些无效的标示使用引号 ''
// 'semi': [2, 'never'], // 要求或禁止使用分号代替 ASI,分号设置:never 从不出现分号,always 必须分号结尾
semi: [0],
'semi-spacing': [2, {
before: false,
after: true,
}], // 强制分号之前和之后使用一致的空格
'space-before-blocks': [2, 'always'], // 强制在块之前使用一致的空格(在大括号前空一格)
'space-before-function-paren': [2, 'always'], // 强制在 function的左括号之前使用一致的空格
'space-in-parens': [2, 'never'], // 强制在圆括号内使用一致的空格
'space-infix-ops': 2, // 要求操作符周围有空格
'space-unary-ops': [2, {
words: true,
nonwords: false,
}], // 强制在一元操作符前后使用一致的空格
'spaced-comment': [2, 'always', {
markers: ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ','],
}], // 强制在注释中 // 或 /* 使用一致的空格
'template-curly-spacing': [2, 'never'], // 要求或禁止模板字符串中的嵌入表达式周围空格的使用
'use-isnan': 2, // 要求使用 isNaN() 检查 NaN
'valid-typeof': 2, // 强制 typeof 表达式与有效的字符串进行比较
'wrap-iife': [2, 'any'], // 要求 IIFE 使用括号括起来
'yield-star-spacing': [2, 'both'], // 强制在 yield* 表达式中 * 周围使用空格
yoda: [2, 'never'], // 要求或禁止 “Yoda” 条件
'prefer-const': 2, // 要求使用 const 声明那些声明后不再被修改的变量
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, // 禁用 debugger
'object-curly-spacing': [2, 'always', {
objectsInObjects: false,
}], // 强制在大括号中使用一致的空格
'vue/no-use-v-if-with-v-for': ['error', {
allowUsingIterationVar: true,
}],
'array-bracket-spacing': [2, 'never'], // 强制数组方括号中使用一致的空格
},
}

3、 创建.eslintignore文件

1
2
3
4
node_modules
dist/
test
build/

4、package.json文件

1
2
3
"scripts": {
"lint": "eslint --ext .js,.vue src --fix"
},

5、vscode 保存自动格式化

安装Vetur插件

把下列勾选项去掉避免和eslint冲突

安装eslint插件

配置setting.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"eslint.autoFixOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"html",
"vue"
],
"eslint.alwaysShowStatus": true,
}


stylelint 配置

1、安装 相应npm包

1
npm i -D stylelint stylelint-config-airbnb

如果使用标准规范只需改为 安装 stylelint-config-stand即可

2、安装适配预处理语法的插件(sass)

1
npm i -D stylelint-scss

3、安装stylelint-config-airbnb缺失依赖

1
npm i stylelint-order -D

4、创建.stylelintrc.js文件

更多规则配置中文文档 http://stylelint.docschina.org/user-guide/rules/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module.exports = {
ignoreFiles: ["**/*.js", "src/assets/css/element-variables.scss", "theme/"],
extends: ["stylelint-config-airbnb"],
plugins: ['stylelint-scss'],
rules: {
// 不要使用已被 autoprefixer 支持的浏览器前缀
'media-feature-name-no-vendor-prefix': true,
'at-rule-no-vendor-prefix': true,
'selector-no-vendor-prefix': true,
'property-no-vendor-prefix': true,
'value-no-vendor-prefix': true,
// 最多允许嵌套20层,去掉默认的最多2层
'max-nesting-depth': 20,
// 颜色值要小写
'color-hex-case': 'lower',
// 颜色值能短则短
'color-hex-length': 'short',
}
};

5、package.json文件

1
2
3
"scripts": {
"stylelint": "stylelint **/*.{html,vue,css,sass,scss} --fex"
},

6、vscode 保存自动格式化

安装stylelint插件

配置settings.json

1
2
3
4
5
{
"editor.codeActionsOnSave": {
"source.fixAll.stylelint": true
}
}


git提交检测自动格式化保存

安装husky

1
npm i husky -D

安装lint-staged

1
npm i lint-staged -D

配置package.json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"husky": {
"hooks": {
"pre-commit": "lint-staged",
}
},
"lint-staged": {
"src/**/*.{js,vue}": [
"eslint --ext .js,.vue src --fix",
"git add"
],
"src/**/*.{css,scss,sass,vue}": [
"stylelint --fix",
"git add"
]
}

git commit 规范

1
2
3
4
5
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

大致分为三个部分(使用空行分割):

  1. 标题行: 必填, 描述主要修改类型和内容
  2. 主题内容: 描述为什么修改, 做了什么样的修改, 以及开发的思路等等
  3. 页脚注释: 可以写注释,BUG 号链接

type: commit 的类型

  • feat: 新功能、新特性
  • fix: 修改 bug
  • perf: 更改代码,以提高性能
  • refactor: 代码重构(重构,在不影响代码内部行为、功能下的代码修改)
  • docs: 文档修改
  • style: 代码格式修改, 注意不是 css 修改(例如分号修改)
  • test: 测试用例新增、修改
  • build: 影响项目构建或依赖项修改
  • revert: 恢复上一次提交
  • ci: 持续集成相关文件修改
  • chore: 其他修改(不在上述类型中的修改)
  • release: 发布新版本
  • workflow: 工作流相关文件修改
  1. scope: commit 影响的范围, 比如: route, component, utils, build…
  2. subject: commit 的概述
  3. body: commit 具体修改内容, 可以分为多行.
  4. footer: 一些备注, 通常是 BREAKING CHANGE 或修复的 bug 的链接.

git 提交检测规范

创建和package.json同级verify-commit.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const msgPath = process.env.HUSKY_GIT_PARAMS
const msg = require('fs')
.readFileSync(msgPath, 'utf-8')
.trim()

const commitRE = /^(feat|fix|docs|style|refactor|perf|test|workflow|build|ci|chore|release|workflow)(\(.+\))?: .{1,50}/

if (!commitRE.test(msg)) {
console.log()
console.error(`
不合法的 commit 消息格式。
请前往 README.md 文件 查看 git commit 规范
`)

process.exit(1)
}

配置 package.json

1
2
3
4
5
6
7
8
{
"husky": {
"hooks": {
...
"commit-msg": "node verify-commit.js"
}
},
}

路径别名提示(vscode)

  • 安装 Path Intellisense
  • 根目录下创建jsconfig.json文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    {
    "compilerOptions": {
    "target": "es6",
    "baseUrl": ".",
    "paths": { // 可以自定义其他别名提示
    "@/*": [
    "src/*"
    ]
    }
    },
    "exclude": [
    "node_modules",
    "dist"
    ],
    "include": [
    "src/**/*"
    ]
    }

终于整理完了。留着之后创建项目的时候用