generated from SystemLight/T-webpack5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eslintrc.js
184 lines (167 loc) · 4.31 KB
/
.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
const path = require('path')
const fs = require('fs')
// https://eslint.org/docs/user-guide/configuring/
class EslintRecommendConfig {
constructor() {
this.cwd = process.cwd()
this.isTsProject = fs.existsSync(path.resolve(this.cwd, 'tsconfig.json'))
this.packageJSON = require(path.join(this.cwd, 'package.json')) // package.json文件信息对象
this.dependencies = Object.keys({
...this.packageJSON['devDependencies'],
...this.packageJSON['dependencies']
}) // 项目依赖库数组,用于判定包含什么框架
this.config = {}
}
build() {
this.buildBasic()
this.buildParser()
this.buildFormatter()
this.buildPlugin()
this.buildConfig()
this.toConfig()
return this
}
buildBasic() {
this.config.root = true
this.config.env = {
browser: true,
es2022: true,
node: true
}
this.config.settings = {}
this.config.extends = [
'eslint:recommended'
]
if (this.isTsProject) {
this.config.extends.push(
'plugin:@typescript-eslint/recommended' // @typescript-eslint/eslint-plugin
)
}
if (this.dependencies.includes('react')) {
this.config.settings.react = {
version: 'detect'
}
this.config.extends.push(
'plugin:react/recommended', // eslint-plugin-react
'plugin:react-hooks/recommended' // eslint-plugin-react-hooks
)
}
return this
}
buildParser() {
// ts解析器:https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/parser
// js解析器:https://www.npmjs.com/package/@babel/eslint-parser
if (this.isTsProject) {
this.config.parser = '@typescript-eslint/parser'
this.config.parserOptions = {}
} else {
this.config.parser = '@babel/eslint-parser'
let presets = ['@babel/preset-env']
let plugins = []
if (this.dependencies.includes('react')) {
presets.push('@babel/preset-react')
}
if (this.dependencies.includes('@babel/plugin-proposal-decorators')) {
plugins.push(
// 装饰器属性,https://babel.dev/docs/en/babel-plugin-proposal-decorators
[
'@babel/plugin-proposal-decorators',
{
legacy: true
}
]
)
}
this.config.parserOptions = {
requireConfigFile: false,
babelOptions: {
presets: presets,
plugins: plugins
}
}
}
Object.assign(this.config.parserOptions, {
sourceType: 'module',
ecmaVersion: 2022
})
return this
}
buildFormatter() {
return this
}
buildPlugin() {
return this
}
buildConfig() {
this.config.rules = {
'require-jsdoc': 'off',
'no-control-regex': 'off',
'no-invalid-this': 'off',
'linebreak-style': 'off',
'max-len': 'off',
'no-unused-vars': 'off',
'quotes': [
'error',
'single'
],
'semi': [
'error',
'never'
],
'arrow-parens': [
'error',
'always'
],
'comma-dangle': [
'error',
{
'arrays': 'never',
'objects': 'never',
'imports': 'never',
'exports': 'never',
'functions': 'never'
}
],
'indent': [
'error',
2,
{
'SwitchCase': 1
}
],
'padded-blocks': [
'error',
'never'
],
'space-before-function-paren': [
'error',
{
'asyncArrow': 'always',
'anonymous': 'always',
'named': 'never'
}
],
'no-multiple-empty-lines': [
'error',
{
'max': 1
}
]
}
if (this.isTsProject) {
Object.assign(this.config.rules, {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-this-alias': 'off'
})
}
return this
}
toConfig(debug) {
if (debug) {
console.log(this.config)
}
return this.config
}
}
module.exports = new EslintRecommendConfig().build().toConfig()