-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy patheslint.config.mjs
More file actions
345 lines (308 loc) · 10.8 KB
/
Copy patheslint.config.mjs
File metadata and controls
345 lines (308 loc) · 10.8 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
import path from 'node:path';
import url from 'node:url';
import eslint from '@eslint/js';
import tsParser from '@typescript-eslint/parser';
import eslintPluginVitest from '@vitest/eslint-plugin';
import eslintConfig from 'eslint/config';
import eslintPluginJsdoc from 'eslint-plugin-jsdoc';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import eslintPluginSimpleImportSort from 'eslint-plugin-simple-import-sort';
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import noArrayPushSpread from './.eslint/rules/performance/noArrayPushSpread.mjs';
import noArrayRebuildInLoop from './.eslint/rules/performance/noArrayRebuildInLoop.mjs';
import noArrayShiftMutationInLoop from './.eslint/rules/performance/noArrayShiftMutationInLoop.mjs';
import noNumberToLocaleString from './.eslint/rules/performance/noNumberToLocaleString.mjs';
import noFsPromisify from './.eslint/rules/style/noFsPromisify.mjs';
import noNodeSubpathImports from './.eslint/rules/style/noNodeSubpathImports.mjs';
import preferNodeDefaultImport from './.eslint/rules/style/preferNodeDefaultImport.mjs';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default eslintConfig.defineConfig([
{
ignores: ['.*/**', 'dist/**', 'packages/*/deps/**', 'site/**'],
},
// @typescript-eslint
eslint.configs.recommended,
{
files: ['**/*.{js,ts}'],
extends: [tseslint.configs.strictTypeChecked, tseslint.configs.stylisticTypeChecked],
},
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: __dirname,
},
},
rules: {
// Conflicts between @typescript-eslint plugins 😡
'@typescript-eslint/non-nullable-type-assertion-style': 'off',
// Personal preferences
'@typescript-eslint/no-misused-promises': [
'error',
{
// Allow passing async functions to places that don't explicitly accept them, such as
// setTimeout()
checksVoidReturn: false,
},
],
'@typescript-eslint/restrict-template-expressions': [
'error',
{
allow: ['unknown'],
allowNever: true,
},
],
'@typescript-eslint/unbound-method': 'error',
},
},
{
ignores: ['**/*.ts'],
...tseslint.configs.disableTypeChecked,
},
// Custom plugins
{
plugins: {
local: {
rules: {
'no-fs-promisify': noFsPromisify,
'no-node-subpath-imports': noNodeSubpathImports,
'prefer-node-default-import': preferNodeDefaultImport,
'no-array-push-spread': noArrayPushSpread,
'no-array-rebuild-in-loop': noArrayRebuildInLoop,
'no-array-shift-mutation-in-loop': noArrayShiftMutationInLoop,
'no-number-to-locale-string': noNumberToLocaleString,
},
},
},
rules: {
'local/no-fs-promisify': 'error',
'local/no-node-subpath-imports': 'error',
'local/prefer-node-default-import': 'error',
'local/no-array-push-spread': 'error',
'local/no-array-rebuild-in-loop': 'error',
'local/no-array-shift-mutation-in-loop': 'error',
'local/no-number-to-locale-string': 'error',
},
},
// Third party configs
eslintPluginUnicorn.configs.recommended,
eslintPluginJsdoc.configs['flat/recommended-typescript-error'],
{
plugins: { vitest: eslintPluginVitest },
rules: {
...eslintPluginVitest.configs.recommended.rules,
'vitest/expect-expect': 'off',
'vitest/no-unneeded-async-expect-function': 'off', // for Jest compatability for now
},
settings: {
vitest: {
typecheck: true,
},
},
},
{
plugins: {
'simple-import-sort': eslintPluginSimpleImportSort,
},
rules: {
'simple-import-sort/exports': 'error',
'simple-import-sort/imports': 'error',
},
},
eslintPluginPrettierRecommended, // MUST BE THE LAST PLUGIN!
// Language options
{
files: ['**/*.ts'],
languageOptions: {
parser: tsParser,
parserOptions: {
projectService: true,
tsconfigRootDir: __dirname,
},
sourceType: 'module',
globals: {
...globals.builtin,
...eslintPluginVitest.environments.env.globals,
},
},
},
{
files: ['**/*.mjs'],
languageOptions: {
globals: {
...globals.node,
},
},
},
// Rule overrides
{
// Rules for all JavaScript files
rules: {
// ***** Files *****
'unicorn/filename-case': [
'error',
{
case: 'camelCase',
checkDirectories: false,
},
],
// ***** Imports *****
'simple-import-sort/exports': 'error',
'simple-import-sort/imports': 'error',
// ***** JSDoc *****
'jsdoc/require-jsdoc': [
'error',
{
// Require on public parts of classes
checkConstructors: false,
contexts: [
'ClassDeclaration',
// TODO(cemmer): require private methods as well
'MethodDefinition[accessibility!=private][key.name!=/^(get|set|with)[A-Z][a-zA-Z]+/]',
],
},
],
'jsdoc/require-param': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/no-blank-blocks': 'error',
// ***** Promises *****
// ***** Functions *****
// ***** Errors *****
// ***** Operands *****
eqeqeq: 'error',
// ***** Conditionals *****
// ***** Loops *****
// ***** Objects *****
// ***** Arrays *****
// ***** Numbers *****
// ***** Strings *****
'prefer-template': 'error',
// ********** Recommended Overrides **********
// ***** unicorn:recommended *****
// Superseded by other rules
'unicorn/no-this-outside-of-class': 'off', // made safe by @typescript-eslint/unbound-method
// Fixes
'unicorn/prefer-single-call': [
'error',
{
// Readable#push() doesn't have a rest parameter 😡
ignore: ['readable.push'],
},
],
// Style and clarity preference differences
'unicorn/consistent-class-member-order': 'off',
'unicorn/consistent-function-scoping': ['error', { checkArrowFunctions: false }],
'unicorn/import-style': 'off', // mostly overridden by noNodeSubpathImports.mjs
'unicorn/max-nested-calls': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-break-in-nested-loop': 'off', // unicorn/no-duplicate-loops + unicorn/prefer-continue encourage this
'unicorn/no-hex-escape': 'off',
'unicorn/no-unreadable-new-expression': 'off',
'unicorn/no-useless-undefined': 'off',
'unicorn/prefer-string-raw': 'off',
'unicorn/prefer-switch': 'off',
'unicorn/prefer-ternary': 'off',
'unicorn/prefer-type-error': 'off',
'unicorn/prevent-abbreviations': 'off',
// Overly broad rules with too many false positives 😡
'unicorn/no-unsafe-string-replacement': 'off', // doesn't curly braces in regex search
'unicorn/prefer-await': 'off', // doesn't check if the call site is in an async context
'unicorn/prefer-https': 'off', // comments can't be excluded
'unicorn/prefer-iterator-to-array': 'off', // ArrayIterator#toArray() doesn't exist
'unicorn/prefer-minimal-ternary': 'off', // lots of false positives, hurts readability
// ***** ESLint:recommended *****
// Referencing ASCII characters <32 is entirely legitimate
'no-control-regex': 'off',
},
},
{
// Rules for TypeScript files
files: ['**/*.ts'],
rules: {
// ***** Types *****
// ***** Imports *****
'@typescript-eslint/consistent-type-exports': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/no-import-type-side-effects': 'error',
// ***** Promises *****
// Require any function or method that returns a Promise to be marked async.
'@typescript-eslint/promise-function-async': ['error'],
// Be even more strict than @typescript-eslint/strict-type-checked
'@typescript-eslint/return-await': ['error', 'always'],
// ***** Interfaces *****
'@typescript-eslint/method-signature-style': 'error',
// ***** Classes *****
'@typescript-eslint/explicit-member-accessibility': ['error', { accessibility: 'no-public' }],
'@typescript-eslint/prefer-readonly': 'error',
// A lot of utility classes contain private functions that shouldn't be exposed
'@typescript-eslint/no-extraneous-class': [
'error',
{
allowStaticOnly: true,
},
],
// TODO(cemmer)
'@typescript-eslint/no-misused-spread': 'off',
// ***** Functions *****
// Require explicit return types on functions and class methods.
'@typescript-eslint/explicit-function-return-type': 'error',
// ***** Conditionals *****
// Don't allow unnecessary conditional checks, such as when a value is always true, which can also help catch cases
// such as accidentally checking `if([]){}` vs. `if([].length){}`
'@typescript-eslint/strict-boolean-expressions': [
'error',
{
allowAny: true,
allowNullableBoolean: true,
allowNullableString: true,
},
],
// ***** Objects *****
'@typescript-eslint/no-unused-vars': [
'error',
{
/*** @ESLint/js defaults ***/
vars: 'all',
caughtErrors: 'all',
reportUsedIgnorePattern: false,
/*** Overrides ***/
args: 'all',
argsIgnorePattern: '^_',
// Allow the use of destructuring to remove keys from an object
ignoreRestSiblings: true,
},
],
// ********** Recommended Overrides **********
// ***** plugin:@typescript-eslint/strict-type-checked *****
// There are a few places where this needs to be allowed, but only a few, so warn on them
'@typescript-eslint/no-floating-promises': 'warn',
// There are a few places where this needs to be allowed, but only a few, so warn on them
'@typescript-eslint/no-unused-expressions': 'warn',
// TODO(cemmer): seems to conflict with Prettier with Array#reduce() calls
'@typescript-eslint/no-unnecessary-type-arguments': 'off',
},
},
{
// These files have switch cases on enum values, and have defensive
// programming in case it was written wrong
files: [
'src/factories/**/*.ts',
'src/models/files/archives/**/*.ts',
'src/models/patches/**/*.ts',
],
rules: {
'@typescript-eslint/no-unnecessary-condition': 'off',
},
},
// Ignore JSDoc requirements for some files
{
files: ['test/**/*.ts', 'packages/*/test/**/*.ts'],
rules: {
'jsdoc/require-jsdoc': 'off',
},
},
]);