@typescript-eslint/eslint-plugin 8.33.1-alpha.5 → 8.33.1
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.
- package/package.json +9 -10
- package/docs/rules/README.md +0 -57
- package/docs/rules/TEMPLATE.md +0 -36
- package/docs/rules/adjacent-overload-signatures.mdx +0 -105
- package/docs/rules/array-type.mdx +0 -126
- package/docs/rules/await-thenable.mdx +0 -184
- package/docs/rules/ban-ts-comment.mdx +0 -165
- package/docs/rules/ban-tslint-comment.mdx +0 -45
- package/docs/rules/ban-types.md +0 -26
- package/docs/rules/block-spacing.md +0 -15
- package/docs/rules/brace-style.md +0 -15
- package/docs/rules/camelcase.md +0 -15
- package/docs/rules/class-literal-property-style.mdx +0 -112
- package/docs/rules/class-methods-use-this.mdx +0 -135
- package/docs/rules/comma-dangle.md +0 -15
- package/docs/rules/comma-spacing.md +0 -15
- package/docs/rules/consistent-generic-constructors.mdx +0 -87
- package/docs/rules/consistent-indexed-object-style.mdx +0 -105
- package/docs/rules/consistent-return.mdx +0 -51
- package/docs/rules/consistent-type-assertions.mdx +0 -196
- package/docs/rules/consistent-type-definitions.mdx +0 -133
- package/docs/rules/consistent-type-exports.mdx +0 -97
- package/docs/rules/consistent-type-imports.mdx +0 -139
- package/docs/rules/default-param-last.mdx +0 -59
- package/docs/rules/dot-notation.mdx +0 -94
- package/docs/rules/explicit-function-return-type.mdx +0 -359
- package/docs/rules/explicit-member-accessibility.mdx +0 -353
- package/docs/rules/explicit-module-boundary-types.mdx +0 -287
- package/docs/rules/func-call-spacing.md +0 -15
- package/docs/rules/indent.md +0 -15
- package/docs/rules/init-declarations.mdx +0 -12
- package/docs/rules/key-spacing.md +0 -15
- package/docs/rules/keyword-spacing.md +0 -15
- package/docs/rules/lines-around-comment.md +0 -15
- package/docs/rules/lines-between-class-members.md +0 -15
- package/docs/rules/max-params.mdx +0 -54
- package/docs/rules/member-delimiter-style.md +0 -15
- package/docs/rules/member-ordering.mdx +0 -1483
- package/docs/rules/method-signature-style.mdx +0 -124
- package/docs/rules/naming-convention.mdx +0 -755
- package/docs/rules/no-array-constructor.mdx +0 -34
- package/docs/rules/no-array-delete.mdx +0 -44
- package/docs/rules/no-base-to-string.mdx +0 -115
- package/docs/rules/no-confusing-non-null-assertion.mdx +0 -75
- package/docs/rules/no-confusing-void-expression.mdx +0 -148
- package/docs/rules/no-deprecated.mdx +0 -119
- package/docs/rules/no-dupe-class-members.mdx +0 -16
- package/docs/rules/no-duplicate-enum-values.mdx +0 -66
- package/docs/rules/no-duplicate-imports.mdx +0 -17
- package/docs/rules/no-duplicate-type-constituents.mdx +0 -89
- package/docs/rules/no-dynamic-delete.mdx +0 -64
- package/docs/rules/no-empty-function.mdx +0 -94
- package/docs/rules/no-empty-interface.mdx +0 -75
- package/docs/rules/no-empty-object-type.mdx +0 -150
- package/docs/rules/no-explicit-any.mdx +0 -177
- package/docs/rules/no-extra-non-null-assertion.mdx +0 -60
- package/docs/rules/no-extra-parens.md +0 -15
- package/docs/rules/no-extra-semi.md +0 -15
- package/docs/rules/no-extraneous-class.mdx +0 -329
- package/docs/rules/no-floating-promises.mdx +0 -282
- package/docs/rules/no-for-in-array.mdx +0 -67
- package/docs/rules/no-implied-eval.mdx +0 -106
- package/docs/rules/no-import-type-side-effects.mdx +0 -80
- package/docs/rules/no-inferrable-types.mdx +0 -113
- package/docs/rules/no-invalid-this.mdx +0 -16
- package/docs/rules/no-invalid-void-type.mdx +0 -119
- package/docs/rules/no-loop-func.mdx +0 -12
- package/docs/rules/no-loss-of-precision.mdx +0 -17
- package/docs/rules/no-magic-numbers.mdx +0 -131
- package/docs/rules/no-meaningless-void-operator.mdx +0 -61
- package/docs/rules/no-misused-new.mdx +0 -53
- package/docs/rules/no-misused-promises.mdx +0 -314
- package/docs/rules/no-misused-spread.mdx +0 -132
- package/docs/rules/no-mixed-enums.mdx +0 -96
- package/docs/rules/no-namespace.mdx +0 -157
- package/docs/rules/no-non-null-asserted-nullish-coalescing.mdx +0 -60
- package/docs/rules/no-non-null-asserted-optional-chain.mdx +0 -46
- package/docs/rules/no-non-null-assertion.mdx +0 -48
- package/docs/rules/no-parameter-properties.mdx +0 -16
- package/docs/rules/no-redeclare.mdx +0 -79
- package/docs/rules/no-redundant-type-constituents.mdx +0 -102
- package/docs/rules/no-require-imports.mdx +0 -114
- package/docs/rules/no-restricted-imports.mdx +0 -84
- package/docs/rules/no-restricted-types.mdx +0 -70
- package/docs/rules/no-shadow.mdx +0 -143
- package/docs/rules/no-this-alias.mdx +0 -124
- package/docs/rules/no-type-alias.mdx +0 -626
- package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +0 -165
- package/docs/rules/no-unnecessary-condition.mdx +0 -293
- package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +0 -42
- package/docs/rules/no-unnecessary-qualifier.mdx +0 -57
- package/docs/rules/no-unnecessary-template-expression.mdx +0 -108
- package/docs/rules/no-unnecessary-type-arguments.mdx +0 -85
- package/docs/rules/no-unnecessary-type-assertion.mdx +0 -97
- package/docs/rules/no-unnecessary-type-constraint.mdx +0 -61
- package/docs/rules/no-unnecessary-type-conversion.mdx +0 -79
- package/docs/rules/no-unnecessary-type-parameters.mdx +0 -255
- package/docs/rules/no-unsafe-argument.mdx +0 -98
- package/docs/rules/no-unsafe-assignment.mdx +0 -101
- package/docs/rules/no-unsafe-call.mdx +0 -120
- package/docs/rules/no-unsafe-declaration-merging.mdx +0 -65
- package/docs/rules/no-unsafe-enum-comparison.mdx +0 -98
- package/docs/rules/no-unsafe-function-type.mdx +0 -65
- package/docs/rules/no-unsafe-member-access.mdx +0 -81
- package/docs/rules/no-unsafe-return.mdx +0 -126
- package/docs/rules/no-unsafe-type-assertion.mdx +0 -63
- package/docs/rules/no-unsafe-unary-minus.mdx +0 -60
- package/docs/rules/no-unused-expressions.mdx +0 -52
- package/docs/rules/no-unused-vars.mdx +0 -136
- package/docs/rules/no-use-before-define.mdx +0 -98
- package/docs/rules/no-useless-constructor.mdx +0 -21
- package/docs/rules/no-useless-empty-export.mdx +0 -53
- package/docs/rules/no-useless-template-literals.mdx +0 -9
- package/docs/rules/no-var-requires.mdx +0 -77
- package/docs/rules/no-wrapper-object-types.mdx +0 -75
- package/docs/rules/non-nullable-type-assertion-style.mdx +0 -47
- package/docs/rules/object-curly-spacing.md +0 -15
- package/docs/rules/only-throw-error.mdx +0 -150
- package/docs/rules/padding-line-between-statements.md +0 -15
- package/docs/rules/parameter-properties.mdx +0 -522
- package/docs/rules/prefer-as-const.mdx +0 -51
- package/docs/rules/prefer-destructuring.mdx +0 -101
- package/docs/rules/prefer-enum-initializers.mdx +0 -68
- package/docs/rules/prefer-find.mdx +0 -45
- package/docs/rules/prefer-for-of.mdx +0 -50
- package/docs/rules/prefer-function-type.mdx +0 -98
- package/docs/rules/prefer-includes.mdx +0 -81
- package/docs/rules/prefer-literal-enum-member.mdx +0 -111
- package/docs/rules/prefer-namespace-keyword.mdx +0 -51
- package/docs/rules/prefer-nullish-coalescing.mdx +0 -349
- package/docs/rules/prefer-optional-chain.mdx +0 -304
- package/docs/rules/prefer-promise-reject-errors.mdx +0 -78
- package/docs/rules/prefer-readonly-parameter-types.mdx +0 -408
- package/docs/rules/prefer-readonly.mdx +0 -111
- package/docs/rules/prefer-reduce-type-parameter.mdx +0 -66
- package/docs/rules/prefer-regexp-exec.mdx +0 -52
- package/docs/rules/prefer-return-this-type.mdx +0 -93
- package/docs/rules/prefer-string-starts-ends-with.mdx +0 -84
- package/docs/rules/prefer-ts-expect-error.mdx +0 -86
- package/docs/rules/promise-function-async.mdx +0 -143
- package/docs/rules/quotes.md +0 -15
- package/docs/rules/related-getter-setter-pairs.mdx +0 -61
- package/docs/rules/require-array-sort-compare.mdx +0 -89
- package/docs/rules/require-await.mdx +0 -53
- package/docs/rules/restrict-plus-operands.mdx +0 -245
- package/docs/rules/restrict-template-expressions.mdx +0 -167
- package/docs/rules/return-await.mdx +0 -339
- package/docs/rules/semi.md +0 -15
- package/docs/rules/sort-type-constituents.mdx +0 -209
- package/docs/rules/sort-type-union-intersection-members.mdx +0 -16
- package/docs/rules/space-before-blocks.md +0 -15
- package/docs/rules/space-before-function-paren.md +0 -15
- package/docs/rules/space-infix-ops.md +0 -15
- package/docs/rules/strict-boolean-expressions.mdx +0 -184
- package/docs/rules/switch-exhaustiveness-check.mdx +0 -280
- package/docs/rules/triple-slash-reference.mdx +0 -129
- package/docs/rules/type-annotation-spacing.md +0 -15
- package/docs/rules/typedef.mdx +0 -350
- package/docs/rules/unbound-method.mdx +0 -114
- package/docs/rules/unified-signatures.mdx +0 -132
- package/docs/rules/use-unknown-in-catch-callback-variable.mdx +0 -97
@@ -1,60 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow extra non-null assertions.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-extra-non-null-assertion** for documentation.
|
11
|
-
|
12
|
-
The `!` non-null assertion operator in TypeScript is used to assert that a value's type does not include `null` or `undefined`.
|
13
|
-
Using the operator any more than once on a single value does nothing.
|
14
|
-
|
15
|
-
## Examples
|
16
|
-
|
17
|
-
<Tabs>
|
18
|
-
<TabItem value="❌ Incorrect">
|
19
|
-
|
20
|
-
```ts
|
21
|
-
const foo: { bar: number } | null = null;
|
22
|
-
const bar = foo!!!.bar;
|
23
|
-
```
|
24
|
-
|
25
|
-
```ts
|
26
|
-
function foo(bar: number | undefined) {
|
27
|
-
const bar: number = bar!!!;
|
28
|
-
}
|
29
|
-
```
|
30
|
-
|
31
|
-
```ts
|
32
|
-
function foo(bar?: { n: number }) {
|
33
|
-
return bar!?.n;
|
34
|
-
}
|
35
|
-
```
|
36
|
-
|
37
|
-
</TabItem>
|
38
|
-
<TabItem value="✅ Correct">
|
39
|
-
|
40
|
-
```ts
|
41
|
-
const foo: { bar: number } | null = null;
|
42
|
-
const bar = foo!.bar;
|
43
|
-
```
|
44
|
-
|
45
|
-
```ts
|
46
|
-
function foo(bar: number | undefined) {
|
47
|
-
const bar: number = bar!;
|
48
|
-
}
|
49
|
-
```
|
50
|
-
|
51
|
-
```ts
|
52
|
-
function foo(bar?: { n: number }) {
|
53
|
-
return bar?.n;
|
54
|
-
}
|
55
|
-
```
|
56
|
-
|
57
|
-
</TabItem>
|
58
|
-
</Tabs>
|
59
|
-
|
60
|
-
{/* Intentionally Omitted: When Not To Use It */}
|
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
displayed_sidebar: rulesSidebar
|
3
|
-
---
|
4
|
-
|
5
|
-
:::danger Deprecated
|
6
|
-
|
7
|
-
This rule has been moved to the [ESLint stylistic plugin](https://eslint.style).
|
8
|
-
See [#8072](https://github.com/typescript-eslint/typescript-eslint/issues/8072) and [#8074](https://github.com/typescript-eslint/typescript-eslint/issues/8074) for more information.
|
9
|
-
|
10
|
-
:::
|
11
|
-
|
12
|
-
<!-- This doc file has been left on purpose to help direct people to the stylistic plugin.
|
13
|
-
|
14
|
-
Note that there is no actual way to get to this page in the normal navigation,
|
15
|
-
so end-users will only be able to get to this page from the search bar. -->
|
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
displayed_sidebar: rulesSidebar
|
3
|
-
---
|
4
|
-
|
5
|
-
:::danger Deprecated
|
6
|
-
|
7
|
-
This rule has been moved to the [ESLint stylistic plugin](https://eslint.style).
|
8
|
-
See [#8072](https://github.com/typescript-eslint/typescript-eslint/issues/8072) and [#8074](https://github.com/typescript-eslint/typescript-eslint/issues/8074) for more information.
|
9
|
-
|
10
|
-
:::
|
11
|
-
|
12
|
-
<!-- This doc file has been left on purpose to help direct people to the stylistic plugin.
|
13
|
-
|
14
|
-
Note that there is no actual way to get to this page in the normal navigation,
|
15
|
-
so end-users will only be able to get to this page from the search bar. -->
|
@@ -1,329 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow classes used as namespaces.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-extraneous-class** for documentation.
|
11
|
-
|
12
|
-
This rule reports when a class has no non-static members, such as for a class used exclusively as a static namespace.
|
13
|
-
|
14
|
-
Users who come from a [OOP](https://en.wikipedia.org/wiki/Object-oriented_programming) paradigm may wrap their utility functions in an extra class, instead of putting them at the top level of an ECMAScript module.
|
15
|
-
Doing so is generally unnecessary in JavaScript and TypeScript projects.
|
16
|
-
|
17
|
-
- Wrapper classes add extra cognitive complexity to code without adding any structural improvements
|
18
|
-
- Whatever would be put on them, such as utility functions, are already organized by virtue of being in a module.
|
19
|
-
- As an alternative, you can `import * as ...` the module to get all of them in a single object.
|
20
|
-
- IDEs can't provide as good suggestions for static class or namespace imported properties when you start typing property names
|
21
|
-
- It's more difficult to statically analyze code for unused variables, etc. when they're all on the class (see: [Finding dead code (and dead types) in TypeScript](https://effectivetypescript.com/2020/10/20/tsprune)).
|
22
|
-
|
23
|
-
This rule also reports classes that have only a constructor and no fields.
|
24
|
-
Those classes can generally be replaced with a standalone function.
|
25
|
-
|
26
|
-
## Examples
|
27
|
-
|
28
|
-
<Tabs>
|
29
|
-
<TabItem value="❌ Incorrect">
|
30
|
-
|
31
|
-
```ts
|
32
|
-
class StaticConstants {
|
33
|
-
static readonly version = 42;
|
34
|
-
|
35
|
-
static isProduction() {
|
36
|
-
return process.env.NODE_ENV === 'production';
|
37
|
-
}
|
38
|
-
}
|
39
|
-
|
40
|
-
class HelloWorldLogger {
|
41
|
-
constructor() {
|
42
|
-
console.log('Hello, world!');
|
43
|
-
}
|
44
|
-
}
|
45
|
-
|
46
|
-
abstract class Foo {}
|
47
|
-
```
|
48
|
-
|
49
|
-
</TabItem>
|
50
|
-
<TabItem value="✅ Correct">
|
51
|
-
|
52
|
-
```ts
|
53
|
-
export const version = 42;
|
54
|
-
|
55
|
-
export function isProduction() {
|
56
|
-
return process.env.NODE_ENV === 'production';
|
57
|
-
}
|
58
|
-
|
59
|
-
function logHelloWorld() {
|
60
|
-
console.log('Hello, world!');
|
61
|
-
}
|
62
|
-
|
63
|
-
abstract class Foo {
|
64
|
-
abstract prop: string;
|
65
|
-
}
|
66
|
-
```
|
67
|
-
|
68
|
-
</TabItem>
|
69
|
-
</Tabs>
|
70
|
-
|
71
|
-
## Alternatives
|
72
|
-
|
73
|
-
### Individual Exports (Recommended)
|
74
|
-
|
75
|
-
Instead of using a static utility class we recommend you individually export the utilities from your module.
|
76
|
-
|
77
|
-
<Tabs>
|
78
|
-
<TabItem value="❌ Incorrect">
|
79
|
-
|
80
|
-
```ts
|
81
|
-
export class Utilities {
|
82
|
-
static util1() {
|
83
|
-
return Utilities.util3();
|
84
|
-
}
|
85
|
-
|
86
|
-
static util2() {
|
87
|
-
/* ... */
|
88
|
-
}
|
89
|
-
|
90
|
-
static util3() {
|
91
|
-
/* ... */
|
92
|
-
}
|
93
|
-
}
|
94
|
-
```
|
95
|
-
|
96
|
-
</TabItem>
|
97
|
-
<TabItem value="✅ Correct">
|
98
|
-
|
99
|
-
```ts
|
100
|
-
export function util1() {
|
101
|
-
return util3();
|
102
|
-
}
|
103
|
-
|
104
|
-
export function util2() {
|
105
|
-
/* ... */
|
106
|
-
}
|
107
|
-
|
108
|
-
export function util3() {
|
109
|
-
/* ... */
|
110
|
-
}
|
111
|
-
```
|
112
|
-
|
113
|
-
</TabItem>
|
114
|
-
</Tabs>
|
115
|
-
|
116
|
-
### Namespace Imports (Not Recommended)
|
117
|
-
|
118
|
-
If you strongly prefer to have all constructs from a module available as properties of a single object, you can `import * as` the module.
|
119
|
-
This is known as a "namespace import".
|
120
|
-
Namespace imports are sometimes preferable because they keep all properties nested and don't need to be changed as you start or stop using various properties from the module.
|
121
|
-
|
122
|
-
However, namespace imports are impacted by these downsides:
|
123
|
-
|
124
|
-
- They also don't play as well with tree shaking in modern bundlers
|
125
|
-
- They require a name prefix before each property's usage
|
126
|
-
|
127
|
-
<Tabs>
|
128
|
-
<TabItem value="❌ Incorrect">
|
129
|
-
|
130
|
-
```ts
|
131
|
-
// utilities.ts
|
132
|
-
export class Utilities {
|
133
|
-
static sayHello() {
|
134
|
-
console.log('Hello, world!');
|
135
|
-
}
|
136
|
-
}
|
137
|
-
|
138
|
-
// consumers.ts
|
139
|
-
import { Utilities } from './utilities';
|
140
|
-
|
141
|
-
Utilities.sayHello();
|
142
|
-
```
|
143
|
-
|
144
|
-
</TabItem>
|
145
|
-
<TabItem value="⚠️ Namespace Imports">
|
146
|
-
|
147
|
-
```ts
|
148
|
-
// utilities.ts
|
149
|
-
export function sayHello() {
|
150
|
-
console.log('Hello, world!');
|
151
|
-
}
|
152
|
-
|
153
|
-
// consumers.ts
|
154
|
-
import * as utilities from './utilities';
|
155
|
-
|
156
|
-
utilities.sayHello();
|
157
|
-
```
|
158
|
-
|
159
|
-
</TabItem>
|
160
|
-
<TabItem value="✅ Standalone Imports">
|
161
|
-
|
162
|
-
```ts
|
163
|
-
// utilities.ts
|
164
|
-
export function sayHello() {
|
165
|
-
console.log('Hello, world!');
|
166
|
-
}
|
167
|
-
|
168
|
-
// consumers.ts
|
169
|
-
import { sayHello } from './utilities';
|
170
|
-
|
171
|
-
sayHello();
|
172
|
-
```
|
173
|
-
|
174
|
-
</TabItem>
|
175
|
-
</Tabs>
|
176
|
-
|
177
|
-
### Notes on Mutating Variables
|
178
|
-
|
179
|
-
One case you need to be careful of is exporting mutable variables.
|
180
|
-
While class properties can be mutated externally, exported variables are always constant.
|
181
|
-
This means that importers can only ever read the first value they are assigned and cannot write to the variables.
|
182
|
-
|
183
|
-
Needing to write to an exported variable is very rare and is generally considered a code smell.
|
184
|
-
If you do need it you can accomplish it using getter and setter functions:
|
185
|
-
|
186
|
-
<Tabs>
|
187
|
-
<TabItem value="❌ Incorrect">
|
188
|
-
|
189
|
-
```ts
|
190
|
-
export class Utilities {
|
191
|
-
static mutableCount = 1;
|
192
|
-
|
193
|
-
static incrementCount() {
|
194
|
-
Utilities.mutableCount += 1;
|
195
|
-
}
|
196
|
-
}
|
197
|
-
```
|
198
|
-
|
199
|
-
</TabItem>
|
200
|
-
<TabItem value="✅ Correct">
|
201
|
-
|
202
|
-
```ts
|
203
|
-
let mutableCount = 1;
|
204
|
-
|
205
|
-
export function getMutableCount() {
|
206
|
-
return mutableField;
|
207
|
-
}
|
208
|
-
|
209
|
-
export function incrementCount() {
|
210
|
-
mutableField += 1;
|
211
|
-
}
|
212
|
-
```
|
213
|
-
|
214
|
-
</TabItem>
|
215
|
-
</Tabs>
|
216
|
-
|
217
|
-
## Options
|
218
|
-
|
219
|
-
This rule normally bans classes that are empty (have no constructor or fields).
|
220
|
-
The rule's options each add an exemption for a specific type of class.
|
221
|
-
|
222
|
-
### `allowConstructorOnly`
|
223
|
-
|
224
|
-
{/* insert option description */}
|
225
|
-
|
226
|
-
<Tabs>
|
227
|
-
<TabItem value="❌ Incorrect">
|
228
|
-
|
229
|
-
```ts option='{ "allowConstructorOnly": true }'
|
230
|
-
class NoFields {}
|
231
|
-
```
|
232
|
-
|
233
|
-
</TabItem>
|
234
|
-
<TabItem value="✅ Correct">
|
235
|
-
|
236
|
-
```ts option='{ "allowConstructorOnly": true }'
|
237
|
-
class NoFields {
|
238
|
-
constructor() {
|
239
|
-
console.log('Hello, world!');
|
240
|
-
}
|
241
|
-
}
|
242
|
-
```
|
243
|
-
|
244
|
-
</TabItem>
|
245
|
-
</Tabs>
|
246
|
-
|
247
|
-
### `allowEmpty`
|
248
|
-
|
249
|
-
{/* insert option description */}
|
250
|
-
|
251
|
-
<Tabs>
|
252
|
-
<TabItem value="❌ Incorrect">
|
253
|
-
|
254
|
-
```ts option='{ "allowEmpty": true }'
|
255
|
-
class NoFields {
|
256
|
-
constructor() {
|
257
|
-
console.log('Hello, world!');
|
258
|
-
}
|
259
|
-
}
|
260
|
-
```
|
261
|
-
|
262
|
-
</TabItem>
|
263
|
-
<TabItem value="✅ Correct">
|
264
|
-
|
265
|
-
```ts option='{ "allowEmpty": true }'
|
266
|
-
class NoFields {}
|
267
|
-
```
|
268
|
-
|
269
|
-
</TabItem>
|
270
|
-
</Tabs>
|
271
|
-
|
272
|
-
### `allowStaticOnly`
|
273
|
-
|
274
|
-
{/* insert option description */}
|
275
|
-
|
276
|
-
:::caution
|
277
|
-
We strongly recommend against the `allowStaticOnly` exemption.
|
278
|
-
It works against this rule's primary purpose of discouraging classes used only for static members.
|
279
|
-
:::
|
280
|
-
|
281
|
-
<Tabs>
|
282
|
-
<TabItem value="❌ Incorrect">
|
283
|
-
|
284
|
-
```ts option='{ "allowStaticOnly": true }'
|
285
|
-
class EmptyClass {}
|
286
|
-
```
|
287
|
-
|
288
|
-
</TabItem>
|
289
|
-
<TabItem value="✅ Correct">
|
290
|
-
|
291
|
-
```ts option='{ "allowStaticOnly": true }'
|
292
|
-
class NotEmptyClass {
|
293
|
-
static version = 42;
|
294
|
-
}
|
295
|
-
```
|
296
|
-
|
297
|
-
</TabItem>
|
298
|
-
</Tabs>
|
299
|
-
|
300
|
-
### `allowWithDecorator`
|
301
|
-
|
302
|
-
{/* insert option description */}
|
303
|
-
|
304
|
-
<Tabs>
|
305
|
-
<TabItem value="❌ Incorrect">
|
306
|
-
|
307
|
-
```ts option='{ "allowWithDecorator": true }'
|
308
|
-
class Constants {
|
309
|
-
static readonly version = 42;
|
310
|
-
}
|
311
|
-
```
|
312
|
-
|
313
|
-
</TabItem>
|
314
|
-
<TabItem value="✅ Correct">
|
315
|
-
|
316
|
-
```ts option='{ "allowWithDecorator": true }'
|
317
|
-
@logOnRead()
|
318
|
-
class Constants {
|
319
|
-
static readonly version = 42;
|
320
|
-
}
|
321
|
-
```
|
322
|
-
|
323
|
-
</TabItem>
|
324
|
-
</Tabs>
|
325
|
-
|
326
|
-
## When Not To Use It
|
327
|
-
|
328
|
-
If your project was set up before modern class and namespace practices, and you don't have the time to switch over, you might not be practically able to use this rule.
|
329
|
-
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
@@ -1,282 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Require Promise-like statements to be handled appropriately.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-floating-promises** for documentation.
|
11
|
-
|
12
|
-
A "floating" Promise is one that is created without any code set up to handle any errors it might throw.
|
13
|
-
Floating Promises can cause several issues, such as improperly sequenced operations, ignored Promise rejections, and more.
|
14
|
-
|
15
|
-
This rule will report Promise-valued statements that are not treated in one of the following ways:
|
16
|
-
|
17
|
-
- Calling its `.then()` with two arguments
|
18
|
-
- Calling its `.catch()` with one argument
|
19
|
-
- `await`ing it
|
20
|
-
- `return`ing it
|
21
|
-
- [`void`ing it](#ignorevoid)
|
22
|
-
|
23
|
-
This rule also reports when an Array containing Promises is created and not properly handled. The main way to resolve this is by using one of the Promise concurrency methods to create a single Promise, then handling that according to the procedure above. These methods include:
|
24
|
-
|
25
|
-
- `Promise.all()`
|
26
|
-
- `Promise.allSettled()`
|
27
|
-
- `Promise.any()`
|
28
|
-
- `Promise.race()`
|
29
|
-
|
30
|
-
:::tip
|
31
|
-
`no-floating-promises` only detects apparently unhandled Promise _statements_.
|
32
|
-
See [`no-misused-promises`](./no-misused-promises.mdx) for detecting code that provides Promises to _logical_ locations such as if statements.
|
33
|
-
|
34
|
-
See [_Using promises (error handling) on MDN_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#error_handling) for a detailed writeup on Promise error-handling.
|
35
|
-
:::
|
36
|
-
|
37
|
-
## Examples
|
38
|
-
|
39
|
-
<Tabs>
|
40
|
-
<TabItem value="❌ Incorrect">
|
41
|
-
|
42
|
-
```ts
|
43
|
-
const promise = new Promise((resolve, reject) => resolve('value'));
|
44
|
-
promise;
|
45
|
-
|
46
|
-
async function returnsPromise() {
|
47
|
-
return 'value';
|
48
|
-
}
|
49
|
-
returnsPromise().then(() => {});
|
50
|
-
|
51
|
-
Promise.reject('value').catch();
|
52
|
-
|
53
|
-
Promise.reject('value').finally();
|
54
|
-
|
55
|
-
[1, 2, 3].map(async x => x + 1);
|
56
|
-
```
|
57
|
-
|
58
|
-
</TabItem>
|
59
|
-
<TabItem value="✅ Correct">
|
60
|
-
|
61
|
-
```ts
|
62
|
-
const promise = new Promise((resolve, reject) => resolve('value'));
|
63
|
-
await promise;
|
64
|
-
|
65
|
-
async function returnsPromise() {
|
66
|
-
return 'value';
|
67
|
-
}
|
68
|
-
|
69
|
-
void returnsPromise();
|
70
|
-
|
71
|
-
returnsPromise().then(
|
72
|
-
() => {},
|
73
|
-
() => {},
|
74
|
-
);
|
75
|
-
|
76
|
-
Promise.reject('value').catch(() => {});
|
77
|
-
|
78
|
-
await Promise.reject('value').finally(() => {});
|
79
|
-
|
80
|
-
await Promise.all([1, 2, 3].map(async x => x + 1));
|
81
|
-
```
|
82
|
-
|
83
|
-
</TabItem>
|
84
|
-
</Tabs>
|
85
|
-
|
86
|
-
## Options
|
87
|
-
|
88
|
-
### `checkThenables`
|
89
|
-
|
90
|
-
{/* insert option description */}
|
91
|
-
|
92
|
-
A ["Thenable"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#thenables) value is an object which has a `then` method, such as a `Promise`.
|
93
|
-
Other Thenables include TypeScript's built-in `PromiseLike` interface and any custom object that happens to have a `.then()`.
|
94
|
-
|
95
|
-
The `checkThenables` option triggers `no-floating-promises` to also consider all values that satisfy the Thenable shape (a `.then()` method that takes two callback parameters), not just Promises.
|
96
|
-
This can be useful if your code works with older `Promise` polyfills instead of the native `Promise` class.
|
97
|
-
|
98
|
-
<Tabs>
|
99
|
-
<TabItem value="❌ Incorrect">
|
100
|
-
|
101
|
-
```ts option='{"checkThenables": true}'
|
102
|
-
declare function createPromiseLike(): PromiseLike<string>;
|
103
|
-
|
104
|
-
createPromiseLike();
|
105
|
-
|
106
|
-
interface MyThenable {
|
107
|
-
then(onFulfilled: () => void, onRejected: () => void): MyThenable;
|
108
|
-
}
|
109
|
-
|
110
|
-
declare function createMyThenable(): MyThenable;
|
111
|
-
|
112
|
-
createMyThenable();
|
113
|
-
```
|
114
|
-
|
115
|
-
</TabItem>
|
116
|
-
<TabItem value="✅ Correct">
|
117
|
-
|
118
|
-
```ts option='{"checkThenables": true}'
|
119
|
-
declare function createPromiseLike(): PromiseLike<string>;
|
120
|
-
|
121
|
-
await createPromiseLike();
|
122
|
-
|
123
|
-
interface MyThenable {
|
124
|
-
then(onFulfilled: () => void, onRejected: () => void): MyThenable;
|
125
|
-
}
|
126
|
-
|
127
|
-
declare function createMyThenable(): MyThenable;
|
128
|
-
|
129
|
-
await createMyThenable();
|
130
|
-
```
|
131
|
-
|
132
|
-
</TabItem>
|
133
|
-
</Tabs>
|
134
|
-
|
135
|
-
### `ignoreVoid`
|
136
|
-
|
137
|
-
{/* insert option description */}
|
138
|
-
|
139
|
-
Placing the [`void` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) in front of a Promise can be a convenient way to explicitly mark that Promise as intentionally not awaited.
|
140
|
-
|
141
|
-
:::warning
|
142
|
-
Voiding a Promise doesn't handle it or change the runtime behavior.
|
143
|
-
The outcome is just ignored, like disabling the rule with an [ESLint disable comment](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1).
|
144
|
-
Such Promise rejections will still be unhandled.
|
145
|
-
:::
|
146
|
-
|
147
|
-
Examples of **correct** code for this rule with `{ ignoreVoid: true }`:
|
148
|
-
|
149
|
-
```ts option='{ "ignoreVoid": true }' showPlaygroundButton
|
150
|
-
async function returnsPromise() {
|
151
|
-
return 'value';
|
152
|
-
}
|
153
|
-
void returnsPromise();
|
154
|
-
|
155
|
-
void Promise.reject('value');
|
156
|
-
```
|
157
|
-
|
158
|
-
When this option is set to `true`, if you are using `no-void`, you should turn on the [`allowAsStatement`](https://eslint.org/docs/rules/no-void#allowasstatement) option.
|
159
|
-
|
160
|
-
### `ignoreIIFE`
|
161
|
-
|
162
|
-
{/* insert option description */}
|
163
|
-
|
164
|
-
Examples of **correct** code for this rule with `{ ignoreIIFE: true }`:
|
165
|
-
|
166
|
-
{/* prettier-ignore */}
|
167
|
-
```ts option='{ "ignoreIIFE": true }' showPlaygroundButton
|
168
|
-
await (async function () {
|
169
|
-
await res(1);
|
170
|
-
})();
|
171
|
-
|
172
|
-
(async function () {
|
173
|
-
await res(1);
|
174
|
-
})();
|
175
|
-
```
|
176
|
-
|
177
|
-
### `allowForKnownSafePromises`
|
178
|
-
|
179
|
-
{/* insert option description */}
|
180
|
-
|
181
|
-
For example, you may need to do this in the case of libraries whose APIs return Promises whose rejections are safely handled by the library.
|
182
|
-
|
183
|
-
This option takes the shared [`TypeOrValueSpecifier` format](/packages/type-utils/type-or-value-specifier).
|
184
|
-
|
185
|
-
Examples of code for this rule with:
|
186
|
-
|
187
|
-
```json
|
188
|
-
{
|
189
|
-
"allowForKnownSafePromises": [
|
190
|
-
{ "from": "file", "name": "SafePromise" },
|
191
|
-
{ "from": "lib", "name": "PromiseLike" },
|
192
|
-
{ "from": "package", "name": "Bar", "package": "bar-lib" }
|
193
|
-
]
|
194
|
-
}
|
195
|
-
```
|
196
|
-
|
197
|
-
<Tabs>
|
198
|
-
<TabItem value="❌ Incorrect">
|
199
|
-
|
200
|
-
```ts option='{"allowForKnownSafePromises":[{"from":"file","name":"SafePromise"},{"from":"lib","name":"PromiseLike"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
|
201
|
-
let promise: Promise<number> = Promise.resolve(2);
|
202
|
-
promise;
|
203
|
-
|
204
|
-
function returnsPromise(): Promise<number> {
|
205
|
-
return Promise.resolve(42);
|
206
|
-
}
|
207
|
-
|
208
|
-
returnsPromise();
|
209
|
-
```
|
210
|
-
|
211
|
-
</TabItem>
|
212
|
-
<TabItem value="✅ Correct">
|
213
|
-
|
214
|
-
```ts option='{"allowForKnownSafePromises":[{"from":"file","name":"SafePromise"},{"from":"lib","name":"PromiseLike"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
|
215
|
-
// promises can be marked as safe by using branded types
|
216
|
-
type SafePromise = Promise<number> & { __linterBrands?: string };
|
217
|
-
|
218
|
-
let promise: SafePromise = Promise.resolve(2);
|
219
|
-
promise;
|
220
|
-
|
221
|
-
function returnsSafePromise(): SafePromise {
|
222
|
-
return Promise.resolve(42);
|
223
|
-
}
|
224
|
-
|
225
|
-
returnsSafePromise();
|
226
|
-
```
|
227
|
-
|
228
|
-
</TabItem>
|
229
|
-
</Tabs>
|
230
|
-
|
231
|
-
### `allowForKnownSafeCalls`
|
232
|
-
|
233
|
-
{/* insert option description */}
|
234
|
-
|
235
|
-
For example, you may need to do this in the case of libraries whose APIs may be called without handling the resultant Promises.
|
236
|
-
|
237
|
-
This option takes the shared [`TypeOrValueSpecifier` format](/packages/type-utils/type-or-value-specifier).
|
238
|
-
|
239
|
-
Examples of code for this rule with:
|
240
|
-
|
241
|
-
```json
|
242
|
-
{
|
243
|
-
"allowForKnownSafeCalls": [
|
244
|
-
{ "from": "file", "name": "safe", "path": "input.ts" }
|
245
|
-
]
|
246
|
-
}
|
247
|
-
```
|
248
|
-
|
249
|
-
<Tabs>
|
250
|
-
<TabItem value="❌ Incorrect">
|
251
|
-
|
252
|
-
```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}'
|
253
|
-
declare function unsafe(...args: unknown[]): Promise<void>;
|
254
|
-
|
255
|
-
unsafe('...', () => {});
|
256
|
-
```
|
257
|
-
|
258
|
-
</TabItem>
|
259
|
-
<TabItem value="✅ Correct">
|
260
|
-
|
261
|
-
```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}' skipValidation
|
262
|
-
declare function safe(...args: unknown[]): Promise<void>;
|
263
|
-
|
264
|
-
safe('...', () => {});
|
265
|
-
```
|
266
|
-
|
267
|
-
</TabItem>
|
268
|
-
</Tabs>
|
269
|
-
|
270
|
-
## When Not To Use It
|
271
|
-
|
272
|
-
This rule can be difficult to enable on large existing projects that set up many floating Promises.
|
273
|
-
Alternately, if you're not worried about crashes from floating or misused Promises -such as if you have global unhandled Promise handlers registered- then in some cases it may be safe to not use this rule.
|
274
|
-
You might consider using `void`s and/or [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
275
|
-
|
276
|
-
## Related To
|
277
|
-
|
278
|
-
- [`no-misused-promises`](./no-misused-promises.mdx)
|
279
|
-
|
280
|
-
## Further Reading
|
281
|
-
|
282
|
-
- ["Using Promises" MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). Note especially the sections on [Promise rejection events](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#promise_rejection_events) and [Composition](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#composition).
|