@rushstack/eslint-config 2.4.5 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,837 +1,837 @@
1
- // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
- // See LICENSE in the project root for license information.
3
-
4
- const macros = require('./_macros');
5
-
6
- // Rule severity guidelines
7
- // ------------------------
8
- //
9
- // Errors are generally printed in red, and may prevent other build tasks from running (e.g. unit tests).
10
- // Developers should never ignore errors. Warnings are generally printed in yellow, and do not block local
11
- // development, although they must be fixed/suppressed before merging. Developers will commonly ignore warnings
12
- // until their feature is working.
13
- //
14
- // Rules that should be a WARNING:
15
- // - An issue that is very common in partially implemented work (e.g. missing type declaration)
16
- // - An issue that "keeps things nice" but otherwise doesn't affect the meaning of the code (e.g. naming convention)
17
- // - Security rules -- developers may need to temporarily introduce "insecure" expressions while debugging;
18
- // if our policy forces them to suppress the lint rule, they may forget to reenable it later.
19
- //
20
- // Rules that should be an ERROR:
21
- // - An issue that is very likely to be a typo (e.g. "x = x;")
22
- // - An issue that catches code that is likely to malfunction (e.g. unterminated promise chain)
23
- // - An obsolete language feature that nobody should be using for any good reason
24
-
25
- function buildRules(profile) {
26
- return {
27
- // After an .eslintrc.js file is loaded, ESLint will normally continue visiting all parent folders
28
- // to look for other .eslintrc.js files, and also consult a personal file ~/.eslintrc.js. If any files
29
- // are found, their options will be merged. This is difficult for humans to understand, and it will cause
30
- // nondeterministic behavior if files are loaded from outside the Git working folder.
31
- //
32
- // Setting root=true causes ESLint to stop looking for other config files after the first .eslintrc.js
33
- // is loaded.
34
- root: true,
35
-
36
- // Disable the parser by default
37
- parser: '',
38
-
39
- plugins: [
40
- // Plugin documentation: https://www.npmjs.com/package/@rushstack/eslint-plugin
41
- '@rushstack/eslint-plugin',
42
- // Plugin documentation: https://www.npmjs.com/package/@rushstack/eslint-plugin-security
43
- '@rushstack/eslint-plugin-security',
44
- // Plugin documentation: https://www.npmjs.com/package/@typescript-eslint/eslint-plugin
45
- '@typescript-eslint/eslint-plugin',
46
- // Plugin documentation: https://www.npmjs.com/package/eslint-plugin-promise
47
- 'eslint-plugin-promise'
48
- ],
49
-
50
- // Manually authored .d.ts files are generally used to describe external APIs that are not expected
51
- // to follow our coding conventions. Linting those files tends to produce a lot of spurious suppressions,
52
- // so we simply ignore them.
53
- ignorePatterns: ['*.d.ts'],
54
-
55
- overrides: [
56
- {
57
- // Declare an override that applies to TypeScript files only
58
- files: ['*.ts', '*.tsx'],
59
- parser: '@typescript-eslint/parser',
60
- parserOptions: {
61
- // The "project" path is resolved relative to parserOptions.tsconfigRootDir.
62
- // Your local .eslintrc.js must specify that parserOptions.tsconfigRootDir=__dirname.
63
- project: './tsconfig.json',
64
-
65
- // Allow parsing of newer ECMAScript constructs used in TypeScript source code. Although tsconfig.json
66
- // may allow only a small subset of ES2018 features, this liberal setting ensures that ESLint will correctly
67
- // parse whatever is encountered.
68
- ecmaVersion: 2018,
69
-
70
- sourceType: 'module'
71
- },
72
-
73
- rules: {
74
- // ====================================================================
75
- // CUSTOM RULES
76
- // ====================================================================
77
-
78
- // The @rushstack rules are documented in the package README:
79
- // https://www.npmjs.com/package/@rushstack/eslint-plugin
80
-
81
- // RATIONALE: See the @rushstack/eslint-plugin documentation
82
- '@rushstack/no-new-null': 'warn',
83
-
84
- // RATIONALE: See the @rushstack/eslint-plugin documentation
85
- '@rushstack/typedef-var': 'warn',
86
-
87
- // RATIONALE: See the @rushstack/eslint-plugin documentation
88
- // This is enabled and classified as an error because it is required when using Heft.
89
- // It's not required when using ts-jest, but still a good practice.
90
- '@rushstack/hoist-jest-mock': 'error',
91
-
92
- // ====================================================================
93
- // SECURITY RULES
94
- // ====================================================================
95
-
96
- // This is disabled for tools because, for example, it is a common and safe practice for a tool
97
- // to read a RegExp from a config file and use it to filter files paths.
98
- '@rushstack/security/no-unsafe-regexp': profile === 'node-trusted-tool' ? 'off' : 'warn',
99
-
100
- // ====================================================================
101
- // GENERAL RULES
102
- // ====================================================================
103
-
104
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
105
- '@typescript-eslint/adjacent-overload-signatures': 'warn',
106
-
107
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
108
- //
109
- // CONFIGURATION: By default, these are banned: String, Boolean, Number, Object, Symbol
110
- '@typescript-eslint/ban-types': [
111
- 'warn',
112
- {
113
- extendDefaults: false, // (the complete list is in this file)
114
- types: {
115
- String: {
116
- message: 'Use "string" instead',
117
- fixWith: 'string'
118
- },
119
- Boolean: {
120
- message: 'Use "boolean" instead',
121
- fixWith: 'boolean'
122
- },
123
- Number: {
124
- message: 'Use "number" instead',
125
- fixWith: 'number'
126
- },
127
- Object: {
128
- message: 'Use "object" instead, or else define a proper TypeScript type:'
129
- },
130
- Symbol: {
131
- message: 'Use "symbol" instead',
132
- fixWith: 'symbol'
133
- },
134
- Function: {
135
- message: [
136
- 'The "Function" type accepts any function-like value.',
137
- 'It provides no type safety when calling the function, which can be a common source of bugs.',
138
- 'It also accepts things like class declarations, which will throw at runtime as they will not be called with "new".',
139
- 'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.'
140
- ].join('\n')
141
- }
142
-
143
- // This is a good idea, but before enabling it we need to put some thought into the recommended
144
- // coding practices; the default suggestions are too vague.
145
- //
146
- // '{}': {
147
- // message: [
148
- // '"{}" actually means "any non-nullish value".',
149
- // '- If you want a type meaning "any object", you probably want "Record<string, unknown>" instead.',
150
- // '- If you want a type meaning "any value", you probably want "unknown" instead.'
151
- // ].join('\n')
152
- // }
153
- }
154
- }
155
- ],
156
-
157
- // RATIONALE: We require "x as number" instead of "<number>x" to avoid conflicts with JSX.
158
- '@typescript-eslint/consistent-type-assertions': 'warn',
159
-
160
- // RATIONALE: We prefer "interface IBlah { x: number }" over "type Blah = { x: number }"
161
- // because code is more readable when it is built from stereotypical forms
162
- // (interfaces, enums, functions, etc.) instead of freeform type algebra.
163
- '@typescript-eslint/consistent-type-definitions': 'warn',
164
-
165
- // RATIONALE: Code is more readable when the type of every variable is immediately obvious.
166
- // Even if the compiler may be able to infer a type, this inference will be unavailable
167
- // to a person who is reviewing a GitHub diff. This rule makes writing code harder,
168
- // but writing code is a much less important activity than reading it.
169
- //
170
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
171
- '@typescript-eslint/explicit-function-return-type': [
172
- 'warn',
173
- {
174
- allowExpressions: true,
175
- allowTypedFunctionExpressions: true,
176
- allowHigherOrderFunctions: false
177
- }
178
- ],
179
-
180
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
181
- '@typescript-eslint/explicit-member-accessibility': 'warn',
182
-
183
- // RATIONALE: Object-oriented programming organizes code into "classes" that associate
184
- // data structures (the class's fields) and the operations performed on those
185
- // data structures (the class's members). Studying the fields often reveals the "idea"
186
- // behind a class. The choice of which class a field belongs to may greatly impact
187
- // the code readability and complexity. Thus, we group the fields prominently at the top
188
- // of the class declaration. We do NOT enforce sorting based on public/protected/private
189
- // or static/instance, because these designations tend to change as code evolves, and
190
- // reordering methods produces spurious diffs that make PRs hard to read. For classes
191
- // with lots of methods, alphabetization is probably a more useful secondary ordering.
192
- '@typescript-eslint/member-ordering': [
193
- 'warn',
194
- {
195
- default: 'never',
196
- classes: ['field', 'constructor', 'method']
197
- }
198
- ],
199
-
200
- // NOTE: This new rule replaces several deprecated rules from @typescript-eslint/eslint-plugin@2.3.3:
201
- //
202
- // - @typescript-eslint/camelcase
203
- // - @typescript-eslint/class-name-casing
204
- // - @typescript-eslint/interface-name-prefix
205
- // - @typescript-eslint/member-naming
206
- //
207
- // Docs: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md
208
- '@typescript-eslint/naming-convention': [
209
- 'warn',
210
- ...macros.expandNamingConventionSelectors([
211
- {
212
- // We should be stricter about 'enumMember', but it often functions legitimately as an ad hoc namespace.
213
- selectors: ['variable', 'enumMember', 'function'],
214
-
215
- format: ['camelCase', 'UPPER_CASE', 'PascalCase'],
216
- leadingUnderscore: 'allow',
217
-
218
- filter: {
219
- regex: [
220
- // This is a special exception for naming patterns that use an underscore to separate two camel-cased
221
- // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
222
- '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
223
- ]
224
- .map((x) => `(${x})`)
225
- .join('|'),
226
- match: false
227
- }
228
- },
229
-
230
- {
231
- selectors: ['parameter'],
232
-
233
- format: ['camelCase'],
234
-
235
- filter: {
236
- regex: [
237
- // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
238
- // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
239
- '^__'
240
- ]
241
- .map((x) => `(${x})`)
242
- .join('|'),
243
- match: false
244
- }
245
- },
246
-
247
- // Genuine properties
248
- {
249
- selectors: ['parameterProperty', 'accessor'],
250
- enforceLeadingUnderscoreWhenPrivate: true,
251
-
252
- format: ['camelCase', 'UPPER_CASE'],
253
-
254
- filter: {
255
- regex: [
256
- // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
257
- // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
258
- '^__',
259
- // Ignore quoted identifiers such as { "X+Y": 123 }. Currently @typescript-eslint/naming-convention
260
- // cannot detect whether an identifier is quoted or not, so we simply assume that it is quoted
261
- // if-and-only-if it contains characters that require quoting.
262
- '[^a-zA-Z0-9_]',
263
- // This is a special exception for naming patterns that use an underscore to separate two camel-cased
264
- // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
265
- '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
266
- ]
267
- .map((x) => `(${x})`)
268
- .join('|'),
269
- match: false
270
- }
271
- },
272
-
273
- // Properties that incorrectly match other contexts
274
- // See issue https://github.com/typescript-eslint/typescript-eslint/issues/2244
275
- {
276
- selectors: ['property'],
277
- enforceLeadingUnderscoreWhenPrivate: true,
278
-
279
- // The @typescript-eslint/naming-convention "property" selector matches cases like this:
280
- //
281
- // someLegacyApiWeCannotChange.invokeMethod({ SomeProperty: 123 });
282
- //
283
- // and this:
284
- //
285
- // const { CONSTANT1, CONSTANT2 } = someNamespace.constants;
286
- //
287
- // Thus for now "property" is more like a variable than a class member.
288
- format: ['camelCase', 'UPPER_CASE', 'PascalCase'],
289
- leadingUnderscore: 'allow',
290
-
291
- filter: {
292
- regex: [
293
- // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
294
- // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
295
- '^__',
296
- // Ignore quoted identifiers such as { "X+Y": 123 }. Currently @typescript-eslint/naming-convention
297
- // cannot detect whether an identifier is quoted or not, so we simply assume that it is quoted
298
- // if-and-only-if it contains characters that require quoting.
299
- '[^a-zA-Z0-9_]',
300
- // This is a special exception for naming patterns that use an underscore to separate two camel-cased
301
- // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
302
- '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
303
- ]
304
- .map((x) => `(${x})`)
305
- .join('|'),
306
- match: false
307
- }
308
- },
309
-
310
- {
311
- selectors: ['method'],
312
- enforceLeadingUnderscoreWhenPrivate: true,
313
-
314
- // A PascalCase method can arise somewhat legitimately in this way:
315
- //
316
- // class MyClass {
317
- // public static MyReactButton(props: IButtonProps): JSX.Element {
318
- // . . .
319
- // }
320
- // }
321
- format: ['camelCase', 'PascalCase'],
322
- leadingUnderscore: 'allow',
323
-
324
- filter: {
325
- regex: [
326
- // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
327
- // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
328
- '^__',
329
- // This is a special exception for naming patterns that use an underscore to separate two camel-cased
330
- // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
331
- '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
332
- ]
333
- .map((x) => `(${x})`)
334
- .join('|'),
335
- match: false
336
- }
337
- },
338
-
339
- // Types should use PascalCase
340
- {
341
- // Group selector for: class, interface, typeAlias, enum, typeParameter
342
- selectors: ['class', 'typeAlias', 'enum', 'typeParameter'],
343
- format: ['PascalCase'],
344
- leadingUnderscore: 'allow'
345
- },
346
-
347
- {
348
- selectors: ['interface'],
349
-
350
- // It is very common for a class to implement an interface of the same name.
351
- // For example, the Widget class may implement the IWidget interface. The "I" prefix
352
- // avoids the need to invent a separate name such as "AbstractWidget" or "WidgetInterface".
353
- // In TypeScript it is also common to declare interfaces that are implemented by primitive
354
- // objects, here the "I" prefix also helps by avoiding spurious conflicts with classes
355
- // by the same name.
356
- format: ['PascalCase'],
357
-
358
- custom: {
359
- regex: '^_?I[A-Z]',
360
- match: true
361
- }
362
- }
363
- ])
364
- ],
365
-
366
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
367
- '@typescript-eslint/no-array-constructor': 'warn',
368
-
369
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
370
- //
371
- // RATIONALE: The "any" keyword disables static type checking, the main benefit of using TypeScript.
372
- // This rule should be suppressed only in very special cases such as JSON.stringify()
373
- // where the type really can be anything. Even if the type is flexible, another type
374
- // may be more appropriate such as "unknown", "{}", or "Record<k,V>".
375
- '@typescript-eslint/no-explicit-any': 'warn',
376
-
377
- // RATIONALE: The #1 rule of promises is that every promise chain must be terminated by a catch()
378
- // handler. Thus wherever a Promise arises, the code must either append a catch handler,
379
- // or else return the object to a caller (who assumes this responsibility). Unterminated
380
- // promise chains are a serious issue. Besides causing errors to be silently ignored,
381
- // they can also cause a NodeJS process to terminate unexpectedly.
382
- '@typescript-eslint/no-floating-promises': 'error',
383
-
384
- // RATIONALE: Catches a common coding mistake.
385
- '@typescript-eslint/no-for-in-array': 'error',
386
-
387
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
388
- '@typescript-eslint/no-misused-new': 'error',
389
-
390
- // RATIONALE: The "namespace" keyword is not recommended for organizing code because JavaScript lacks
391
- // a "using" statement to traverse namespaces. Nested namespaces prevent certain bundler
392
- // optimizations. If you are declaring loose functions/variables, it's better to make them
393
- // static members of a class, since classes support property getters and their private
394
- // members are accessible by unit tests. Also, the exercise of choosing a meaningful
395
- // class name tends to produce more discoverable APIs: for example, search+replacing
396
- // the function "reverse()" is likely to return many false matches, whereas if we always
397
- // write "Text.reverse()" is more unique. For large scale organization, it's recommended
398
- // to decompose your code into separate NPM packages, which ensures that component
399
- // dependencies are tracked more conscientiously.
400
- //
401
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
402
- '@typescript-eslint/no-namespace': [
403
- 'warn',
404
- {
405
- // Discourage "namespace" in .ts and .tsx files
406
- allowDeclarations: false,
407
-
408
- // Allow it in .d.ts files that describe legacy libraries
409
- allowDefinitionFiles: false
410
- }
411
- ],
412
-
413
- // RATIONALE: Parameter properties provide a shorthand such as "constructor(public title: string)"
414
- // that avoids the effort of declaring "title" as a field. This TypeScript feature makes
415
- // code easier to write, but arguably sacrifices readability: In the notes for
416
- // "@typescript-eslint/member-ordering" we pointed out that fields are central to
417
- // a class's design, so we wouldn't want to bury them in a constructor signature
418
- // just to save some typing.
419
- //
420
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
421
- '@typescript-eslint/no-parameter-properties': 'warn',
422
-
423
- // RATIONALE: When left in shipping code, unused variables often indicate a mistake. Dead code
424
- // may impact performance.
425
- //
426
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
427
- '@typescript-eslint/no-unused-vars': [
428
- 'warn',
429
- {
430
- vars: 'all',
431
- // Unused function arguments often indicate a mistake in JavaScript code. However in TypeScript code,
432
- // the compiler catches most of those mistakes, and unused arguments are fairly common for type signatures
433
- // that are overriding a base class method or implementing an interface.
434
- args: 'none'
435
- }
436
- ],
437
-
438
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
439
- '@typescript-eslint/no-use-before-define': [
440
- 'error',
441
- {
442
- // Base ESLint options
443
-
444
- // We set functions=false so that functions can be ordered based on exported/local visibility
445
- // similar to class methods. Also the base lint rule incorrectly flags a legitimate case like:
446
- //
447
- // function a(n: number): void {
448
- // if (n > 0) {
449
- // b(n-1); // lint error
450
- // }
451
- // }
452
- // function b(n: number): void {
453
- // if (n > 0) {
454
- // a(n-1);
455
- // }
456
- // }
457
- functions: false,
458
- classes: true,
459
- variables: true,
460
-
461
- // TypeScript extensions
462
-
463
- enums: true,
464
- typedefs: true
465
- // ignoreTypeReferences: true
466
- }
467
- ],
468
-
469
- // TODO: This is a good rule for web browser apps, but it is commonly needed API for Node.js tools.
470
- // '@typescript-eslint/no-var-requires': 'error',
471
-
472
- // RATIONALE: The "module" keyword is deprecated except when describing legacy libraries.
473
- //
474
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
475
- '@typescript-eslint/prefer-namespace-keyword': 'warn',
476
-
477
- // RATIONALE: We require explicit type annotations, even when the compiler could infer the type.
478
- // This can be a controversial policy because it makes code more verbose. There are
479
- // a couple downsides to type inference, however. First, it is not always available.
480
- // For example, when reviewing a pull request or examining a Git history, we may see
481
- // code like this:
482
- //
483
- // // What is the type of "y" here? The compiler knows, but the
484
- // // person reading the code may have no clue.
485
- // const x = f.();
486
- // const y = x.z;
487
- //
488
- // Second, relying on implicit types also discourages design discussions and documentation.
489
- // Consider this example:
490
- //
491
- // // Where's the documentation for "correlation" and "inventory"?
492
- // // Where would you even write the TSDoc comments?
493
- // function g() {
494
- // return { correlation: 123, inventory: 'xyz' };
495
- // }
496
- //
497
- // Implicit types make sense for small scale scenarios, where everyone is familiar with
498
- // the project, and code should be "easy to write". Explicit types are preferable
499
- // for large scale scenarios, where people regularly work with source files they've never
500
- // seen before, and code should be "easy to read."
501
- //
502
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
503
- '@typescript-eslint/typedef': [
504
- 'warn',
505
- {
506
- arrayDestructuring: false,
507
- arrowParameter: false,
508
- memberVariableDeclaration: true,
509
- objectDestructuring: false,
510
- parameter: true,
511
- propertyDeclaration: true,
512
-
513
- // This case is handled by our "@rushstack/typedef-var" rule
514
- variableDeclaration: false,
515
-
516
- // Normally we require type declarations for class members. However, that rule is relaxed
517
- // for situations where we need to bind the "this" pointer for a callback. For example, consider
518
- // this event handler for a React component:
519
- //
520
- // class MyComponent {
521
- // public render(): React.ReactNode {
522
- // return (
523
- // <a href="#" onClick={this._onClick}> click me </a>
524
- // );
525
- // }
526
- //
527
- // // The assignment here avoids the need for "this._onClick.bind(this)"
528
- // private _onClick = (event: React.MouseEvent<HTMLAnchorElement>): void => {
529
- // console.log("Clicked! " + this.props.title);
530
- // };
531
- // }
532
- //
533
- // This coding style has limitations and should be used sparingly. For example, "_onClick"
534
- // will not participate correctly in "virtual"/"override" inheritance.
535
- //
536
- // NOTE: This option affects both "memberVariableDeclaration" and "variableDeclaration" options.
537
- variableDeclarationIgnoreFunction: true
538
- }
539
- ],
540
-
541
- // RATIONALE: This rule warns if setters are defined without getters, which is probably a mistake.
542
- 'accessor-pairs': 'error',
543
-
544
- // RATIONALE: In TypeScript, if you write x["y"] instead of x.y, it disables type checking.
545
- 'dot-notation': [
546
- 'warn',
547
- {
548
- allowPattern: '^_'
549
- }
550
- ],
551
-
552
- // RATIONALE: Catches code that is likely to be incorrect
553
- eqeqeq: 'error',
554
-
555
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
556
- 'for-direction': 'warn',
557
-
558
- // RATIONALE: Catches a common coding mistake.
559
- 'guard-for-in': 'error',
560
-
561
- // RATIONALE: If you have more than 2,000 lines in a single source file, it's probably time
562
- // to split up your code.
563
- 'max-lines': ['warn', { max: 2000 }],
564
-
565
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
566
- 'no-async-promise-executor': 'error',
567
-
568
- // RATIONALE: "|" and "&" are relatively rare, and are more likely to appear as a mistake when
569
- // someone meant "||" or "&&". (But nobody types the other operators by mistake.)
570
- 'no-bitwise': [
571
- 'warn',
572
- {
573
- allow: [
574
- '^',
575
- // "|",
576
- // "&",
577
- '<<',
578
- '>>',
579
- '>>>',
580
- '^=',
581
- // "|=",
582
- //"&=",
583
- '<<=',
584
- '>>=',
585
- '>>>=',
586
- '~'
587
- ]
588
- }
589
- ],
590
-
591
- // RATIONALE: Deprecated language feature.
592
- 'no-caller': 'error',
593
-
594
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
595
- 'no-compare-neg-zero': 'error',
596
-
597
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
598
- 'no-cond-assign': 'error',
599
-
600
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
601
- 'no-constant-condition': 'warn',
602
-
603
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
604
- 'no-control-regex': 'error',
605
-
606
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
607
- 'no-debugger': 'warn',
608
-
609
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
610
- 'no-delete-var': 'error',
611
-
612
- // RATIONALE: Catches code that is likely to be incorrect
613
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
614
- 'no-duplicate-case': 'error',
615
-
616
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
617
- 'no-empty': 'warn',
618
-
619
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
620
- 'no-empty-character-class': 'error',
621
-
622
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
623
- 'no-empty-pattern': 'warn',
624
-
625
- // RATIONALE: Eval is a security concern and a performance concern.
626
- 'no-eval': 'warn',
627
-
628
- // RATIONALE: Catches code that is likely to be incorrect
629
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
630
- 'no-ex-assign': 'error',
631
-
632
- // RATIONALE: System types are global and should not be tampered with in a scalable code base.
633
- // If two different libraries (or two versions of the same library) both try to modify
634
- // a type, only one of them can win. Polyfills are acceptable because they implement
635
- // a standardized interoperable contract, but polyfills are generally coded in plain
636
- // JavaScript.
637
- 'no-extend-native': 'error',
638
-
639
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
640
- 'no-extra-boolean-cast': 'warn',
641
-
642
- 'no-extra-label': 'warn',
643
-
644
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
645
- 'no-fallthrough': 'error',
646
-
647
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
648
- 'no-func-assign': 'warn',
649
-
650
- // RATIONALE: Catches a common coding mistake.
651
- 'no-implied-eval': 'error',
652
-
653
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
654
- 'no-invalid-regexp': 'error',
655
-
656
- // RATIONALE: Catches a common coding mistake.
657
- 'no-label-var': 'error',
658
-
659
- // RATIONALE: Eliminates redundant code.
660
- 'no-lone-blocks': 'warn',
661
-
662
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
663
- 'no-misleading-character-class': 'error',
664
-
665
- // RATIONALE: Catches a common coding mistake.
666
- 'no-multi-str': 'error',
667
-
668
- // RATIONALE: It's generally a bad practice to call "new Thing()" without assigning the result to
669
- // a variable. Either it's part of an awkward expression like "(new Thing()).doSomething()",
670
- // or else implies that the constructor is doing nontrivial computations, which is often
671
- // a poor class design.
672
- 'no-new': 'warn',
673
-
674
- // RATIONALE: Obsolete language feature that is deprecated.
675
- 'no-new-func': 'error',
676
-
677
- // RATIONALE: Obsolete language feature that is deprecated.
678
- 'no-new-object': 'error',
679
-
680
- // RATIONALE: Obsolete notation.
681
- 'no-new-wrappers': 'warn',
682
-
683
- // RATIONALE: Catches code that is likely to be incorrect
684
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
685
- 'no-octal': 'error',
686
-
687
- // RATIONALE: Catches code that is likely to be incorrect
688
- 'no-octal-escape': 'error',
689
-
690
- // RATIONALE: Catches code that is likely to be incorrect
691
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
692
- 'no-regex-spaces': 'error',
693
-
694
- // RATIONALE: Catches a common coding mistake.
695
- 'no-return-assign': 'error',
696
-
697
- // RATIONALE: Security risk.
698
- 'no-script-url': 'warn',
699
-
700
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
701
- 'no-self-assign': 'error',
702
-
703
- // RATIONALE: Catches a common coding mistake.
704
- 'no-self-compare': 'error',
705
-
706
- // RATIONALE: This avoids statements such as "while (a = next(), a && a.length);" that use
707
- // commas to create compound expressions. In general code is more readable if each
708
- // step is split onto a separate line. This also makes it easier to set breakpoints
709
- // in the debugger.
710
- 'no-sequences': 'error',
711
-
712
- // RATIONALE: Catches code that is likely to be incorrect
713
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
714
- 'no-shadow-restricted-names': 'error',
715
-
716
- // RATIONALE: Obsolete language feature that is deprecated.
717
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
718
- 'no-sparse-arrays': 'error',
719
-
720
- // RATIONALE: Although in theory JavaScript allows any possible data type to be thrown as an exception,
721
- // such flexibility adds pointless complexity, by requiring every catch block to test
722
- // the type of the object that it receives. Whereas if catch blocks can always assume
723
- // that their object implements the "Error" contract, then the code is simpler, and
724
- // we generally get useful additional information like a call stack.
725
- 'no-throw-literal': 'error',
726
-
727
- // RATIONALE: Catches a common coding mistake.
728
- 'no-unmodified-loop-condition': 'warn',
729
-
730
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
731
- 'no-unsafe-finally': 'error',
732
-
733
- // RATIONALE: Catches a common coding mistake.
734
- 'no-unused-expressions': 'warn',
735
-
736
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
737
- 'no-unused-labels': 'warn',
738
-
739
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
740
- 'no-useless-catch': 'warn',
741
-
742
- // RATIONALE: Avoids a potential performance problem.
743
- 'no-useless-concat': 'warn',
744
-
745
- // RATIONALE: The "var" keyword is deprecated because of its confusing "hoisting" behavior.
746
- // Always use "let" or "const" instead.
747
- //
748
- // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
749
- 'no-var': 'error',
750
-
751
- // RATIONALE: Generally not needed in modern code.
752
- 'no-void': 'error',
753
-
754
- // RATIONALE: Obsolete language feature that is deprecated.
755
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
756
- 'no-with': 'error',
757
-
758
- // RATIONALE: Makes logic easier to understand, since constants always have a known value
759
- // @typescript-eslint\eslint-plugin\dist\configs\eslint-recommended.js
760
- 'prefer-const': 'warn',
761
-
762
- // RATIONALE: Catches a common coding mistake where "resolve" and "reject" are confused.
763
- 'promise/param-names': 'error',
764
-
765
- // RATIONALE: Catches code that is likely to be incorrect
766
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
767
- 'require-atomic-updates': 'error',
768
-
769
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
770
- 'require-yield': 'warn',
771
-
772
- // "Use strict" is redundant when using the TypeScript compiler.
773
- strict: ['error', 'never'],
774
-
775
- // RATIONALE: Catches code that is likely to be incorrect
776
- // STANDARDIZED BY: eslint\conf\eslint-recommended.js
777
- 'use-isnan': 'error'
778
-
779
- // The "no-restricted-syntax" rule is a general purpose pattern matcher that we can use to experiment with
780
- // new rules. If a rule works well, we should convert it to a proper rule so it gets its own name
781
- // for suppressions and documentation.
782
- // How it works: https://eslint.org/docs/rules/no-restricted-syntax
783
- // AST visualizer: https://astexplorer.net/
784
- // Debugger: http://estools.github.io/esquery/
785
- //
786
- // "no-restricted-syntax": [
787
- // ],
788
- }
789
- },
790
- {
791
- // For unit tests, we can be a little bit less strict. The settings below revise the
792
- // defaults specified above.
793
- files: [
794
- // Test files
795
- '*.test.ts',
796
- '*.test.tsx',
797
- '*.spec.ts',
798
- '*.spec.tsx',
799
-
800
- // Facebook convention
801
- '**/__mocks__/*.ts',
802
- '**/__mocks__/*.tsx',
803
- '**/__tests__/*.ts',
804
- '**/__tests__/*.tsx',
805
-
806
- // Microsoft convention
807
- '**/test/*.ts',
808
- '**/test/*.tsx'
809
- ],
810
- rules: {
811
- // Unit tests sometimes use a standalone statement like "new Thing(123);" to test a constructor.
812
- 'no-new': 'off',
813
-
814
- // Jest's mocking API is designed in a way that produces compositional data types that often have
815
- // no concise description. Since test code does not ship, and typically does not introduce new
816
- // concepts or algorithms, the usual arguments for prioritizing readability over writability can be
817
- // relaxed in this case.
818
- '@rushstack/typedef-var': 'off',
819
- '@typescript-eslint/typedef': [
820
- 'warn',
821
- {
822
- arrayDestructuring: false,
823
- arrowParameter: false,
824
- memberVariableDeclaration: true,
825
- objectDestructuring: false,
826
- parameter: true,
827
- propertyDeclaration: true,
828
- variableDeclaration: false, // <--- special case for test files
829
- variableDeclarationIgnoreFunction: true
830
- }
831
- ]
832
- }
833
- }
834
- ]
835
- };
836
- }
837
- exports.buildRules = buildRules;
1
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
+ // See LICENSE in the project root for license information.
3
+
4
+ const macros = require('./_macros');
5
+
6
+ // Rule severity guidelines
7
+ // ------------------------
8
+ //
9
+ // Errors are generally printed in red, and may prevent other build tasks from running (e.g. unit tests).
10
+ // Developers should never ignore errors. Warnings are generally printed in yellow, and do not block local
11
+ // development, although they must be fixed/suppressed before merging. Developers will commonly ignore warnings
12
+ // until their feature is working.
13
+ //
14
+ // Rules that should be a WARNING:
15
+ // - An issue that is very common in partially implemented work (e.g. missing type declaration)
16
+ // - An issue that "keeps things nice" but otherwise doesn't affect the meaning of the code (e.g. naming convention)
17
+ // - Security rules -- developers may need to temporarily introduce "insecure" expressions while debugging;
18
+ // if our policy forces them to suppress the lint rule, they may forget to reenable it later.
19
+ //
20
+ // Rules that should be an ERROR:
21
+ // - An issue that is very likely to be a typo (e.g. "x = x;")
22
+ // - An issue that catches code that is likely to malfunction (e.g. unterminated promise chain)
23
+ // - An obsolete language feature that nobody should be using for any good reason
24
+
25
+ function buildRules(profile) {
26
+ return {
27
+ // After an .eslintrc.js file is loaded, ESLint will normally continue visiting all parent folders
28
+ // to look for other .eslintrc.js files, and also consult a personal file ~/.eslintrc.js. If any files
29
+ // are found, their options will be merged. This is difficult for humans to understand, and it will cause
30
+ // nondeterministic behavior if files are loaded from outside the Git working folder.
31
+ //
32
+ // Setting root=true causes ESLint to stop looking for other config files after the first .eslintrc.js
33
+ // is loaded.
34
+ root: true,
35
+
36
+ // Disable the parser by default
37
+ parser: '',
38
+
39
+ plugins: [
40
+ // Plugin documentation: https://www.npmjs.com/package/@rushstack/eslint-plugin
41
+ '@rushstack/eslint-plugin',
42
+ // Plugin documentation: https://www.npmjs.com/package/@rushstack/eslint-plugin-security
43
+ '@rushstack/eslint-plugin-security',
44
+ // Plugin documentation: https://www.npmjs.com/package/@typescript-eslint/eslint-plugin
45
+ '@typescript-eslint/eslint-plugin',
46
+ // Plugin documentation: https://www.npmjs.com/package/eslint-plugin-promise
47
+ 'eslint-plugin-promise'
48
+ ],
49
+
50
+ // Manually authored .d.ts files are generally used to describe external APIs that are not expected
51
+ // to follow our coding conventions. Linting those files tends to produce a lot of spurious suppressions,
52
+ // so we simply ignore them.
53
+ ignorePatterns: ['*.d.ts'],
54
+
55
+ overrides: [
56
+ {
57
+ // Declare an override that applies to TypeScript files only
58
+ files: ['*.ts', '*.tsx'],
59
+ parser: '@typescript-eslint/parser',
60
+ parserOptions: {
61
+ // The "project" path is resolved relative to parserOptions.tsconfigRootDir.
62
+ // Your local .eslintrc.js must specify that parserOptions.tsconfigRootDir=__dirname.
63
+ project: './tsconfig.json',
64
+
65
+ // Allow parsing of newer ECMAScript constructs used in TypeScript source code. Although tsconfig.json
66
+ // may allow only a small subset of ES2018 features, this liberal setting ensures that ESLint will correctly
67
+ // parse whatever is encountered.
68
+ ecmaVersion: 2018,
69
+
70
+ sourceType: 'module'
71
+ },
72
+
73
+ rules: {
74
+ // ====================================================================
75
+ // CUSTOM RULES
76
+ // ====================================================================
77
+
78
+ // The @rushstack rules are documented in the package README:
79
+ // https://www.npmjs.com/package/@rushstack/eslint-plugin
80
+
81
+ // RATIONALE: See the @rushstack/eslint-plugin documentation
82
+ '@rushstack/no-new-null': 'warn',
83
+
84
+ // RATIONALE: See the @rushstack/eslint-plugin documentation
85
+ '@rushstack/typedef-var': 'warn',
86
+
87
+ // RATIONALE: See the @rushstack/eslint-plugin documentation
88
+ // This is enabled and classified as an error because it is required when using Heft.
89
+ // It's not required when using ts-jest, but still a good practice.
90
+ '@rushstack/hoist-jest-mock': 'error',
91
+
92
+ // ====================================================================
93
+ // SECURITY RULES
94
+ // ====================================================================
95
+
96
+ // This is disabled for tools because, for example, it is a common and safe practice for a tool
97
+ // to read a RegExp from a config file and use it to filter files paths.
98
+ '@rushstack/security/no-unsafe-regexp': profile === 'node-trusted-tool' ? 'off' : 'warn',
99
+
100
+ // ====================================================================
101
+ // GENERAL RULES
102
+ // ====================================================================
103
+
104
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
105
+ '@typescript-eslint/adjacent-overload-signatures': 'warn',
106
+
107
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
108
+ //
109
+ // CONFIGURATION: By default, these are banned: String, Boolean, Number, Object, Symbol
110
+ '@typescript-eslint/ban-types': [
111
+ 'warn',
112
+ {
113
+ extendDefaults: false, // (the complete list is in this file)
114
+ types: {
115
+ String: {
116
+ message: 'Use "string" instead',
117
+ fixWith: 'string'
118
+ },
119
+ Boolean: {
120
+ message: 'Use "boolean" instead',
121
+ fixWith: 'boolean'
122
+ },
123
+ Number: {
124
+ message: 'Use "number" instead',
125
+ fixWith: 'number'
126
+ },
127
+ Object: {
128
+ message: 'Use "object" instead, or else define a proper TypeScript type:'
129
+ },
130
+ Symbol: {
131
+ message: 'Use "symbol" instead',
132
+ fixWith: 'symbol'
133
+ },
134
+ Function: {
135
+ message: [
136
+ 'The "Function" type accepts any function-like value.',
137
+ 'It provides no type safety when calling the function, which can be a common source of bugs.',
138
+ 'It also accepts things like class declarations, which will throw at runtime as they will not be called with "new".',
139
+ 'If you are expecting the function to accept certain arguments, you should explicitly define the function shape.'
140
+ ].join('\n')
141
+ }
142
+
143
+ // This is a good idea, but before enabling it we need to put some thought into the recommended
144
+ // coding practices; the default suggestions are too vague.
145
+ //
146
+ // '{}': {
147
+ // message: [
148
+ // '"{}" actually means "any non-nullish value".',
149
+ // '- If you want a type meaning "any object", you probably want "Record<string, unknown>" instead.',
150
+ // '- If you want a type meaning "any value", you probably want "unknown" instead.'
151
+ // ].join('\n')
152
+ // }
153
+ }
154
+ }
155
+ ],
156
+
157
+ // RATIONALE: We require "x as number" instead of "<number>x" to avoid conflicts with JSX.
158
+ '@typescript-eslint/consistent-type-assertions': 'warn',
159
+
160
+ // RATIONALE: We prefer "interface IBlah { x: number }" over "type Blah = { x: number }"
161
+ // because code is more readable when it is built from stereotypical forms
162
+ // (interfaces, enums, functions, etc.) instead of freeform type algebra.
163
+ '@typescript-eslint/consistent-type-definitions': 'warn',
164
+
165
+ // RATIONALE: Code is more readable when the type of every variable is immediately obvious.
166
+ // Even if the compiler may be able to infer a type, this inference will be unavailable
167
+ // to a person who is reviewing a GitHub diff. This rule makes writing code harder,
168
+ // but writing code is a much less important activity than reading it.
169
+ //
170
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
171
+ '@typescript-eslint/explicit-function-return-type': [
172
+ 'warn',
173
+ {
174
+ allowExpressions: true,
175
+ allowTypedFunctionExpressions: true,
176
+ allowHigherOrderFunctions: false
177
+ }
178
+ ],
179
+
180
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
181
+ '@typescript-eslint/explicit-member-accessibility': 'warn',
182
+
183
+ // RATIONALE: Object-oriented programming organizes code into "classes" that associate
184
+ // data structures (the class's fields) and the operations performed on those
185
+ // data structures (the class's members). Studying the fields often reveals the "idea"
186
+ // behind a class. The choice of which class a field belongs to may greatly impact
187
+ // the code readability and complexity. Thus, we group the fields prominently at the top
188
+ // of the class declaration. We do NOT enforce sorting based on public/protected/private
189
+ // or static/instance, because these designations tend to change as code evolves, and
190
+ // reordering methods produces spurious diffs that make PRs hard to read. For classes
191
+ // with lots of methods, alphabetization is probably a more useful secondary ordering.
192
+ '@typescript-eslint/member-ordering': [
193
+ 'warn',
194
+ {
195
+ default: 'never',
196
+ classes: ['field', 'constructor', 'method']
197
+ }
198
+ ],
199
+
200
+ // NOTE: This new rule replaces several deprecated rules from @typescript-eslint/eslint-plugin@2.3.3:
201
+ //
202
+ // - @typescript-eslint/camelcase
203
+ // - @typescript-eslint/class-name-casing
204
+ // - @typescript-eslint/interface-name-prefix
205
+ // - @typescript-eslint/member-naming
206
+ //
207
+ // Docs: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md
208
+ '@typescript-eslint/naming-convention': [
209
+ 'warn',
210
+ ...macros.expandNamingConventionSelectors([
211
+ {
212
+ // We should be stricter about 'enumMember', but it often functions legitimately as an ad hoc namespace.
213
+ selectors: ['variable', 'enumMember', 'function'],
214
+
215
+ format: ['camelCase', 'UPPER_CASE', 'PascalCase'],
216
+ leadingUnderscore: 'allow',
217
+
218
+ filter: {
219
+ regex: [
220
+ // This is a special exception for naming patterns that use an underscore to separate two camel-cased
221
+ // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
222
+ '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
223
+ ]
224
+ .map((x) => `(${x})`)
225
+ .join('|'),
226
+ match: false
227
+ }
228
+ },
229
+
230
+ {
231
+ selectors: ['parameter'],
232
+
233
+ format: ['camelCase'],
234
+
235
+ filter: {
236
+ regex: [
237
+ // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
238
+ // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
239
+ '^__'
240
+ ]
241
+ .map((x) => `(${x})`)
242
+ .join('|'),
243
+ match: false
244
+ }
245
+ },
246
+
247
+ // Genuine properties
248
+ {
249
+ selectors: ['parameterProperty', 'accessor'],
250
+ enforceLeadingUnderscoreWhenPrivate: true,
251
+
252
+ format: ['camelCase', 'UPPER_CASE'],
253
+
254
+ filter: {
255
+ regex: [
256
+ // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
257
+ // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
258
+ '^__',
259
+ // Ignore quoted identifiers such as { "X+Y": 123 }. Currently @typescript-eslint/naming-convention
260
+ // cannot detect whether an identifier is quoted or not, so we simply assume that it is quoted
261
+ // if-and-only-if it contains characters that require quoting.
262
+ '[^a-zA-Z0-9_]',
263
+ // This is a special exception for naming patterns that use an underscore to separate two camel-cased
264
+ // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
265
+ '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
266
+ ]
267
+ .map((x) => `(${x})`)
268
+ .join('|'),
269
+ match: false
270
+ }
271
+ },
272
+
273
+ // Properties that incorrectly match other contexts
274
+ // See issue https://github.com/typescript-eslint/typescript-eslint/issues/2244
275
+ {
276
+ selectors: ['property'],
277
+ enforceLeadingUnderscoreWhenPrivate: true,
278
+
279
+ // The @typescript-eslint/naming-convention "property" selector matches cases like this:
280
+ //
281
+ // someLegacyApiWeCannotChange.invokeMethod({ SomeProperty: 123 });
282
+ //
283
+ // and this:
284
+ //
285
+ // const { CONSTANT1, CONSTANT2 } = someNamespace.constants;
286
+ //
287
+ // Thus for now "property" is more like a variable than a class member.
288
+ format: ['camelCase', 'UPPER_CASE', 'PascalCase'],
289
+ leadingUnderscore: 'allow',
290
+
291
+ filter: {
292
+ regex: [
293
+ // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
294
+ // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
295
+ '^__',
296
+ // Ignore quoted identifiers such as { "X+Y": 123 }. Currently @typescript-eslint/naming-convention
297
+ // cannot detect whether an identifier is quoted or not, so we simply assume that it is quoted
298
+ // if-and-only-if it contains characters that require quoting.
299
+ '[^a-zA-Z0-9_]',
300
+ // This is a special exception for naming patterns that use an underscore to separate two camel-cased
301
+ // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
302
+ '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
303
+ ]
304
+ .map((x) => `(${x})`)
305
+ .join('|'),
306
+ match: false
307
+ }
308
+ },
309
+
310
+ {
311
+ selectors: ['method'],
312
+ enforceLeadingUnderscoreWhenPrivate: true,
313
+
314
+ // A PascalCase method can arise somewhat legitimately in this way:
315
+ //
316
+ // class MyClass {
317
+ // public static MyReactButton(props: IButtonProps): JSX.Element {
318
+ // . . .
319
+ // }
320
+ // }
321
+ format: ['camelCase', 'PascalCase'],
322
+ leadingUnderscore: 'allow',
323
+
324
+ filter: {
325
+ regex: [
326
+ // Silently accept names with a double-underscore prefix; we would like to be more strict about this,
327
+ // pending a fix for https://github.com/typescript-eslint/typescript-eslint/issues/2240
328
+ '^__',
329
+ // This is a special exception for naming patterns that use an underscore to separate two camel-cased
330
+ // parts. Example: "checkBox1_onChanged" or "_checkBox1_onChanged"
331
+ '^_?[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*_[a-z][a-z0-9]*([A-Z][a-z]?[a-z0-9]*)*$'
332
+ ]
333
+ .map((x) => `(${x})`)
334
+ .join('|'),
335
+ match: false
336
+ }
337
+ },
338
+
339
+ // Types should use PascalCase
340
+ {
341
+ // Group selector for: class, interface, typeAlias, enum, typeParameter
342
+ selectors: ['class', 'typeAlias', 'enum', 'typeParameter'],
343
+ format: ['PascalCase'],
344
+ leadingUnderscore: 'allow'
345
+ },
346
+
347
+ {
348
+ selectors: ['interface'],
349
+
350
+ // It is very common for a class to implement an interface of the same name.
351
+ // For example, the Widget class may implement the IWidget interface. The "I" prefix
352
+ // avoids the need to invent a separate name such as "AbstractWidget" or "WidgetInterface".
353
+ // In TypeScript it is also common to declare interfaces that are implemented by primitive
354
+ // objects, here the "I" prefix also helps by avoiding spurious conflicts with classes
355
+ // by the same name.
356
+ format: ['PascalCase'],
357
+
358
+ custom: {
359
+ regex: '^_?I[A-Z]',
360
+ match: true
361
+ }
362
+ }
363
+ ])
364
+ ],
365
+
366
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
367
+ '@typescript-eslint/no-array-constructor': 'warn',
368
+
369
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
370
+ //
371
+ // RATIONALE: The "any" keyword disables static type checking, the main benefit of using TypeScript.
372
+ // This rule should be suppressed only in very special cases such as JSON.stringify()
373
+ // where the type really can be anything. Even if the type is flexible, another type
374
+ // may be more appropriate such as "unknown", "{}", or "Record<k,V>".
375
+ '@typescript-eslint/no-explicit-any': 'warn',
376
+
377
+ // RATIONALE: The #1 rule of promises is that every promise chain must be terminated by a catch()
378
+ // handler. Thus wherever a Promise arises, the code must either append a catch handler,
379
+ // or else return the object to a caller (who assumes this responsibility). Unterminated
380
+ // promise chains are a serious issue. Besides causing errors to be silently ignored,
381
+ // they can also cause a NodeJS process to terminate unexpectedly.
382
+ '@typescript-eslint/no-floating-promises': 'error',
383
+
384
+ // RATIONALE: Catches a common coding mistake.
385
+ '@typescript-eslint/no-for-in-array': 'error',
386
+
387
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
388
+ '@typescript-eslint/no-misused-new': 'error',
389
+
390
+ // RATIONALE: The "namespace" keyword is not recommended for organizing code because JavaScript lacks
391
+ // a "using" statement to traverse namespaces. Nested namespaces prevent certain bundler
392
+ // optimizations. If you are declaring loose functions/variables, it's better to make them
393
+ // static members of a class, since classes support property getters and their private
394
+ // members are accessible by unit tests. Also, the exercise of choosing a meaningful
395
+ // class name tends to produce more discoverable APIs: for example, search+replacing
396
+ // the function "reverse()" is likely to return many false matches, whereas if we always
397
+ // write "Text.reverse()" is more unique. For large scale organization, it's recommended
398
+ // to decompose your code into separate NPM packages, which ensures that component
399
+ // dependencies are tracked more conscientiously.
400
+ //
401
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
402
+ '@typescript-eslint/no-namespace': [
403
+ 'warn',
404
+ {
405
+ // Discourage "namespace" in .ts and .tsx files
406
+ allowDeclarations: false,
407
+
408
+ // Allow it in .d.ts files that describe legacy libraries
409
+ allowDefinitionFiles: false
410
+ }
411
+ ],
412
+
413
+ // RATIONALE: Parameter properties provide a shorthand such as "constructor(public title: string)"
414
+ // that avoids the effort of declaring "title" as a field. This TypeScript feature makes
415
+ // code easier to write, but arguably sacrifices readability: In the notes for
416
+ // "@typescript-eslint/member-ordering" we pointed out that fields are central to
417
+ // a class's design, so we wouldn't want to bury them in a constructor signature
418
+ // just to save some typing.
419
+ //
420
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
421
+ '@typescript-eslint/no-parameter-properties': 'warn',
422
+
423
+ // RATIONALE: When left in shipping code, unused variables often indicate a mistake. Dead code
424
+ // may impact performance.
425
+ //
426
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
427
+ '@typescript-eslint/no-unused-vars': [
428
+ 'warn',
429
+ {
430
+ vars: 'all',
431
+ // Unused function arguments often indicate a mistake in JavaScript code. However in TypeScript code,
432
+ // the compiler catches most of those mistakes, and unused arguments are fairly common for type signatures
433
+ // that are overriding a base class method or implementing an interface.
434
+ args: 'none'
435
+ }
436
+ ],
437
+
438
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
439
+ '@typescript-eslint/no-use-before-define': [
440
+ 'error',
441
+ {
442
+ // Base ESLint options
443
+
444
+ // We set functions=false so that functions can be ordered based on exported/local visibility
445
+ // similar to class methods. Also the base lint rule incorrectly flags a legitimate case like:
446
+ //
447
+ // function a(n: number): void {
448
+ // if (n > 0) {
449
+ // b(n-1); // lint error
450
+ // }
451
+ // }
452
+ // function b(n: number): void {
453
+ // if (n > 0) {
454
+ // a(n-1);
455
+ // }
456
+ // }
457
+ functions: false,
458
+ classes: true,
459
+ variables: true,
460
+
461
+ // TypeScript extensions
462
+
463
+ enums: true,
464
+ typedefs: true
465
+ // ignoreTypeReferences: true
466
+ }
467
+ ],
468
+
469
+ // TODO: This is a good rule for web browser apps, but it is commonly needed API for Node.js tools.
470
+ // '@typescript-eslint/no-var-requires': 'error',
471
+
472
+ // RATIONALE: The "module" keyword is deprecated except when describing legacy libraries.
473
+ //
474
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
475
+ '@typescript-eslint/prefer-namespace-keyword': 'warn',
476
+
477
+ // RATIONALE: We require explicit type annotations, even when the compiler could infer the type.
478
+ // This can be a controversial policy because it makes code more verbose. There are
479
+ // a couple downsides to type inference, however. First, it is not always available.
480
+ // For example, when reviewing a pull request or examining a Git history, we may see
481
+ // code like this:
482
+ //
483
+ // // What is the type of "y" here? The compiler knows, but the
484
+ // // person reading the code may have no clue.
485
+ // const x = f.();
486
+ // const y = x.z;
487
+ //
488
+ // Second, relying on implicit types also discourages design discussions and documentation.
489
+ // Consider this example:
490
+ //
491
+ // // Where's the documentation for "correlation" and "inventory"?
492
+ // // Where would you even write the TSDoc comments?
493
+ // function g() {
494
+ // return { correlation: 123, inventory: 'xyz' };
495
+ // }
496
+ //
497
+ // Implicit types make sense for small scale scenarios, where everyone is familiar with
498
+ // the project, and code should be "easy to write". Explicit types are preferable
499
+ // for large scale scenarios, where people regularly work with source files they've never
500
+ // seen before, and code should be "easy to read."
501
+ //
502
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
503
+ '@typescript-eslint/typedef': [
504
+ 'warn',
505
+ {
506
+ arrayDestructuring: false,
507
+ arrowParameter: false,
508
+ memberVariableDeclaration: true,
509
+ objectDestructuring: false,
510
+ parameter: true,
511
+ propertyDeclaration: true,
512
+
513
+ // This case is handled by our "@rushstack/typedef-var" rule
514
+ variableDeclaration: false,
515
+
516
+ // Normally we require type declarations for class members. However, that rule is relaxed
517
+ // for situations where we need to bind the "this" pointer for a callback. For example, consider
518
+ // this event handler for a React component:
519
+ //
520
+ // class MyComponent {
521
+ // public render(): React.ReactNode {
522
+ // return (
523
+ // <a href="#" onClick={this._onClick}> click me </a>
524
+ // );
525
+ // }
526
+ //
527
+ // // The assignment here avoids the need for "this._onClick.bind(this)"
528
+ // private _onClick = (event: React.MouseEvent<HTMLAnchorElement>): void => {
529
+ // console.log("Clicked! " + this.props.title);
530
+ // };
531
+ // }
532
+ //
533
+ // This coding style has limitations and should be used sparingly. For example, "_onClick"
534
+ // will not participate correctly in "virtual"/"override" inheritance.
535
+ //
536
+ // NOTE: This option affects both "memberVariableDeclaration" and "variableDeclaration" options.
537
+ variableDeclarationIgnoreFunction: true
538
+ }
539
+ ],
540
+
541
+ // RATIONALE: This rule warns if setters are defined without getters, which is probably a mistake.
542
+ 'accessor-pairs': 'error',
543
+
544
+ // RATIONALE: In TypeScript, if you write x["y"] instead of x.y, it disables type checking.
545
+ 'dot-notation': [
546
+ 'warn',
547
+ {
548
+ allowPattern: '^_'
549
+ }
550
+ ],
551
+
552
+ // RATIONALE: Catches code that is likely to be incorrect
553
+ eqeqeq: 'error',
554
+
555
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
556
+ 'for-direction': 'warn',
557
+
558
+ // RATIONALE: Catches a common coding mistake.
559
+ 'guard-for-in': 'error',
560
+
561
+ // RATIONALE: If you have more than 2,000 lines in a single source file, it's probably time
562
+ // to split up your code.
563
+ 'max-lines': ['warn', { max: 2000 }],
564
+
565
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
566
+ 'no-async-promise-executor': 'error',
567
+
568
+ // RATIONALE: "|" and "&" are relatively rare, and are more likely to appear as a mistake when
569
+ // someone meant "||" or "&&". (But nobody types the other operators by mistake.)
570
+ 'no-bitwise': [
571
+ 'warn',
572
+ {
573
+ allow: [
574
+ '^',
575
+ // "|",
576
+ // "&",
577
+ '<<',
578
+ '>>',
579
+ '>>>',
580
+ '^=',
581
+ // "|=",
582
+ //"&=",
583
+ '<<=',
584
+ '>>=',
585
+ '>>>=',
586
+ '~'
587
+ ]
588
+ }
589
+ ],
590
+
591
+ // RATIONALE: Deprecated language feature.
592
+ 'no-caller': 'error',
593
+
594
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
595
+ 'no-compare-neg-zero': 'error',
596
+
597
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
598
+ 'no-cond-assign': 'error',
599
+
600
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
601
+ 'no-constant-condition': 'warn',
602
+
603
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
604
+ 'no-control-regex': 'error',
605
+
606
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
607
+ 'no-debugger': 'warn',
608
+
609
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
610
+ 'no-delete-var': 'error',
611
+
612
+ // RATIONALE: Catches code that is likely to be incorrect
613
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
614
+ 'no-duplicate-case': 'error',
615
+
616
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
617
+ 'no-empty': 'warn',
618
+
619
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
620
+ 'no-empty-character-class': 'error',
621
+
622
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
623
+ 'no-empty-pattern': 'warn',
624
+
625
+ // RATIONALE: Eval is a security concern and a performance concern.
626
+ 'no-eval': 'warn',
627
+
628
+ // RATIONALE: Catches code that is likely to be incorrect
629
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
630
+ 'no-ex-assign': 'error',
631
+
632
+ // RATIONALE: System types are global and should not be tampered with in a scalable code base.
633
+ // If two different libraries (or two versions of the same library) both try to modify
634
+ // a type, only one of them can win. Polyfills are acceptable because they implement
635
+ // a standardized interoperable contract, but polyfills are generally coded in plain
636
+ // JavaScript.
637
+ 'no-extend-native': 'error',
638
+
639
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
640
+ 'no-extra-boolean-cast': 'warn',
641
+
642
+ 'no-extra-label': 'warn',
643
+
644
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
645
+ 'no-fallthrough': 'error',
646
+
647
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
648
+ 'no-func-assign': 'warn',
649
+
650
+ // RATIONALE: Catches a common coding mistake.
651
+ 'no-implied-eval': 'error',
652
+
653
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
654
+ 'no-invalid-regexp': 'error',
655
+
656
+ // RATIONALE: Catches a common coding mistake.
657
+ 'no-label-var': 'error',
658
+
659
+ // RATIONALE: Eliminates redundant code.
660
+ 'no-lone-blocks': 'warn',
661
+
662
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
663
+ 'no-misleading-character-class': 'error',
664
+
665
+ // RATIONALE: Catches a common coding mistake.
666
+ 'no-multi-str': 'error',
667
+
668
+ // RATIONALE: It's generally a bad practice to call "new Thing()" without assigning the result to
669
+ // a variable. Either it's part of an awkward expression like "(new Thing()).doSomething()",
670
+ // or else implies that the constructor is doing nontrivial computations, which is often
671
+ // a poor class design.
672
+ 'no-new': 'warn',
673
+
674
+ // RATIONALE: Obsolete language feature that is deprecated.
675
+ 'no-new-func': 'error',
676
+
677
+ // RATIONALE: Obsolete language feature that is deprecated.
678
+ 'no-new-object': 'error',
679
+
680
+ // RATIONALE: Obsolete notation.
681
+ 'no-new-wrappers': 'warn',
682
+
683
+ // RATIONALE: Catches code that is likely to be incorrect
684
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
685
+ 'no-octal': 'error',
686
+
687
+ // RATIONALE: Catches code that is likely to be incorrect
688
+ 'no-octal-escape': 'error',
689
+
690
+ // RATIONALE: Catches code that is likely to be incorrect
691
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
692
+ 'no-regex-spaces': 'error',
693
+
694
+ // RATIONALE: Catches a common coding mistake.
695
+ 'no-return-assign': 'error',
696
+
697
+ // RATIONALE: Security risk.
698
+ 'no-script-url': 'warn',
699
+
700
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
701
+ 'no-self-assign': 'error',
702
+
703
+ // RATIONALE: Catches a common coding mistake.
704
+ 'no-self-compare': 'error',
705
+
706
+ // RATIONALE: This avoids statements such as "while (a = next(), a && a.length);" that use
707
+ // commas to create compound expressions. In general code is more readable if each
708
+ // step is split onto a separate line. This also makes it easier to set breakpoints
709
+ // in the debugger.
710
+ 'no-sequences': 'error',
711
+
712
+ // RATIONALE: Catches code that is likely to be incorrect
713
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
714
+ 'no-shadow-restricted-names': 'error',
715
+
716
+ // RATIONALE: Obsolete language feature that is deprecated.
717
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
718
+ 'no-sparse-arrays': 'error',
719
+
720
+ // RATIONALE: Although in theory JavaScript allows any possible data type to be thrown as an exception,
721
+ // such flexibility adds pointless complexity, by requiring every catch block to test
722
+ // the type of the object that it receives. Whereas if catch blocks can always assume
723
+ // that their object implements the "Error" contract, then the code is simpler, and
724
+ // we generally get useful additional information like a call stack.
725
+ 'no-throw-literal': 'error',
726
+
727
+ // RATIONALE: Catches a common coding mistake.
728
+ 'no-unmodified-loop-condition': 'warn',
729
+
730
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
731
+ 'no-unsafe-finally': 'error',
732
+
733
+ // RATIONALE: Catches a common coding mistake.
734
+ 'no-unused-expressions': 'warn',
735
+
736
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
737
+ 'no-unused-labels': 'warn',
738
+
739
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
740
+ 'no-useless-catch': 'warn',
741
+
742
+ // RATIONALE: Avoids a potential performance problem.
743
+ 'no-useless-concat': 'warn',
744
+
745
+ // RATIONALE: The "var" keyword is deprecated because of its confusing "hoisting" behavior.
746
+ // Always use "let" or "const" instead.
747
+ //
748
+ // STANDARDIZED BY: @typescript-eslint\eslint-plugin\dist\configs\recommended.json
749
+ 'no-var': 'error',
750
+
751
+ // RATIONALE: Generally not needed in modern code.
752
+ 'no-void': 'error',
753
+
754
+ // RATIONALE: Obsolete language feature that is deprecated.
755
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
756
+ 'no-with': 'error',
757
+
758
+ // RATIONALE: Makes logic easier to understand, since constants always have a known value
759
+ // @typescript-eslint\eslint-plugin\dist\configs\eslint-recommended.js
760
+ 'prefer-const': 'warn',
761
+
762
+ // RATIONALE: Catches a common coding mistake where "resolve" and "reject" are confused.
763
+ 'promise/param-names': 'error',
764
+
765
+ // RATIONALE: Catches code that is likely to be incorrect
766
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
767
+ 'require-atomic-updates': 'error',
768
+
769
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
770
+ 'require-yield': 'warn',
771
+
772
+ // "Use strict" is redundant when using the TypeScript compiler.
773
+ strict: ['error', 'never'],
774
+
775
+ // RATIONALE: Catches code that is likely to be incorrect
776
+ // STANDARDIZED BY: eslint\conf\eslint-recommended.js
777
+ 'use-isnan': 'error'
778
+
779
+ // The "no-restricted-syntax" rule is a general purpose pattern matcher that we can use to experiment with
780
+ // new rules. If a rule works well, we should convert it to a proper rule so it gets its own name
781
+ // for suppressions and documentation.
782
+ // How it works: https://eslint.org/docs/rules/no-restricted-syntax
783
+ // AST visualizer: https://astexplorer.net/
784
+ // Debugger: http://estools.github.io/esquery/
785
+ //
786
+ // "no-restricted-syntax": [
787
+ // ],
788
+ }
789
+ },
790
+ {
791
+ // For unit tests, we can be a little bit less strict. The settings below revise the
792
+ // defaults specified above.
793
+ files: [
794
+ // Test files
795
+ '*.test.ts',
796
+ '*.test.tsx',
797
+ '*.spec.ts',
798
+ '*.spec.tsx',
799
+
800
+ // Facebook convention
801
+ '**/__mocks__/*.ts',
802
+ '**/__mocks__/*.tsx',
803
+ '**/__tests__/*.ts',
804
+ '**/__tests__/*.tsx',
805
+
806
+ // Microsoft convention
807
+ '**/test/*.ts',
808
+ '**/test/*.tsx'
809
+ ],
810
+ rules: {
811
+ // Unit tests sometimes use a standalone statement like "new Thing(123);" to test a constructor.
812
+ 'no-new': 'off',
813
+
814
+ // Jest's mocking API is designed in a way that produces compositional data types that often have
815
+ // no concise description. Since test code does not ship, and typically does not introduce new
816
+ // concepts or algorithms, the usual arguments for prioritizing readability over writability can be
817
+ // relaxed in this case.
818
+ '@rushstack/typedef-var': 'off',
819
+ '@typescript-eslint/typedef': [
820
+ 'warn',
821
+ {
822
+ arrayDestructuring: false,
823
+ arrowParameter: false,
824
+ memberVariableDeclaration: true,
825
+ objectDestructuring: false,
826
+ parameter: true,
827
+ propertyDeclaration: true,
828
+ variableDeclaration: false, // <--- special case for test files
829
+ variableDeclarationIgnoreFunction: true
830
+ }
831
+ ]
832
+ }
833
+ }
834
+ ]
835
+ };
836
+ }
837
+ exports.buildRules = buildRules;