-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy patheslint.config.mjs
429 lines (377 loc) · 10 KB
/
eslint.config.mjs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// ROOT ESLint Configuration
//
/* eslint-env node */
import url from 'node:url';
import path from 'node:path';
import js from '@eslint/js';
import globals from 'globals';
import babel from '@babel/eslint-plugin';
import babelParser from '@babel/eslint-parser';
import typescript from '@typescript-eslint/eslint-plugin';
import typescriptParser from '@typescript-eslint/parser';
import prettier from 'eslint-config-prettier';
import jest from 'eslint-plugin-jest';
import jestDom from 'eslint-plugin-jest-dom';
import cypress from 'eslint-plugin-cypress';
import importPlugin from 'eslint-plugin-import';
import testingLibrary from 'eslint-plugin-testing-library';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const ecmaVersion = 'latest';
const impliedStrict = true;
const tsconfigRootDir = __dirname;
//
// Plugins
//
// Plugins that apply to ALL envs
const basePlugins = {
'@babel': babel, // @see https://www.npmjs.com/package/@babel/eslint-plugin
};
const importPluginSettings = {
'import/resolver': {
node: {
extensions: [
'.js',
'.jsx',
'.cts',
'.mjs',
'.ts',
'.tsx',
'.cts',
'.mts',
],
moduleDirectory: ['node_modules', 'src/', 'test/'],
},
typescript: {
alwaysTryTypes: true,
},
},
};
//
// Globals
//
// Globals that apply to ALL envs
const baseGlobals = {
// anything in addition to what `languageOptions.ecmaVersion` provides
// @see https://eslint.org/docs/latest/use/configure/language-options#predefined-global-variables
};
// Globals for repo tooling scripts
const toolingGlobals = {
...globals.node,
};
// Globals for browser-based source code
const browserGlobals = {
...globals.browser,
};
// Globals for test files
const testGlobals = {
...globals.node,
...globals.jest,
...cypress.environments.globals.globals,
// `globals.browser` defines this global but it's also part of the `testing-library`
// API so needs to be overwritable to avoid ESLint's `no-redeclare` rule
screen: 'off',
};
// Globals for BUNDLED (Webpack, Rollup, etc) source code
// NOTE: these must also be defined in <repo>/src/globals.d.ts referenced in the
// <repo>/tsconfig.json as well as the `globals` property in <repo>/jest.config.mjs
const bundlerGlobals = {};
//
// Base rules
// @see http://eslint.org/docs/rules/RULE-NAME
//
const baseRules = {
...js.configs.recommended.rules,
'no-regex-spaces': 'off',
'no-await-in-loop': 'error',
'no-async-promise-executor': 'error',
'no-misleading-character-class': 'error',
'no-unsafe-optional-chaining': 'error',
//// Best practices
curly: 'error',
'default-case': 'error',
eqeqeq: 'error',
'guard-for-in': 'error',
'no-alert': 'error',
'no-caller': 'error',
'no-console': 'error',
'no-else-return': 'error',
'no-eq-null': 'error',
'no-eval': 'error',
'no-lone-blocks': 'error',
'no-loop-func': 'error',
'no-multi-spaces': 'error',
'no-new': 'off',
'no-new-func': 'error',
'no-new-wrappers': 'error',
'no-throw-literal': 'error',
'no-warning-comments': [
'error',
{
terms: ['DEBUG', 'FIXME', 'HACK'],
location: 'start',
},
],
//// Strict mode
strict: ['error', 'function'],
//// Variables
'no-catch-shadow': 'error',
'no-shadow': 'error',
'no-unused-vars': [
'error',
{
args: 'none',
caughtErrors: 'none',
vars: 'local',
},
],
'no-use-before-define': 'error',
//// Stylistic issues
// NONE: Prettier will take care of these by reformatting the code on commit,
// save a few exceptions.
// Prettier will format using single quotes per .prettierrc.js settings, but
// will not require single quotes instead of backticks/template strings
// when interpolation isn't used, so this rule will catch those cases
quotes: [
'error',
'single',
{
avoidEscape: true,
allowTemplateLiterals: false,
},
],
//// ECMAScript 6 (non-stylistic issues only)
'no-duplicate-imports': ['error', { includeExports: true }],
'no-useless-constructor': 'error',
'no-var': 'error',
'prefer-const': 'error',
};
//
// TypeScript-specific rules
//
const typescriptRules = {
...typescript.configs['recommended-type-checked'].rules,
// AFTER TypeScript rules to turn off `import` rules that TypeScript covers
...importPlugin.flatConfigs.typescript.rules,
};
//
// TypeScript-specific rules
//
const testRules = {
//// jest plugin
'jest/no-disabled-tests': 'error',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/valid-title': 'error',
// doesn't work well when expect() used with Cypress API
'jest/valid-expect': 'off',
//// jest-dom plugin
// this rule is buggy, and doesn't seem to work well with the Testing Library's queries
'jest-dom/prefer-in-document': 'off',
//// testing-library plugin
// this prevents expect(document.querySelector('foo')), which is useful because not
// all elements can be found using RTL queries (sticking to RTL queries probably
// means less fragile tests, but then there are things we wouldn't be able to
// test like whether something renders in Light mode or Dark mode as expected)
'testing-library/no-node-access': 'off',
// we use custom queries, which don't get added to `screen` (that's a miss in RTL, IMO),
// which means we _must_ destructure the result from `render()` in order to get to
// our custom queries
'testing-library/prefer-screen-queries': 'off',
// not much value in this one, and it's not sophisticated enough to detect all usage
// scenarios so we get false-positives
'testing-library/await-async-utils': 'off',
//// Cypress plugin
...cypress.configs.recommended.rules,
};
//
// Config generators
//
/**
* Project scripts.
* @param {boolean} isModule
* @param {boolean} isTypescript Ignored if `isModule=false`
* @returns {Object} ESLint config.
*/
const createToolingConfig = (isModule = true, isTypescript = false) => ({
files: isModule
? isTypescript
? ['**/*.{ts,mts}']
: ['**/*.{js,mjs}']
: ['**/*.{js,cjs}'],
ignores: ['src/**/*.*', 'test/**/*.*'],
plugins: {
...basePlugins,
...(isModule ? { import: importPlugin } : {}),
...(isTypescript ? { '@typescript-eslint': typescript } : {}),
},
languageOptions: {
ecmaVersion,
parser: isTypescript ? typescriptParser : babelParser,
parserOptions: {
sourceType: isModule ? 'module' : 'script',
...(isModule && isTypescript
? {
project: true,
tsconfigRootDir,
}
: {}),
ecmaFeatures: {
impliedStrict,
jsx: false,
},
},
globals: {
...baseGlobals,
...toolingGlobals,
},
},
settings: {
...(isModule ? importPluginSettings : {}),
},
rules: {
...baseRules,
...(isModule ? importPlugin.flatConfigs.recommended.rules : {}), // BEFORE TypeScript rules
...(isModule && isTypescript ? typescriptRules : {}),
'no-console': 'off', // OK in repo scripts
},
});
/**
* JavaScript source files.
* @returns ESLint config.
*/
const createSourceJSConfig = () => ({
files: ['src/**/*.js'],
plugins: {
...basePlugins,
import: importPlugin,
},
languageOptions: {
ecmaVersion,
parser: babelParser,
parserOptions: {
sourceType: 'module',
ecmaFeatures: {
impliedStrict,
jsx: false,
},
},
globals: {
...baseGlobals,
...bundlerGlobals,
...browserGlobals,
},
},
settings: {
...importPluginSettings,
},
rules: {
...baseRules,
...importPlugin.flatConfigs.recommended.rules,
},
});
const createSourceTSConfig = () => ({
files: ['index.d.ts', 'src/**/*.ts'],
plugins: {
...basePlugins,
import: importPlugin,
'@typescript-eslint': typescript,
},
languageOptions: {
ecmaVersion,
parser: typescriptParser,
parserOptions: {
project: true,
tsconfigRootDir,
sourceType: 'module',
ecmaFeatures: {
impliedStrict,
jsx: false,
},
},
globals: {
...baseGlobals,
...bundlerGlobals,
...browserGlobals,
},
},
rules: {
...baseRules,
...importPlugin.flatConfigs.recommended.rules, // BEFORE TypeScript rules
...typescriptRules,
},
});
const createTestConfig = (isTypescript = false) => ({
files: isTypescript ? ['test/**/*.ts'] : ['test/**/*.js'],
plugins: {
...basePlugins,
import: importPlugin,
...(isTypescript ? { '@typescript-eslint': typescript } : {}),
jest,
'jest-dom': jestDom,
'testing-library': testingLibrary,
cypress,
},
languageOptions: {
ecmaVersion,
parser: isTypescript ? typescriptParser : babelParser,
parserOptions: {
...(isTypescript
? {
project: true,
tsconfigRootDir,
}
: {}),
sourceType: 'module',
ecmaFeatures: {
impliedStrict,
jsx: false,
},
},
globals: {
...baseGlobals,
...bundlerGlobals, // because tests execute code that also gets bundled
...browserGlobals,
...testGlobals,
},
},
rules: {
...baseRules,
...importPlugin.flatConfigs.recommended.rules, // BEFORE TypeScript rules
...(isTypescript ? typescriptRules : {}),
...testRules,
},
});
export default [
// Ignores
{
ignores: [
// third-party
'**/node_modules/',
// build output
'dist/**',
'**/*-bundle.js',
// test output
'coverage/**',
],
},
// Tooling Configs
createToolingConfig(false), // CJS scripts
createToolingConfig(true), // ESM scripts
createToolingConfig(true, true), // TS scripts
// Source Configs
createSourceJSConfig(), // Plain JS source
createSourceTSConfig(), // Plain TS source
// Test Configs
createTestConfig(), // JS tests
createTestConfig(true), // TS tests
// Prettier
// ALWAYS LAST: disable style rules that conflict with prettier
// @see https://typescript-eslint.io/troubleshooting/formatting#suggested-usage---prettier
{
plugins: {
prettier,
},
rules: prettier.rules,
},
];