-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
eslint.config.mjs
546 lines (510 loc) · 16.7 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// @ts-check
import url from 'node:url';
import { FlatCompat } from '@eslint/eslintrc';
import eslint from '@eslint/js';
import tseslintInternalPlugin from '@typescript-eslint/eslint-plugin-internal';
import deprecationPlugin from 'eslint-plugin-deprecation';
import eslintCommentsPlugin from 'eslint-plugin-eslint-comments';
import eslintPluginPlugin from 'eslint-plugin-eslint-plugin';
import importPlugin from 'eslint-plugin-import';
import jestPlugin from 'eslint-plugin-jest';
import jsdocPlugin from 'eslint-plugin-jsdoc';
import jsxA11yPlugin from 'eslint-plugin-jsx-a11y';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import simpleImportSortPlugin from 'eslint-plugin-simple-import-sort';
import unicornPlugin from 'eslint-plugin-unicorn';
import globals from 'globals';
import tseslint from 'typescript-eslint';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const compat = new FlatCompat({ baseDirectory: __dirname });
export default tseslint.config(
// register all of the plugins up-front
{
// note - intentionally uses computed syntax to make it easy to sort the keys
plugins: {
['@typescript-eslint']: tseslint.plugin,
['@typescript-eslint/internal']: tseslintInternalPlugin,
['deprecation']: deprecationPlugin,
['eslint-comments']: eslintCommentsPlugin,
['eslint-plugin']: eslintPluginPlugin,
['import']: importPlugin,
['jest']: jestPlugin,
['jsdoc']: jsdocPlugin,
['jsx-a11y']: jsxA11yPlugin,
['react-hooks']: reactHooksPlugin,
['react']: reactPlugin,
['simple-import-sort']: simpleImportSortPlugin,
['unicorn']: unicornPlugin,
},
},
{
// config with just ignores is the replacement for `.eslintignore`
ignores: [
'**/jest.config.js',
'**/node_modules/**',
'**/dist/**',
'**/fixtures/**',
'**/coverage/**',
'**/__snapshots__/**',
'**/.docusaurus/**',
'**/build/**',
// Files copied as part of the build
'packages/types/src/generated/**/*.ts',
// Playground types downloaded from the web
'packages/website/src/vendor',
// see the file header in eslint-base.test.js for more info
'packages/rule-tester/tests/eslint-base',
],
},
// extends ...
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
jsdocPlugin.configs['flat/recommended-typescript-error'],
// base config
{
languageOptions: {
globals: {
...globals.es2020,
...globals.node,
},
parserOptions: {
allowAutomaticSingleRunInference: true,
cacheLifetime: {
// we pretty well never create/change tsconfig structure - so no need to ever evict the cache
// in the rare case that we do - just need to manually restart their IDE.
glob: 'Infinity',
},
project: [
'tsconfig.json',
'packages/*/tsconfig.json',
/**
* We are currently in the process of transitioning to nx's out of the box structure and
* so need to manually specify converted packages' tsconfig.build.json and tsconfig.spec.json
* files here for now in addition to the tsconfig.json glob pattern.
*
* TODO(#4665): Clean this up once all packages have been transitioned.
*/
'packages/scope-manager/tsconfig.build.json',
'packages/scope-manager/tsconfig.spec.json',
],
tsconfigRootDir: __dirname,
warnOnUnsupportedTypeScriptVersion: false,
},
},
rules: {
// make sure we're not leveraging any deprecated APIs
'deprecation/deprecation': 'error',
// TODO: https://github.com/typescript-eslint/typescript-eslint/issues/8538
'@typescript-eslint/no-confusing-void-expression': 'off',
//
// our plugin :D
//
'@typescript-eslint/ban-ts-comment': [
'error',
{
'ts-expect-error': 'allow-with-description',
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
minimumDescriptionLength: 5,
},
],
'@typescript-eslint/consistent-type-imports': [
'error',
{ prefer: 'type-imports', disallowTypeAnnotations: true },
],
'@typescript-eslint/explicit-function-return-type': [
'error',
{ allowIIFEs: true },
],
'@typescript-eslint/no-explicit-any': 'error',
'no-constant-condition': 'off',
'@typescript-eslint/no-unnecessary-condition': [
'error',
{ allowConstantLoopConditions: true },
],
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/prefer-literal-enum-member': [
'error',
{
allowBitwiseExpressions: true,
},
],
'@typescript-eslint/prefer-string-starts-ends-with': [
'error',
{
allowSingleElementEquality: 'always',
},
],
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/restrict-template-expressions': [
'error',
{
allowNumber: true,
allowBoolean: true,
allowAny: true,
allowNullish: true,
allowRegExp: true,
},
],
'@typescript-eslint/no-unused-vars': [
'error',
{
caughtErrors: 'all',
varsIgnorePattern: '^_',
argsIgnorePattern: '^_',
},
],
'@typescript-eslint/prefer-nullish-coalescing': [
'error',
{
ignoreConditionalTests: true,
ignorePrimitives: true,
},
],
//
// Internal repo rules
//
'@typescript-eslint/internal/no-poorly-typed-ts-props': 'error',
'@typescript-eslint/internal/no-relative-paths-to-internal-packages':
'error',
'@typescript-eslint/internal/no-typescript-default-import': 'error',
'@typescript-eslint/internal/prefer-ast-types-enum': 'error',
//
// eslint-base
//
curly: ['error', 'all'],
eqeqeq: [
'error',
'always',
{
null: 'never',
},
],
'logical-assignment-operators': 'error',
'no-else-return': 'error',
'no-mixed-operators': 'error',
'no-console': 'error',
'no-process-exit': 'error',
'no-fallthrough': [
'error',
{ commentPattern: '.*intentional fallthrough.*' },
],
'one-var': ['error', 'never'],
//
// eslint-plugin-eslint-comment
//
// require a eslint-enable comment for every eslint-disable comment
'eslint-comments/disable-enable-pair': [
'error',
{
allowWholeFile: true,
},
],
// disallow a eslint-enable comment for multiple eslint-disable comments
'eslint-comments/no-aggregating-enable': 'error',
// disallow duplicate eslint-disable comments
'eslint-comments/no-duplicate-disable': 'error',
// disallow eslint-disable comments without rule names
'eslint-comments/no-unlimited-disable': 'error',
// disallow unused eslint-disable comments
'eslint-comments/no-unused-disable': 'error',
// disallow unused eslint-enable comments
'eslint-comments/no-unused-enable': 'error',
// disallow ESLint directive-comments
'eslint-comments/no-use': [
'error',
{
allow: [
'eslint-disable',
'eslint-disable-line',
'eslint-disable-next-line',
'eslint-enable',
'global',
],
},
],
//
// eslint-plugin-import
//
// enforces consistent type specifier style for named imports
'import/consistent-type-specifier-style': 'error',
// disallow non-import statements appearing before import statements
'import/first': 'error',
// Require a newline after the last import/require in a group
'import/newline-after-import': 'error',
// Forbid import of modules using absolute paths
'import/no-absolute-path': 'error',
// disallow AMD require/define
'import/no-amd': 'error',
// forbid default exports - we want to standardize on named exports so that imported names are consistent
'import/no-default-export': 'error',
// disallow imports from duplicate paths
'import/no-duplicates': 'error',
// Forbid the use of extraneous packages
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: true,
peerDependencies: true,
optionalDependencies: false,
},
],
// Forbid mutable exports
'import/no-mutable-exports': 'error',
// Prevent importing the default as if it were named
'import/no-named-default': 'error',
// Prohibit named exports
'import/no-named-export': 'off', // we want everything to be a named export
// Forbid a module from importing itself
'import/no-self-import': 'error',
// Require modules with a single export to use a default export
'import/prefer-default-export': 'off', // we want everything to be named
// enforce a sort order across the codebase
'simple-import-sort/imports': 'error',
//
// eslint-plugin-jsdoc
//
// We often use @remarks or other ad-hoc tag names
'jsdoc/check-tag-names': 'off',
// https://github.com/gajus/eslint-plugin-jsdoc/issues/1169
'jsdoc/check-param-names': 'off',
// https://github.com/gajus/eslint-plugin-jsdoc/issues/1175
'jsdoc/require-jsdoc': 'off',
'jsdoc/require-param': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/require-yields': 'off',
'jsdoc/tag-lines': 'off',
//
// eslint-plugin-unicorn
//
'jsdoc/informative-docs': 'error',
'unicorn/no-typeof-undefined': 'error',
},
},
{
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
rules: {
// turn off other type-aware rules
'deprecation/deprecation': 'off',
'@typescript-eslint/internal/no-poorly-typed-ts-props': 'off',
// turn off rules that don't apply to JS code
'@typescript-eslint/explicit-function-return-type': 'off',
},
},
//
// test file linting
//
// define the jest globals for all test files
{
files: ['packages/*/tests/**/*.{ts,tsx,cts,mts}'],
languageOptions: {
globals: {
...jestPlugin.environments.globals.globals,
},
},
},
// test file specific configuration
{
files: [
'packages/*/tests/**/*.spec.{ts,tsx,cts,mts}',
'packages/*/tests/**/*.test.{ts,tsx,cts,mts}',
'packages/*/tests/**/spec.{ts,tsx,cts,mts}',
'packages/*/tests/**/test.{ts,tsx,cts,mts}',
'packages/parser/tests/**/*.{ts,tsx,cts,mts}',
'packages/integration-tests/tools/integration-test-base.ts',
'packages/integration-tests/tools/pack-packages.ts',
],
rules: {
'@typescript-eslint/no-empty-function': [
'error',
{ allow: ['arrowFunctions'] },
],
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'jest/no-disabled-tests': 'error',
'jest/no-focused-tests': 'error',
'jest/no-alias-methods': 'error',
'jest/no-identical-title': 'error',
'jest/no-jasmine-globals': 'error',
'jest/no-test-prefixes': 'error',
'jest/no-done-callback': 'error',
'jest/no-test-return-statement': 'error',
'jest/prefer-to-be': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-length': 'error',
'jest/prefer-spy-on': 'error',
'jest/valid-expect': 'error',
'jest/no-deprecated-functions': 'error',
},
},
// plugin rule tests
{
files: [
'packages/eslint-plugin-internal/tests/rules/**/*.test.{ts,tsx,cts,mts}',
'packages/eslint-plugin-tslint/tests/rules/**/*.test.{ts,tsx,cts,mts}',
'packages/eslint-plugin/tests/rules/**/*.test.{ts,tsx,cts,mts}',
'packages/eslint-plugin/tests/eslint-rules/**/*.test.{ts,tsx,cts,mts}',
],
rules: {
'@typescript-eslint/internal/plugin-test-formatting': 'error',
},
},
//
// tools and tests
//
{
files: [
'**/tools/**/*.{ts,tsx,cts,mts}',
'**/tests/**/*.{ts,tsx,cts,mts}',
'packages/repo-tools/**/*.{ts,tsx,cts,mts}',
'packages/integration-tests/**/*.{ts,tsx,cts,mts}',
],
rules: {
// allow console logs in tools and tests
'no-console': 'off',
},
},
{
files: ['eslint.config.{js,cjs,mjs}'],
rules: {
// requirement
'import/no-default-export': 'off',
},
},
//
// plugin source file linting
//
{
files: [
'packages/eslint-plugin-internal/**/*.{ts,tsx,cts,mts}',
'packages/eslint-plugin-tslint/**/*.{ts,tsx,cts,mts}',
'packages/eslint-plugin/**/*.{ts,tsx,cts,mts}',
],
rules: {
'@typescript-eslint/internal/no-typescript-estree-import': 'error',
},
extends: [...compat.config(eslintPluginPlugin.configs.recommended)],
},
{
files: [
'packages/eslint-plugin-internal/src/rules/**/*.{ts,tsx,cts,mts}',
'packages/eslint-plugin-tslint/src/rules/**/*.{ts,tsx,cts,mts}',
'packages/eslint-plugin/src/configs/**/*.{ts,tsx,cts,mts}',
'packages/typescript-eslint/src/configs/**/*.{ts,tsx,cts,mts}',
'packages/core/src/configs/**/*.{ts,tsx,cts,mts}',
'packages/eslint-plugin/src/rules/**/*.{ts,tsx,cts,mts}',
],
rules: {
'eslint-plugin/require-meta-docs-description': [
'error',
{ pattern: '^(Enforce|Require|Disallow) .+[^. ]$' },
],
// specifically for rules - default exports makes the tooling easier
'import/no-default-export': 'off',
'no-restricted-syntax': [
'error',
{
selector:
'ExportDefaultDeclaration Property[key.name="create"] MemberExpression[object.name="context"][property.name="options"]',
message:
"Retrieve options from create's second parameter so that defaultOptions are applied.",
},
],
},
},
{
files: ['packages/eslint-plugin/src/rules/index.ts'],
rules: {
// enforce alphabetical ordering
'sort-keys': 'error',
'import/order': ['error', { alphabetize: { order: 'asc' } }],
},
},
//
// generated files
//
{
files: [
'packages/scope-manager/src/lib/*.{ts,tsx,cts,mts}',
'packages/eslint-plugin/src/configs/*.{ts,tsx,cts,mts}',
'packages/core/src/configs/*.{ts,tsx,cts,mts}',
],
rules: {
'@typescript-eslint/internal/no-poorly-typed-ts-props': 'off',
'@typescript-eslint/internal/no-typescript-default-import': 'off',
'@typescript-eslint/internal/prefer-ast-types-enum': 'off',
},
},
//
// ast spec linting
//
{
files: ['packages/ast-spec/src/**/*.{ts,tsx,cts,mts}'],
rules: {
// disallow ALL unused vars
'@typescript-eslint/no-unused-vars': ['error', { caughtErrors: 'all' }],
'@typescript-eslint/sort-type-constituents': 'error',
},
},
{
files: ['packages/ast-spec/**/*.{ts,tsx,cts,mts}'],
rules: {
'no-restricted-imports': [
'error',
{
name: '@typescript-eslint/typescript-estree',
message:
'To prevent nx build errors, all `typescript-estree` imports should be done via `packages/ast-spec/tests/util/parsers/typescript-estree-import.ts`.',
},
],
},
},
//
// website linting
//
{
files: ['packages/website/**/*.{ts,tsx,mts,cts,js,jsx}'],
extends: [
...compat.config(jsxA11yPlugin.configs.recommended),
...compat.config(reactPlugin.configs.recommended),
...compat.config(reactHooksPlugin.configs.recommended),
],
rules: {
'@typescript-eslint/internal/prefer-ast-types-enum': 'off',
'import/no-default-export': 'off',
'react/jsx-no-target-blank': 'off',
'react/no-unescaped-entities': 'off',
'react-hooks/exhaustive-deps': 'warn', // TODO: enable it later
},
settings: {
react: {
version: 'detect',
},
},
},
{
files: ['packages/website/src/**/*.{ts,tsx,cts,mts}'],
rules: {
'import/no-default-export': 'off',
// allow console logs in the website to help with debugging things in production
'no-console': 'off',
},
},
{
files: [
'packages/website-eslint/src/mock/**/*.js',
'**/*.d.{ts,tsx,cts,mts}',
],
rules: {
// mocks and declaration files have to mirror their original package
'import/no-default-export': 'off',
},
},
);