0
# ESLint Configuration
1
2
Pre-configured ESLint setup with React, TypeScript, and Jest support, plus legacy fabric configuration compatibility. The package provides comprehensive ESLint configurations optimized for modern JavaScript and TypeScript development.
3
4
## Capabilities
5
6
### Main ESLint Configuration
7
8
Modern ESLint configuration with React, TypeScript, and Jest support.
9
10
```typescript { .api }
11
// ESLint configuration object (CommonJS module.exports)
12
// Location: src/config/eslint/index.ts
13
const eslintConfig: {
14
parser: string;
15
plugins: string[];
16
settings: {
17
react: {
18
version: string;
19
};
20
};
21
env: {
22
browser: boolean;
23
node: boolean;
24
es2022: boolean;
25
jest: boolean;
26
};
27
rules: Record<string, any>;
28
overrides: Array<{
29
parser?: string;
30
plugins?: string[];
31
files: string[];
32
rules: Record<string, any>;
33
settings?: any;
34
extends?: string[];
35
}>;
36
parserOptions: {
37
ecmaFeatures: {
38
jsx: boolean;
39
};
40
babelOptions: {
41
babelrc: boolean;
42
configFile: boolean;
43
browserslistConfigFile: boolean;
44
presets: string[];
45
};
46
requireConfigFile: boolean;
47
warnOnUnsupportedTypeScriptVersion: boolean;
48
};
49
};
50
```
51
52
**Key Features:**
53
- **Parser**: Babel ESLint parser for JavaScript/JSX
54
- **Plugins**: React and React Hooks plugins
55
- **Environment**: Browser, Node.js, ES2022, and Jest environments
56
- **TypeScript Override**: @typescript-eslint parser for .ts/.tsx files
57
- **Jest Override**: Jest plugin for test files
58
- **Babel Integration**: Uses @umijs/babel-preset-umi
59
60
**Usage:**
61
62
```javascript
63
// .eslintrc.js
64
module.exports = require("@umijs/lint/dist/config/eslint");
65
```
66
67
### Legacy ESLint Configuration
68
69
Legacy fabric-compatible ESLint configuration for backward compatibility.
70
71
```typescript { .api }
72
// Legacy ESLint configuration object (CommonJS module.exports)
73
// Location: src/config/eslint/legacy.ts
74
const eslintLegacyConfig: {
75
extends: string[];
76
parser: string;
77
plugins: string[];
78
env: {
79
browser: boolean;
80
node: boolean;
81
es6: boolean;
82
mocha: boolean;
83
jest: boolean;
84
jasmine: boolean;
85
};
86
rules: Record<string, any>;
87
overrides: Array<{
88
files: string[];
89
parser: string;
90
rules: Record<string, any>;
91
extends: string[];
92
}>;
93
parserOptions: {
94
ecmaFeatures: {
95
jsx: boolean;
96
};
97
babelOptions: {
98
babelrc: boolean;
99
configFile: boolean;
100
browserslistConfigFile: boolean;
101
presets: string[];
102
};
103
requireConfigFile: boolean;
104
warnOnUnsupportedTypeScriptVersion: boolean;
105
};
106
};
107
```
108
109
**Key Features:**
110
- **Extends**: Prettier and React recommended configurations
111
- **Legacy Support**: Compatible with @umijs/fabric configurations
112
- **Test Frameworks**: Mocha, Jest, and Jasmine support
113
- **TypeScript**: @typescript-eslint recommended rules for TypeScript files
114
115
**Usage:**
116
117
```javascript
118
// .eslintrc.js for legacy projects
119
module.exports = require("@umijs/lint/dist/config/eslint/legacy");
120
```
121
122
### ESLint Setup Module
123
124
Module resolution patch for ESLint plugins.
125
126
```typescript { .api }
127
// ESLint plugin resolution patch
128
// Location: src/config/eslint/setup.ts
129
// Automatically patches ESLint plugin resolve logic using @rushstack/eslint-patch
130
```
131
132
**Purpose:**
133
- Resolves ESLint plugin loading issues in monorepos
134
- Applied automatically when ESLint configurations are loaded
135
- Uses @rushstack/eslint-patch for modern module resolution
136
137
### Rule Sets
138
139
#### Recommended Rules
140
141
Modern recommended rules for Umi.js projects.
142
143
```typescript { .api }
144
// Location: src/config/eslint/rules/recommended.ts
145
const recommendedRules: Record<string, number | any[]>;
146
const jestRules: Record<string, number | any[]>;
147
const typescriptRules: Record<string, number | any[]>;
148
```
149
150
**Rule Categories:**
151
- **Core ESLint**: Array callback return, eqeqeq, guard-for-in, no-eval, etc.
152
- **React**: JSX key, no duplicate props, hooks rules, etc.
153
- **Jest**: No focused tests, valid expect, no standalone expect, etc.
154
- **TypeScript**: Ban types, no unused vars, consistent type imports, etc.
155
156
#### Fabric Legacy Rules
157
158
Legacy rules from @umijs/fabric for backward compatibility.
159
160
```typescript { .api }
161
// Location: src/config/eslint/rules/fabric.ts
162
const fabricRules: Record<string, number | any[]>;
163
const fabricTypescriptRules: Record<string, number | any[]>;
164
```
165
166
**Rule Categories:**
167
- **Legacy ESLint**: Based on @umijs/fabric configuration
168
- **React**: Display name, prop types, self-closing components, etc.
169
- **TypeScript**: Array type, method signature style, typedef, etc.
170
171
## Configuration Examples
172
173
### Basic Project Setup
174
175
```javascript
176
// .eslintrc.js
177
module.exports = require("@umijs/lint/dist/config/eslint");
178
```
179
180
### TypeScript Project
181
182
```json
183
// .eslintrc.json
184
{
185
"extends": ["@umijs/lint/dist/config/eslint"]
186
}
187
```
188
189
### Legacy Project Migration
190
191
```javascript
192
// .eslintrc.js
193
module.exports = require("@umijs/lint/dist/config/eslint/legacy");
194
```
195
196
### Custom Rule Overrides
197
198
```javascript
199
// .eslintrc.js
200
const baseConfig = require("@umijs/lint/dist/config/eslint");
201
202
module.exports = {
203
...baseConfig,
204
rules: {
205
...baseConfig.rules,
206
"no-console": "warn",
207
"react/prop-types": "off"
208
}
209
};
210
```
211
212
## File Pattern Matching
213
214
### TypeScript Override
215
- **Files**: `**/*.{ts,tsx}`
216
- **Parser**: @typescript-eslint/parser
217
- **Rules**: TypeScript-specific rules applied
218
219
### Jest Override
220
- **Files**: `**/*.{test,spec,unit,e2e}.{ts,tsx,js,jsx}`
221
- **Plugin**: Jest plugin enabled
222
- **Rules**: Jest-specific rules applied
223
- **Settings**: Automatic Jest version detection
224
225
## Jest Version Detection
226
227
```typescript { .api }
228
/**
229
* Automatically detects Jest version for optimal rule configuration
230
* Falls back to version 29 if Jest is not found
231
* @returns Jest version number
232
*/
233
function detectJestVersion(): number;
234
```
235
236
**Behavior:**
237
- Attempts to resolve `jest/package.json` from current working directory
238
- Extracts version from package.json
239
- Returns 29 as fallback if Jest is not installed
240
- Used for Jest plugin configuration in test file overrides
241
242
## Rule Severity Levels
243
244
- **0 or "off"**: Rule is disabled
245
- **1 or "warn"**: Rule generates warnings
246
- **2 or "error"**: Rule generates errors (fails linting)
247
248
## Built-in Plugins
249
250
### React Plugin
251
- JSX syntax support
252
- React-specific rules and best practices
253
- Automatic React version detection
254
255
### React Hooks Plugin
256
- Rules of Hooks enforcement
257
- Exhaustive dependencies checking
258
259
### TypeScript Plugin
260
- Type-aware linting rules
261
- TypeScript syntax support
262
- Import/export validation
263
264
### Jest Plugin
265
- Test-specific linting rules
266
- Jest API validation
267
- Test structure enforcement