-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy patheslint.config.base.js
1258 lines (1250 loc) · 56.4 KB
/
eslint.config.base.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from 'node:fs';
import path from 'node:path';
import stylisticPlugin from '@stylistic/eslint-plugin-js';
import vitestPlugin from '@vitest/eslint-plugin';
import restrictedGlobals from 'confusing-browser-globals';
import importPlugin from 'eslint-plugin-import';
import jsxA11yPlugin from 'eslint-plugin-jsx-a11y';
import promisePlugin from 'eslint-plugin-promise';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import regexpPlugin from 'eslint-plugin-regexp';
import simpleImportSortPlugin from 'eslint-plugin-simple-import-sort';
import storybookPlugin from 'eslint-plugin-storybook';
import testingLibraryPlugin from 'eslint-plugin-testing-library';
import unicornPlugin from 'eslint-plugin-unicorn';
import globals from 'globals';
import _ from 'lodash';
import tseslint from 'typescript-eslint';
const DEFAULT_OPTS = {
typescript: false,
react: false,
storybook: false,
vitest: false,
cypress: false,
reactTestingLibrary: false,
cypressTestingLibrary: false,
};
export function detectOpts(projectDir) {
const pkgPath = path.resolve(projectDir, './package.json');
const tsconfigPath = path.resolve(projectDir, './tsconfig.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath));
const deps = new Set([
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.devDependencies || {}),
]);
return {
typescript: deps.has('typescript') && fs.existsSync(tsconfigPath),
react: deps.has('react'),
storybook: deps.has('storybook'),
vitest: deps.has('vitest'),
cypress: deps.has('cypress'),
reactTestingLibrary: deps.has('@testing-library/react'),
cypressTestingLibrary: deps.has('@testing-library/cypress'),
};
}
// eslint-disable-next-line complexity
export function createConfig(opts = {}) {
const {
typescript: withTypeScript,
react: withReact,
storybook: withStorybook,
vitest: withVitest,
cypress: withCypress,
reactTestingLibrary: withReactTestingLibrary,
cypressTestingLibrary: withCypressTestingLibrary,
} = { ...DEFAULT_OPTS, ...opts };
return tseslint.config(
_.compact([
withTypeScript && {
/**
* Configure type-aware linting.
*
* To disable on a subset of files, see:
* https://typescript-eslint.io/getting-started/typed-linting#how-can-i-disable-type-aware-linting-for-a-subset-of-files
*/
name: 'h5web/defaults/languages-ts',
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
withReact && {
/**
* Enable JSX parsing.
*/
name: 'h5web/defaults/languages-jsx',
files: ['**/*.{jsx,tsx}'],
languageOptions: {
parserOptions: {
ecmaFeatures: { jsx: true },
jsxPragma: null, // https://typescript-eslint.io/packages/parser/#jsxpragma
},
},
},
{
/**
* Configure browser globals on files we're confident aren't meant to run
* in a non-browser environment.
*/
name: 'h5web/defaults/globals-browser',
files: ['**/*.{jsx,tsx}'],
languageOptions: {
globals: { ...globals.browser },
},
},
withVitest && {
/**
* Configure Vitest globals on test files.
*/
name: 'h5web/defaults/globals-vitest',
files: [
'**/__tests__/**/*.{js,jsx,ts,tsx}',
'**/*.test.{js,jsx,ts,tsx}',
],
languageOptions: {
globals: { ...vitestPlugin.environments.env.globals },
},
},
{
/**
* ESLint core rules: https://eslint.org/docs/latest/rules/
*/
name: 'h5web/defaults/rules/core',
rules: {
'accessor-pairs': 'error',
'array-callback-return': [
'error',
{ checkForEach: true }, // enforce no return in `forEach`
],
// 'arrow-body-style': 'off', // use of curly braces in arrow functions should first be dictated by readabillity
// 'block-scoped-var': 'off', // useless with `no-var`
// camelcase: 'off', // too tedious to fix when dealing with back-end responses, external libs, etc.
// 'capitalized-comments': 'off', // too nitpicky
// 'class-methods-use-this': 'off', // too nitpicky
complexity: 'warn',
'consistent-return': 'error',
// 'consistent-this': 'off', // too nitpicky
'constructor-super': 'error',
curly: [
'warn',
'all', // curly braces are good for readability and help avoid silly bugs
],
'default-case': 'warn',
'default-case-last': 'error',
'default-param-last': 'warn',
'dot-notation': 'warn',
eqeqeq: 'warn',
'for-direction': 'error',
// 'func-name-matching': 'off', // arbitrary
'func-names': [
'warn',
'as-needed', // unnecessary function names add clutter
],
'func-style': [
'warn',
'declaration', // make functions stand out from variables
],
'getter-return': 'error',
'grouped-accessor-pairs': 'warn',
'guard-for-in': 'warn',
// 'id-denylist': 'off',
// 'id-length': 'off',
// 'id-match': 'off',
// 'init-declarations': 'off',
'logical-assignment-operators': [
'warn', // i.e. `||=`, `&&=`, `??=`
'never', // if re-assigning a variable is unavoidable, at least make the logic explicit
],
'max-classes-per-file': ['warn', 1],
'max-depth': 'warn',
// 'max-lines': 'off', // too arbitrary
// 'max-lines-per-function': 'off', // too arbitrary; prefer limiting complexity
// 'max-nested-callbacks': 'off', // `max-depth` is more generic
// 'max-params': 'off', // too arbitrary
// 'max-statements': 'off', // too arbitrary
'new-cap': 'warn',
'no-alert': 'warn',
'no-array-constructor': 'error',
'no-async-promise-executor': 'error',
'no-await-in-loop': 'warn', // promises should by parallelized (disable inline for legitimate cases)
'no-bitwise': 'error',
'no-caller': 'error',
'no-case-declarations': 'warn',
'no-class-assign': 'error',
'no-compare-neg-zero': 'error',
'no-cond-assign': 'error',
'no-console': 'warn',
'no-const-assign': 'error',
'no-constant-binary-expression': 'error',
'no-constant-condition': 'error',
'no-constructor-return': 'error',
'no-continue': 'warn',
'no-control-regex': 'error',
'no-debugger': 'warn',
'no-delete-var': 'error',
'no-div-regex': 'error',
'no-dupe-args': 'error',
'no-dupe-class-members': 'error',
'no-dupe-else-if': 'error',
'no-dupe-keys': 'error',
'no-duplicate-case': 'error',
// 'no-duplicate-imports': 'off', // replaced with `imports/no-duplicates`
'no-else-return': [
'warn',
{ allowElseIf: false }, // `else if` is unnecessary when all previous branch returns
],
'no-empty': 'warn',
'no-empty-character-class': 'error',
'no-empty-function': [
'warn', // insert "noop" comment (or equivalent) in function body if intentional
{ allow: ['arrowFunctions'] }, // often used as default values for callback parameters
],
'no-empty-pattern': 'warn',
'no-empty-static-block': 'warn',
// 'no-eq-null': 'off', // useless with `eqeqeq`
'no-eval': 'error',
'no-ex-assign': 'error',
'no-extend-native': 'error',
'no-extra-bind': 'warn',
'no-extra-boolean-cast': [
'warn',
{ enforceForInnerExpressions: true },
],
// 'no-extra-label': 'off', // useless with `no-labels`
'no-fallthrough': 'error',
'no-func-assign': 'error',
'no-global-assign': 'error',
'no-implicit-coercion': [
'warn',
{ allow: ['!!'] }, // allow only coercing to boolean with `!!`
],
'no-implicit-globals': 'error',
'no-implied-eval': 'error',
'no-import-assign': 'error',
// 'no-inline-comments': 'off', // inline comments are very handy...
'no-inner-declarations': [
'warn',
'functions',
{ blockScopedFunctions: 'disallow' }, // stricter stylistic choice (disable inline for legitimate cases)
],
'no-invalid-regexp': 'error',
'no-invalid-this': 'error',
'no-irregular-whitespace': 'warn',
'no-iterator': 'error',
// 'no-label-var': 'off', // useless with `no-labels`
'no-labels': 'error',
'no-lone-blocks': 'error',
// 'no-lonely-if': 'off',
'no-loop-func': 'error',
'no-loss-of-precision': 'error',
// 'no-magic-numbers': 'off', // impractical
'no-misleading-character-class': 'error',
'no-multi-assign': 'warn',
'no-multi-str': 'error',
// 'no-negated-condition': 'off', // sometimes more readable
// 'no-nested-ternary': 'off', // sometimes convenient
'no-new-func': 'error',
'no-new-native-nonconstructor': 'error',
'no-new-wrappers': 'error',
'no-new': 'warn',
'no-nonoctal-decimal-escape': 'error',
'no-obj-calls': 'error',
'no-object-constructor': 'error',
'no-octal-escape': 'error',
'no-octal': 'error',
'no-param-reassign': [
'warn',
{ props: true }, // disallow mutating function parameters
],
// 'no-plusplus': 'off', // risk obsolete with Prettier configured with `semi: true` (default)
'no-promise-executor-return': 'error',
'no-proto': 'error',
'no-prototype-builtins': 'error',
// 'no-redeclare': 'off', // useless with `no-var`
'no-regex-spaces': 'error',
// 'no-restricted-exports': 'off',
'no-restricted-globals': [
'error', // access globals explicitly via `window`, `self`, etc.
...restrictedGlobals, // https://github.com/facebook/create-react-app/blob/main/packages/confusing-browser-globals/index.js
],
// 'no-restricted-imports': 'off',
// 'no-restricted-properties': 'off',
// 'no-restricted-syntax': 'off',
'no-return-assign': 'error',
'no-script-url': 'error',
'no-self-assign': 'error',
'no-self-compare': 'error',
'no-sequences': 'error',
'no-setter-return': 'error',
'no-shadow-restricted-names': 'error',
'no-shadow': 'warn',
'no-sparse-arrays': 'error',
'no-template-curly-in-string': 'warn',
// 'no-ternary': 'off', // hmm, no thank you
'no-this-before-super': 'error',
'no-throw-literal': 'error',
'no-undef-init': 'warn',
'no-undef': 'error',
// 'no-undefined': 'off', // already enforced by `no-shadow-restricted-names`
// 'no-underscore-dangle': 'off', // not worth arguing about
'no-unexpected-multiline': 'warn',
'no-unmodified-loop-condition': 'error',
'no-unneeded-ternary': 'warn',
'no-unreachable-loop': 'warn',
'no-unreachable': 'error',
'no-unsafe-finally': 'error',
'no-unsafe-negation': 'error',
'no-unsafe-optional-chaining': [
'error',
{ disallowArithmeticOperators: true }, // prevent implicit type coercion of `undefined`
],
'no-unused-expressions': 'warn',
// 'no-unused-labels': 'off', // useless with `no-labels`
'no-unused-private-class-members': 'warn',
'no-unused-vars': [
'warn',
{
args: 'all', // report all unused arguments
argsIgnorePattern: '^_', // unless they are specifically prefixed with an underscore
ignoreRestSiblings: true, // allow using destructuring to remove properties from objects
},
],
// 'no-use-before-define': 'off',
// 'no-useless-assignment': 'off', // false positives in JSX (and already covered by TypeScript)
'no-useless-backreference': 'error',
'no-useless-call': 'error',
'no-useless-catch': 'warn',
'no-useless-computed-key': 'warn',
'no-useless-concat': 'warn',
'no-useless-constructor': 'warn',
'no-useless-escape': 'warn',
'no-useless-rename': 'warn',
'no-useless-return': 'warn',
'no-var': 'error',
'no-void': [
'warn',
{ allowAsStatement: true }, // useful to identify promises that don't need to be chained/awaited
],
// 'no-warning-comments': 'off',
'no-with': 'error',
'object-shorthand': 'warn', // `{ a }` rather than `{ a: a }`
'one-var': ['warn', 'never'], // disallow combining variable declarations - e.g. `const a = 1, b = 2;`
'operator-assignment': ['warn', 'always'], // `x += y` rather than `x = x + y`
'prefer-arrow-callback': 'warn',
'prefer-const': 'warn',
'prefer-destructuring': 'warn',
'prefer-exponentiation-operator': 'warn',
// 'prefer-named-capture-group': 'off', // lacks browser support
'prefer-numeric-literals': 'warn',
'prefer-object-has-own': 'warn',
'prefer-object-spread': 'warn',
'prefer-promise-reject-errors': 'error',
'prefer-regex-literals': 'error',
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'warn',
'require-atomic-updates': 'warn',
// 'require-await': 'off', // making a synchronous function `async` intentionally is perfectly fine
// 'require-unicode-regexp': 'off', // replaced with `regexp/require-unicode-regexp`
'require-yield': 'error',
// 'sort-imports': 'off', // prefer `simple-import-sort/imports`
// 'sort-keys': 'off',
// 'sort-vars': 'off',
'symbol-description': 'warn',
// 'unicode-bom': 'off', // handled by Prettier
// 'use-isnan': 'off', // replaced with `unicorn/prefer-number-properties`
'valid-typeof': 'error',
// 'vars-on-top': 'off', // useless with `no-var`
// radix: 'off', // obsolete since ES5
strict: 'error',
yoda: 'warn',
},
},
withTypeScript && {
/**
* ESLint core rules on TS/TSX files.
* Turn off rules made obsolete by TypeScript or with better alternatives in TypeScript plugin.
*/
name: 'h5web/defaults/rules-ts/core',
files: ['**/*.{ts,tsx}'],
rules: {
'default-case': 'off',
'default-param-last': 'off', // replaced with `@typescript-eslint/default-param-last`
'no-loop-func': 'off', // replaced with `@typescript-eslint/no-loop-func`
'no-shadow': 'off', // replaced with `@typescript-eslint/no-shadow`
'prefer-destructuring': 'off', // replaced with `@typescript-eslint/prefer-destructuring`
},
},
withReact && {
/**
* ESLint core rules on JSX/TSX files.
* Configure specific rules to support/check JSX syntax.
*/
name: 'h5web/defaults/rules-jsx/core',
files: ['**/*.{jsx,tsx}'],
rules: {
'no-unused-expressions': ['warn', { enforceForJSX: true }],
},
},
{
/**
* Promise plugin: https://github.com/eslint-community/eslint-plugin-promise
* Promote use of `async/await` but enforce promise chaining rules as
* fallback in case of local opt-out.
*/
name: 'h5web/defaults/rules/promise',
plugins: { promise: promisePlugin },
rules: {
'promise/always-return': 'error',
// 'promise/avoid-new': 'off', // allow `new Promise()` syntax
'promise/catch-or-return': 'error',
'promise/no-callback-in-promise': 'warn',
'promise/no-multiple-resolved': 'warn',
// 'promise/no-native': 'off', // obsolete
'promise/no-nesting': 'warn',
'promise/no-new-statics': 'error',
'promise/no-promise-in-callback': 'warn',
'promise/no-return-in-finally': 'warn',
'promise/no-return-wrap': 'error',
'promise/param-names': 'warn',
'promise/prefer-await-to-callbacks': 'warn',
'promise/prefer-await-to-then': [
'error', // `async/await` is a must!
{ strict: true }, // disallow `await promise.then()`
],
'promise/prefer-catch': 'warn',
'promise/spec-only': 'error',
'promise/valid-params': 'error',
},
},
withTypeScript && {
/**
* Promise plugin on TS/TSX files.
* Turn off rules that TypeScript can report.
*/
name: 'h5web/defaults/rules-ts/promise',
files: ['**/*.{ts,tsx}'],
rules: {
'promise/valid-params': 'off',
},
},
{
/**
* Regexp plugin: https://ota-meshi.github.io/eslint-plugin-regexp/rules/
* Use recommended configuration, and then tweak.
*/
name: 'h5web/defaults/rules/regexp',
plugins: { regexp: regexpPlugin },
rules: {
...regexpPlugin.configs['flat/recommended'].rules,
'regexp/grapheme-string-literal': 'warn',
'regexp/hexadecimal-escape': 'warn',
'regexp/letter-case': 'warn',
'regexp/no-control-character': 'error', // complements `no-control-regex`
'regexp/no-octal': 'error',
'regexp/no-standalone-backslash': 'error',
'regexp/no-super-linear-move': 'error', // protects from regex DoS
'regexp/prefer-escape-replacement-dollar-char': 'warn',
'regexp/require-unicode-regexp': 'warn',
'regexp/sort-alternatives': 'warn',
'regexp/sort-character-class-elements': 'warn',
'regexp/sort-flags': 'warn', // downgrade
},
},
{
/**
* Regexp plugin on test/Cypress files
*/
name: 'h5web/defaults/rules-test/regexp',
files: [
'**/__tests__/**/*.{js,jsx,ts,tsx}',
'**/*.test.{js,jsx,ts,tsx}',
'cypress/**/*.ts',
],
rules: {
'regexp/require-unicode-regexp': 'off', // futile in tests
},
},
{
/**
* Stylistic plugin: https://eslint.style/
* Minor formatting tweaks to complement Prettier.
*/
name: 'h5web/defaults/rules/stylistic-js',
plugins: { '@stylistic/js': stylisticPlugin },
rules: {
'@stylistic/js/spaced-comment': [
'warn',
'always', // force space after `//` and `/*`, and before `*/`
],
},
},
{
/**
* Stylistic plugin on TS declaration files.
*/
name: 'h5web/defaults/rules-dts/stylistic-js',
files: ['**/*.d.ts'],
rules: {
'@stylistic/js/spaced-comment': [
'warn',
'always',
{ line: { markers: ['/'] } }, // allow triple slash directives
],
},
},
{
/**
* Import plugin: https://github.com/import-js/eslint-plugin-import
* - Assume ESM (`"type": "module"` in `package.json`)
* - Disable slow rules: https://typescript-eslint.io/troubleshooting/typed-linting/performance/#eslint-plugin-import
*/
name: 'h5web/defaults/rules/import',
plugins: { import: importPlugin },
rules: {
'import/consistent-type-specifier-style': [
'warn',
'prefer-top-level', // TODO switch to `prefer-inline`: `import { type Foo } from 'foo';`
],
'import/default': 'error',
// 'import/dynamic-import-chunkname': 'off', // bundler-specific
'import/export': 'error',
// 'import/exports-last': 'off', // can move related pieces of code far apart from one another
// 'import/extensions': 'off', // project-specific
'import/first': 'warn', // move all imports to the top
// 'import/group-exports': 'off', // can move related pieces of code far apart from one another
// 'import/max-dependencies': 'off', // arbitrary
// 'import/named': 'off', // too slow even when applied only to a few JS files
// 'import/namespace': 'off', // too slow even when applied only to a few JS files
'import/newline-after-import': 'warn',
// 'import/no-absolute-path': 'off', // project-specific
'import/no-amd': 'error',
'import/no-anonymous-default-export': 'warn', // good for IntelliSense, `console.log` debugging, etc.
'import/no-commonjs': 'error', // only allowed in CJS files
// 'import/no-cycle': 'off', // way too slow!
// 'import/no-default-export': 'off', // common practice in React projects
// 'import/no-deprecated': 'off', // too slow even when applied only to a few JS files; cf. also `@typescript-eslint/no-deprecated`
'import/no-duplicates': [
'warn',
{
considerQueryString: true, // e.g. `?worker`
'prefer-inline': false, // TODO switch to `true` for compatibility with TypeScript inline `type` imports
},
],
// 'import/no-dynamic-require': 'off', // CommonJS
// 'import/no-empty-named-blocks': 'off',
// 'import/no-extraneous-dependencies': 'off',
// 'import/no-import-module-exports': 'off', // CommonJS
// 'import/no-internal-modules': 'off', // project-specific
'import/no-mutable-exports': 'error',
'import/no-named-as-default': 'error',
'import/no-named-default': 'warn',
'import/no-named-as-default-member': 'error',
// 'import/no-named-export': 'off',
'import/no-namespace': 'warn', // namespaces make it difficult to find unused code, rename exports, etc.
// 'import/no-nodejs-modules': 'off', // project-specific
// 'import/no-relative-packages': 'off', // project-specific
// 'import/no-relative-parent-imports': 'off', // project-specific
// 'import/no-restricted-paths': 'off', // project-specific
'import/no-self-import': 'error',
// 'import/no-unassigned-import': 'off', // project-specific
// 'import/no-unresolved': 'off', // slow, false positives
'import/no-unused-modules': 'warn',
'import/no-useless-path-segments': 'warn',
// 'import/no-webpack-loader-syntax': 'off', // bundler-specific
// 'import/order': 'off', // prefer `simple-import-sort/imports`
// 'import/prefer-default-export': 'off', // project-specific
// 'import/unambiguous': 'off',
},
},
{
/**
* Import plugin on CJS files.
* Expect CommonJS module system in `.cjs` files.
*/
name: 'h5web/defaults/rules-cjs/import',
files: ['**/*.cjs'],
rules: {
'import/no-commonjs': 'off',
'import/no-cycle': 'off', // not supported
'import/no-dynamic-require': 'error', // allow explicitly when needed
'import/no-import-module-exports': 'error',
'import/no-unresolved': ['error', { commonjs: true }],
'import/no-useless-path-segments': ['warn', { commonjs: true }],
},
},
withTypeScript && {
/**
* Import plugin on TS/TSX files.
* Turn off rules that TypeScript can report.
*/
name: 'h5web/defaults/rules-ts/import',
files: ['**/*.{ts,tsx}'],
rules: {
'import/default': 'off',
'import/no-named-as-default-member': 'off',
'import/no-unresolved': 'off',
},
},
{
/**
* Simple Import Sort plugin: https://github.com/lydell/eslint-plugin-simple-import-sort
* Prefer this plugin over ESLint's `sort-imports` to avoid debating exact
* sorting order (i.e. Prettier philosophy).
*/
name: 'h5web/defaults/rules/import-sort',
plugins: { 'simple-import-sort': simpleImportSortPlugin },
rules: {
'simple-import-sort/imports': 'warn', // crucial for consistency and to reduce noise in diffs
// 'simple-import-sort/exports': 'off', // exports order often matters for readability/documentation
},
},
{
/**
* Unicorn plugin: https://github.com/sindresorhus/eslint-plugin-unicorn
*/
name: 'h5web/defaults/rules/unicorn',
languageOptions: { globals: globals.builtin },
plugins: { unicorn: unicornPlugin },
rules: {
'unicorn/better-regex': 'warn',
'unicorn/catch-error-name': 'warn',
// 'unicorn/consistent-destructuring': 'off', // false positives after type narrowing - e.g. `isDataset(entity) && entity.chunks`
'unicorn/consistent-empty-array-spread': 'warn',
'unicorn/consistent-existence-index-check': 'warn',
'unicorn/consistent-function-scoping': 'warn',
'unicorn/custom-error-definition': 'warn',
// 'unicorn/empty-brace-spaces': 'off', // handled by Prettier
'unicorn/error-message': 'warn',
'unicorn/escape-case': 'warn',
// 'unicorn/expiring-todo-comments': 'off', // project-specific
'unicorn/explicit-length-check': 'warn',
// 'unicorn/filename-case': 'off', // would need more fine-grained control
// 'unicorn/import-style': 'off', // project-specific
'unicorn/new-for-builtins': 'warn',
'unicorn/no-abusive-eslint-disable': 'warn',
'unicorn/no-anonymous-default-export': 'warn',
// 'unicorn/no-array-callback-reference': 'off', // too convenient to pass up on
// 'unicorn/no-array-for-each': 'off', // `forEach` is sometimes more concise
'unicorn/no-array-method-this-argument': 'error',
'unicorn/no-array-push-push': 'warn',
// 'unicorn/no-array-reduce': 'off', // too opinionated
'unicorn/no-await-expression-member': 'warn',
'unicorn/no-await-in-promise-methods': 'error',
'unicorn/no-console-spaces': 'warn',
'unicorn/no-document-cookie': 'warn',
'unicorn/no-empty-file': 'warn',
'unicorn/no-for-loop': 'warn',
'unicorn/no-hex-escape': 'warn',
'unicorn/no-instanceof-array': 'warn',
'unicorn/no-invalid-fetch-options': 'error',
'unicorn/no-invalid-remove-event-listener': 'error',
// 'unicorn/no-keyword-prefix': 'off', // keywords stand out with syntax highlighting
'unicorn/no-length-as-slice-end': 'warn',
'unicorn/no-lonely-if': 'warn',
// 'unicorn/no-magic-array-flat-depth': 'off',
// 'unicorn/no-negated-condition': 'off', // sometimes more readable
'unicorn/no-negation-in-equality-check': 'warn',
// 'unicorn/no-nested-ternary': 'off', // sometimes convenient; Prettier provides decent formatting
'unicorn/no-new-array': 'warn',
'unicorn/no-new-buffer': 'warn',
// 'unicorn/no-null': 'off', // `null` declares "active absence"
'unicorn/no-object-as-default-parameter': 'warn',
'unicorn/no-process-exit': 'warn',
'unicorn/no-single-promise-in-promise-methods': 'warn',
'unicorn/no-static-only-class': 'warn',
'unicorn/no-thenable': 'warn',
'unicorn/no-this-assignment': 'warn',
'unicorn/no-typeof-undefined': 'warn',
'unicorn/no-unnecessary-await': 'warn',
// 'unicorn/no-unnecessary-polyfills': 'off', // project-specific
'unicorn/no-unreadable-array-destructuring': 'warn',
'unicorn/no-unreadable-iife': 'warn',
'unicorn/no-unused-properties': 'warn',
'unicorn/no-useless-fallback-in-spread': 'warn',
'unicorn/no-useless-length-check': 'warn',
'unicorn/no-useless-promise-resolve-reject': 'warn',
'unicorn/no-useless-spread': 'warn',
'unicorn/no-useless-switch-case': 'warn',
// 'unicorn/no-useless-undefined': 'off', // prefer explicitness
'unicorn/no-zero-fractions': 'warn',
// 'unicorn/number-literal-case': 'off', // handled by Prettier
'unicorn/numeric-separators-style': 'warn',
'unicorn/prefer-add-event-listener': 'warn',
'unicorn/prefer-array-find': 'warn',
'unicorn/prefer-array-flat-map': 'warn',
'unicorn/prefer-array-flat': 'warn',
'unicorn/prefer-array-index-of': 'warn',
'unicorn/prefer-array-some': 'warn',
// 'unicorn/prefer-at': 'off', // poor browser support
'unicorn/prefer-blob-reading-methods': 'warn',
'unicorn/prefer-code-point': 'warn',
'unicorn/prefer-date-now': 'warn',
'unicorn/prefer-default-parameters': 'warn',
'unicorn/prefer-dom-node-append': 'warn',
'unicorn/prefer-dom-node-dataset': 'warn',
'unicorn/prefer-dom-node-remove': 'warn',
'unicorn/prefer-dom-node-text-content': 'warn',
'unicorn/prefer-event-target': 'warn',
'unicorn/prefer-export-from': 'warn',
'unicorn/prefer-global-this': 'warn',
'unicorn/prefer-includes': 'warn',
'unicorn/prefer-json-parse-buffer': 'warn',
'unicorn/prefer-keyboard-event-key': 'warn',
'unicorn/prefer-logical-operator-over-ternary': 'warn',
'unicorn/prefer-math-min-max': 'warn',
'unicorn/prefer-math-trunc': 'warn',
'unicorn/prefer-modern-dom-apis': 'warn',
'unicorn/prefer-modern-math-apis': 'warn',
'unicorn/prefer-module': 'warn',
'unicorn/prefer-native-coercion-functions': 'warn',
'unicorn/prefer-negative-index': 'warn',
'unicorn/prefer-node-protocol': 'warn',
'unicorn/prefer-number-properties': [
'warn',
{ checkInfinity: false }, // `Number.POSITIVE_INFINITY` is too verbose
],
'unicorn/prefer-object-from-entries': 'warn',
'unicorn/prefer-optional-catch-binding': 'warn',
'unicorn/prefer-prototype-methods': 'warn',
// 'unicorn/prefer-query-selector': 'off', // `getElementById` is simpler
'unicorn/prefer-reflect-apply': 'warn',
'unicorn/prefer-regexp-test': 'warn',
'unicorn/prefer-set-has': 'warn',
'unicorn/prefer-set-size': 'warn',
'unicorn/prefer-spread': 'warn',
'unicorn/prefer-string-raw': 'warn',
'unicorn/prefer-string-replace-all': 'warn',
'unicorn/prefer-string-slice': 'warn',
// 'unicorn/prefer-string-starts-ends-with': 'off', // replaced with `@typescript-eslint/prefer-string-starts-ends-with`
'unicorn/prefer-string-trim-start-end': 'warn',
// 'unicorn/prefer-structured-clone': 'off', // poor browser support
'unicorn/prefer-switch': 'warn',
// 'unicorn/prefer-ternary': 'off', // there's such a thing as ternary overuse...
// 'unicorn/prefer-top-level-await': 'off', // difficult to detect if supported
'unicorn/prefer-type-error': 'warn',
// 'unicorn/prevent-abbreviations': 'off', // way too opinionated
'unicorn/relative-url-style': 'warn',
'unicorn/require-array-join-separator': 'warn',
'unicorn/require-number-to-fixed-digits-argument': 'warn',
'unicorn/require-post-message-target-origin': 'warn',
// 'unicorn/string-content': 'off', // project-specific
'unicorn/switch-case-braces': ['warn', 'avoid'], // `switch` should be less verbose than if/else if/else
'unicorn/template-indent': 'warn',
// 'unicorn/text-encoding-identifier-case': 'off', // false positives
'unicorn/throw-new-error': 'warn',
},
},
withTypeScript && {
/**
* Unicorn plugin on TS/TSX files.
* Turn off rules made obsolete by TypeScript.
*/
name: 'h5web/defaults/rules-ts/unicorn',
files: ['**/*.{ts,tsx}'],
rules: {
'unicorn/no-unnecessary-await': 'off',
},
},
withCypress && {
/**
* Unicorn plugin on Cypress files.
*/
name: 'h5web/defaults/rules-cypress/unicorn',
files: ['cypress/**/*.ts'],
rules: {
'unicorn/numeric-separators-style': 'off', // not supported
},
},
withTypeScript && {
/**
* TypeScript plugin: https://typescript-eslint.io/rules/
* Use both `strictTypeChecked` and stylisticTypeChecked` configs, and then tweak.
*/
name: 'h5web/defaults/rules-ts/typescript',
extends: [
tseslint.configs.strictTypeChecked,
tseslint.configs.stylisticTypeChecked,
],
rules: {
'@typescript-eslint/ban-ts-comment': [
'error',
{
'ts-expect-error': 'allow-with-description', // allow explicitly disabling errors
'ts-ignore': true,
'ts-nocheck': true,
},
],
'@typescript-eslint/consistent-indexed-object-style': 'warn', // downgrade
'@typescript-eslint/consistent-type-assertions': [
'error',
{
assertionStyle: 'as',
objectLiteralTypeAssertions: 'allow-as-parameter', // prefer `const x: T = { ... };` to `const x = { ... } as T; `
},
],
'@typescript-eslint/consistent-type-exports': 'warn',
'@typescript-eslint/consistent-type-imports': [
// Prefer this rule to TypeScript's `verbatimModuleSyntax` option
// https://typescript-eslint.io/rules/consistent-type-imports/#comparison-with-importsnotusedasvalues--verbatimmodulesyntax
'warn',
{
disallowTypeAnnotations: true,
fixStyle: 'inline-type-imports', // `import { type Foo } from 'foo';`
prefer: 'type-imports',
},
],
'@typescript-eslint/default-param-last': 'error',
'@typescript-eslint/explicit-member-accessibility': 'warn',
'@typescript-eslint/explicit-module-boundary-types': 'warn',
'@typescript-eslint/member-ordering': 'warn',
'@typescript-eslint/method-signature-style': 'warn',
'@typescript-eslint/no-confusing-void-expression': [
'warn',
{ ignoreArrowShorthand: true },
],
'@typescript-eslint/no-empty-function': [
'warn', // insert "noop" comment (or equivalent) in function body if intentional
{ allow: ['arrowFunctions'] }, // often used as default values for callback parameters
],
'@typescript-eslint/no-empty-object-type': [
'error',
{ allowInterfaces: 'always' }, // empty interfaces don't have the same downside as the empty object type `{}`
],
'@typescript-eslint/no-loop-func': 'error',
'@typescript-eslint/no-shadow': 'warn',
'@typescript-eslint/no-unnecessary-boolean-literal-compare': [
'warn',
{
allowComparingNullableBooleansToFalse: false,
allowComparingNullableBooleansToTrue: false,
},
],
'@typescript-eslint/no-unnecessary-condition': 'off', // false positives
'@typescript-eslint/no-unnecessary-parameter-property-assignment':
'warn',
'@typescript-eslint/no-unnecessary-qualifier': 'warn',
'@typescript-eslint/no-unsafe-argument': 'warn',
'@typescript-eslint/no-unsafe-assignment': 'off', // too tricky to fix when `any` is declared externally (JSON.parse, Array.isArray, etc.)
'@typescript-eslint/no-unused-vars': [
'warn', // downgrade
{
args: 'all', // report all unused arguments
argsIgnorePattern: '^_', // unless they are specifically prefixed with an underscore
ignoreRestSiblings: true, // allow using destructuring to remove properties from objects
},
],
'@typescript-eslint/no-useless-empty-export': 'error',
'@typescript-eslint/parameter-properties': [
'warn',
{ prefer: 'parameter-property' }, // e.g. `constructor(public readonly name: string) {}`
],
'@typescript-eslint/prefer-destructuring': 'warn',
'@typescript-eslint/prefer-enum-initializers': 'error',
'@typescript-eslint/prefer-nullish-coalescing': 'off', // `??` not supported before FF 72
'@typescript-eslint/prefer-readonly': 'error',
'@typescript-eslint/prefer-regexp-exec': 'off', // handled by `regexp/prefer-regexp-exec`
'@typescript-eslint/promise-function-async': 'error',
'@typescript-eslint/require-array-sort-compare': 'error',
'@typescript-eslint/require-await': 'off', // making a synchronous function `async` intentionally is perfectly fine
'@typescript-eslint/restrict-template-expressions': [
'error',
{ allowNumber: true },
],
},
},
withTypeScript && {
/**
* TypeScript plugin on JS/JSX/CJS files
* Disable some TS rules that could cause issues in JS files.
*/
name: 'h5web/defaults/rules-js/typescript',
files: ['**/*.{js,jsx,cjs}'],
rules: {
'@typescript-eslint/explicit-member-accessibility': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unsafe-argument': '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',
},
},
withTypeScript && {
/**
* TypeScript plugin on JSX/TSX files.
*/
name: 'h5web/defaults/rules-jsx/typescript',
files: ['**/*.{jsx,tsx}'],
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'off', // affects readability of React components
},
},
withReact && {
/**
* React plugin: https://github.com/jsx-eslint/eslint-plugin-react
*/
name: 'h5web/defaults/rules-jsx/react',
plugins: { react: reactPlugin },
settings: { react: { version: 'detect' } },
rules: {
// 'react/boolean-prop-naming': 'off',
'react/button-has-type': 'warn',
'react/checked-requires-onchange-or-readonly': 'warn',
'react/default-props-match-prop-types': 'error',
'react/destructuring-assignment': 'warn',
'react/display-name': 'warn',
// 'react/forbid-component-props': 'off', // project-specific
// 'react/forbid-dom-props': 'off', // project-specific
// 'react/forbid-elements': 'off', // project-specific
// 'react/forbid-foreign-prop-types': 'off', // obsolete
// 'react/forbid-prop-types': 'error', // project-specific
'react/forward-ref-uses-ref': 'error',
'react/function-component-definition': [
'warn',
{
namedComponents: 'function-declaration', // `function Foo() { return <div />; }`
unnamedComponents: 'arrow-function', // `() => { return <div />; }`
},
],
// 'react/hook-use-state': 'off', // too restrictive
'react/iframe-missing-sandbox': 'error',
'react/jsx-boolean-value': 'warn',
// 'react/jsx-child-element-spacing': 'off', // Prettier
// 'react/jsx-closing-bracket-location': 'off', // Prettier
// 'react/jsx-closing-tag-location': 'off', // Prettier
'react/jsx-curly-brace-presence': ['warn', 'never'], // remove curly braces when not needed
// 'react/jsx-curly-newline': 'off', // Prettier
// 'react/jsx-curly-spacing': 'off', // Prettier
// 'react/jsx-equals-spacing': 'off', // Prettier
'react/jsx-filename-extension': [
'warn',
{
allow: 'always', // allow components without JSX to have JSX/TSX extension
extensions: ['.jsx', '.tsx'],
ignoreFilesWithoutCode: true,
},
],
// 'react/jsx-first-prop-new-line': 'off', // Prettier
'react/jsx-fragments': ['warn', 'syntax'], // prefer `<></>` over `<Fragment></Fragment>` (except with `key`)
// 'react/jsx-handler-names': 'off', // too restrictive
// 'react/jsx-indent': 'off', // Prettier
// 'react/jsx-indent-props': 'off', // Prettier
'react/jsx-key': [
'error',
{
checkFragmentShorthand: true,
checkKeyMustBeforeSpread: true,
warnOnDuplicates: true,
},
],
// 'react/jsx-max-depth': 'off', // arbitrary
// 'react/jsx-max-props-per-line': 'off', // Prettier
// 'react/jsx-newline': 'off', // Prettier
// 'react/jsx-no-bind': 'off', // premature optimisation
'react/jsx-no-comment-textnodes': 'error',
// 'react/jsx-no-constructed-context-values': 'off', // premature optimisation
'react/jsx-no-duplicate-props': 'error',
'react/jsx-no-leaked-render': 'error', // disabled later on TS files
// 'react/jsx-no-literals': 'off', // absurd
'react/jsx-no-script-url': 'error',
// 'react/jsx-no-target-blank': 'off', // obsolete with modern browsers
'react/jsx-no-undef': 'error',
'react/jsx-no-useless-fragment': 'warn',
// 'react/jsx-one-expression-per-line': 'off', // Prettier
'react/jsx-pascal-case': 'error',
// 'react/jsx-props-no-multi-spaces': 'off', // Prettier
'react/jsx-props-no-spread-multi': 'error',
// 'react/jsx-props-no-spreading': 'off', // too restrictive
// 'react/jsx-sort-default-props': 'off',
// 'react/jsx-sort-props': 'off',
// 'react/jsx-space-before-closing': 'off', // Prettier
// 'react/jsx-tag-spacing': 'off', // Prettier
// 'react/jsx-uses-react': 'off', // importing React in every file shouldn't be required with modern bundlers
'react/jsx-uses-vars': 'warn', // for `no-unused-vars` and `@typescript-eslint/no-unused-vars` to not report variables used in JSX/TSX
// 'react/jsx-wrap-multilines': 'off', // Prettier
'react/no-access-state-in-setstate': 'error',
// 'react/no-adjacent-inline-elements': 'off', // arbitrary
'react/no-array-index-key': 'warn', // disable inline as needed
'react/no-arrow-function-lifecycle': 'error',
'react/no-children-prop': 'error',
// 'react/no-danger': 'off', // React's API gives sufficient warning
'react/no-danger-with-children': 'error',
'react/no-deprecated': 'error',
'react/no-did-mount-set-state': 'warn',
'react/no-did-update-set-state': 'error',
'react/no-direct-mutation-state': 'error',
'react/no-find-dom-node': 'error',
'react/no-invalid-html-attribute': 'error', // check value of `rel` attribute is allowed
'react/no-is-mounted': 'error',
'react/no-multi-comp': 'warn',
'react/no-namespace': 'error',
// 'react/no-object-type-as-default-prop': 'off', // premature optimisation
'react/no-redundant-should-component-update': 'error',
'react/no-render-return-value': 'error',
// 'react/no-set-state': 'off', // absurd
'react/no-string-refs': 'error',
'react/no-this-in-sfc': 'error',
'react/no-typos': 'error',
// 'react/no-unescaped-entities': 'off', // Prettier probably sufficient to spot these typos