forked from recharts/recharts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint.config.mjs
More file actions
502 lines (486 loc) · 13.2 KB
/
eslint.config.mjs
File metadata and controls
502 lines (486 loc) · 13.2 KB
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
import path from 'node:path';
import globals from 'globals';
import { includeIgnoreFile } from '@eslint/compat';
import js from '@eslint/js';
import tseslint from 'typescript-eslint';
import { configs } from 'eslint-config-airbnb-extended/legacy';
import { rules as prettierConfigRules } from 'eslint-config-prettier';
import prettierPlugin from 'eslint-plugin-prettier';
import storybook from 'eslint-plugin-storybook';
import comments from '@eslint-community/eslint-plugin-eslint-comments/configs';
import reactPerf from 'eslint-plugin-react-perf';
import jsdoc from 'eslint-plugin-jsdoc';
const gitignorePath = path.resolve('.', '.gitignore');
const allFiles = [
'**/*.js',
'**/*.cjs',
'**/*.mjs',
'**/*.jsx',
'**/*.ts',
'**/*.cts',
'**/*.mts',
'**/*.tsx',
'**/*.d.ts',
];
const customIgnores = [
{
name: 'x/ignores',
ignores: ['!.storybook'],
},
{
name: 'playwright',
ignores: ['./test-vr/playwright-report', './test-vr/screenshots', './test-vr/playwright'],
},
{
name: 'webpack',
ignores: ['./webpack.config.js'],
},
{
name: 'www autogenerated',
ignores: ['./www/docs'],
},
];
const languageOptions = [
{
name: 'x/language-options',
files: allFiles,
languageOptions: {
globals: {
...globals.browser,
...globals.node,
...globals.jest,
},
},
},
];
const jsConfig = [
// ESLint Recommended Rules
{
name: 'js/config',
...js.configs.recommended,
},
// NOTE: DO NOT ADD ANYTHING AFTER THIS LINE BECAUSE IT WILL BE OVERWRITTEN BY AIRBNB CONFIG
];
const reactConfig = [
// Airbnb React Recommended Config
...configs.react.recommended,
// Airbnb React Hooks Config
...configs.react.hooks,
{
name: 'x/general',
rules: {
'no-var': 'error',
'no-unused-vars': 'off',
'one-var': [
'error',
{
uninitialized: 'always',
},
],
'one-var-declaration-per-line': ['error', 'initializations'],
'no-plusplus': 'off',
'no-bitwise': 'off',
'class-methods-use-this': ['warn', { ignoreClassesWithImplements: 'public-fields' }],
'max-len': 'off',
'prefer-destructuring': 'error',
'object-curly-newline': 'off',
'function-paren-newline': 'off',
'no-restricted-globals': 'error',
'no-multi-assign': 'off',
'no-use-before-define': 'off',
/*
* Typescript already handles no-redeclare, so we don't need to enforce it with ESLint.
* Also, Typescript handles it better because TS can handle function overloads
* which ESLint doesn't.
* See: https://typescript-eslint.io/rules/no-redeclare/
*/
'no-redeclare': 'off',
'no-restricted-imports': [
'error',
{
paths: [
{
name: 'lodash',
message: 'Import [module] from lodash/[module] instead',
},
],
patterns: [
{
group: ['recharts/types/*', 'recharts/src/*'],
message:
'Examples should only show usage of public API. Import from the public API (recharts) instead of internal paths. If a symbol is not exported, but it would be useful in an example, then it should be exported.',
},
],
},
],
},
},
{
name: 'x/import',
rules: {
'import/extensions': 'off',
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: true,
packageDir: ['.', './www'],
},
],
'import/prefer-default-export': 'off',
'import/no-named-as-default': 'error',
'import/no-cycle': 'error',
'import/no-default-export': 'error',
'import/no-import-module-exports': 'error',
'import/no-relative-packages': 'off',
},
},
{
name: 'x/react',
rules: {
'react/jsx-key': [
'error',
{
checkFragmentShorthand: true,
checkKeyMustBeforeSpread: true,
warnOnDuplicates: true,
},
],
'react/no-did-update-set-state': 'warn',
'react/jsx-no-constructed-context-values': 'warn',
'react/no-unused-class-component-methods': 'off',
'react/sort-comp': 'off',
'react/jsx-uses-react': 'error',
'react/jsx-uses-vars': 'error',
'react/react-in-jsx-scope': 'error',
'react/jsx-filename-extension': [
'error',
{
extensions: ['.ts', '.tsx', '.js', '.jsx'],
},
],
'react/jsx-no-bind': 'error',
'react/prop-types': 'off',
'react/no-unused-prop-types': 'off',
'react/require-default-props': 'off',
'react/default-props-match-prop-types': 'off',
'react/function-component-definition': 'off',
/**
* In a chart, using the index as key is acceptable because the order of items
* has meaning. Reordering, or removing elements creates a whole new chart.
* For Recharts, array index is a good identifier.
*/
'react/no-array-index-key': 'off',
'react/no-access-state-in-setstate': 'off',
'react/destructuring-assignment': 'off',
'react/jsx-closing-tag-location': 'error',
'react/no-unused-state': 'error',
'react/prefer-stateless-function': 'error',
'react/jsx-props-no-spreading': 'off',
'react/state-in-constructor': 'off',
'react/static-property-placement': 'off',
},
},
{
name: 'x/jsx-a11y',
rules: {
'jsx-a11y/label-has-associated-control': ['error', { assert: 'either', depth: 2 }],
},
},
];
const typescriptConfig = [
// Typescript ESLint Recommended Config
...tseslint.configs.recommended,
{
name: 'x/typescript-eslint',
rules: {
'@typescript-eslint/no-use-before-define': 'error',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
args: 'after-used',
ignoreRestSiblings: true,
argsIgnorePattern: '^_',
},
],
},
},
];
const storybookConfig = [
// Storybook Config
...storybook.configs['flat/recommended'],
];
const prettierConfig = [
// Prettier Plugin
{
name: 'prettier/plugin/config',
plugins: {
prettier: prettierPlugin,
},
},
// Prettier Config
{
name: 'prettier/config',
rules: {
...prettierConfigRules,
'prettier/prettier': 'error',
'arrow-body-style': 'off',
'prefer-arrow-callback': 'off',
},
},
];
const commentsConfig = [
// ESLint Community ESLint Comments
{
name: comments.recommended.name,
plugins: comments.recommended.plugins,
rules: {
'@eslint-community/eslint-comments/no-unused-disable': 'error',
},
},
];
const overridesConfig = [
{
name: 'x/override-1',
files: ['./test/**/*.tsx'],
rules: {
'react/jsx-no-useless-fragment': 'off',
},
},
{
// https://github.com/airbnb/javascript/issues/1271#issuecomment-548688952
name: 'allow-for-loop',
files: allFiles,
rules: {
'no-restricted-syntax': [
'error',
{
selector: 'LabeledStatement',
message: 'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
},
{
selector: 'WithStatement',
message: '`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
},
],
},
},
{
name: 'allow-continue',
files: allFiles,
rules: {
'no-continue': 'off',
},
},
{
name: 'x/override-2',
files: ['./storybook/**/*.stories.@(ts|tsx)', './storybook/**/*.@(ts|tsx)', '*.js'],
rules: {
'no-useless-constructor': 'off',
'import/no-default-export': 'off',
/*
* eslint/import reports false positive errors after bumping to storybook 9
* https://github.com/storybookjs/storybook/issues/31863
* This is safe to disable because typescript is checking for imports too
* so we still get alerted in case of a typo or something.
*/
'import/no-unresolved': 'off',
'react-hooks/rules-of-hooks': 'off',
},
},
{
name: 'x/override-3',
files: ['src/state/*Slice.ts', 'test/**/*.spec.tsx'],
// param-reassign is allowed in slices following Redux recommendation: https://redux-toolkit.js.org/usage/immer-reducers#linting-state-mutations
rules: {
'no-param-reassign': ['error', { props: false }],
},
},
{
name: 'scripts override',
files: ['scripts/*.js', 'scripts/*.mjs', 'www/scripts/*.tsx'],
rules: {
'@typescript-eslint/no-require-imports': 'off',
'no-console': 'off',
},
},
{
name: 'x/override-5',
files: ['eslint.config.mjs', 'prettier.config.mjs', 'stryker.config.mjs'],
rules: {
'import/no-default-export': 'off',
},
},
{
name: 'website-overrides',
files: ['www/**/*'],
rules: {
/*
* There is something in React 17+ that no longer requires React to be in scope? I think.
*/
'react/react-in-jsx-scope': 'off',
/*
* We use this vite feature to import both the code, and the source code as a string.
*/
'import/no-duplicates': 'off',
/*
* This is a good recommendation, but we have so many that I will leave this as a warning for now.
*/
'import/no-default-export': 'warn',
},
},
{
name: 'default-export-override',
/*
* Files on the website, that are not index, and that are public examples
* must have only default export, because they are passed to react-runner
* which requires default export.
*/
basePath: './www/src',
files: [
'docs/exampleComponents/**/*.tsx',
'docs/exampleComponents/**/*.ts',
'docs/apiExamples/**/*.tsx',
'docs/apiExamples/**/*.ts',
'components/GuideView/*/*.tsx',
'components/GuideView/*/*.ts',
'views/IndexView/*.tsx',
'views/IndexView/*.ts',
],
ignores: ['**/index.ts', '**/index.tsx'],
rules: {
'import/no-default-export': 'off',
'import/prefer-default-export': 'error',
},
},
{
name: 'vite-config-override',
/*
* Vite config files use default export by convention.
*/
files: ['**/vite.config*', '**/vitest.config*'],
rules: {
'import/no-default-export': 'off',
},
},
{
name: 'test-helpers-override',
/*
* Test helpers sometimes use console logs for debugging purposes.
*/
files: ['test/helper/**'],
rules: {
'no-console': 'off',
},
},
];
const omnidocOverrides = [
{
name: 'omnidoc-overrides',
files: ['omnidoc/**'],
rules: {
// class methods are not required to use `this` in omnidoc scripts
'class-methods-use-this': 'off',
// omnidoc can use console logs
'no-console': 'off',
// we're okay if the docs are generated sequentially
'no-await-in-loop': 'off',
},
},
];
const settings = [
{
name: 'x/settings',
files: allFiles,
settings: {
'import/resolver': {
typescript: {},
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
},
},
},
];
const perfOptimization = {
name: 'perf-optimization-examples',
files: ['./www/src/docs/exampleComponents/LineChart/CompareTwoLines.tsx'],
plugins: { 'react-perf': reactPerf },
rules: {
// new object as prop creates a new reference on each render - but we can memo this one
// 'react-perf/jsx-no-new-object-as-prop': 'error',
'react-perf/jsx-no-new-array-as-prop': 'error',
'react-perf/jsx-no-new-function-as-prop': 'error',
'react-perf/jsx-no-jsx-as-prop': 'error',
},
};
const generatedApiDocsOverrides = {
name: 'generated-api-docs-overrides',
files: ['www/src/docs/api/**', 'storybook/stories/API/arg-types/**'],
rules: {
// These files are autogenerated so we can be less strict on the contents
'no-template-curly-in-string': 'off',
},
};
const jsdocConfig = [
{
name: 'jsdoc/config',
files: allFiles,
plugins: {
jsdoc,
},
settings: {
jsdoc: {
mode: 'typescript',
},
},
rules: {
'jsdoc/match-description': [
'error',
{
contexts: ['any'],
matchDescription: '[\\s\\S]*',
tags: {
see: {
match: '^(?!http).*',
message:
'@see must not immediately follow http. Use {@link ...} instead. If you use @see https://example.com, then omnidoc splits the https and example.com and the link will be malformed in docs.',
},
},
},
],
},
},
];
export default [
// Ignore .gitignore files/folder in eslint
includeIgnoreFile(gitignorePath),
// Custom Ignores
...customIgnores,
// Language Options
...languageOptions,
// Javascript Config
...jsConfig,
// React Config
...reactConfig,
// TypeScript Config
...typescriptConfig,
// Storybook Config
...storybookConfig,
// Prettier Config
...prettierConfig,
// ESLint Comments
...commentsConfig,
// Overrides
...overridesConfig,
// Settings
...settings,
// Omnidoc Overrides
...omnidocOverrides,
// JSDoc Config
...jsdocConfig,
generatedApiDocsOverrides,
perfOptimization,
];