You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(eslint-plugin): [no-misused-promises] check subtype methods against heritage type methods (typescript-eslint#8765)
* Implement checking of subtypes for no-misused-promises (currently works for subtypes implementing
interfaces, and interfaces extending classes, but not classes extending classes)
* Finished working logic for no-misused-promises checksVoidReturn.subtypes
* Cleanup
* Refactor and improve (better more concise approach), and add more test cases
* Handle type aliases and add type alias test cases
* Added expected {{ baseTypeName }} values to test cases
* Added test cases for handling class expressions, and fixed code to handle them
* Fix no-misused-promises schema snapshot to account for new subtypes option
* Updated copy (base type => heritage type) and added documentation for new subtypes option
* Update copy in test cases (baseTypeName => heritageTypeName)
* Refactoring
* Copy change again: subtypes => heritageTypes
* Fix location of heritageTypes examples in no-misused-promises mdx doc
* Refactor out getHeritageTypes function
* Update no-misused-promises doc to specify that it checks named methods
* Add test cases for ignoring unnamed methods (signatures) and add explanatory comments to the code that ignores unnamed methods
* Add combination test cases which add coverage for when the member is not found in the heritage type
* Rename subtypes => heritageTypes in region comments
* Adjust no-misused-promises doc to be more explicit about heritageTypes
ignoring signatures / only checking named methods
* Remove `#region`s from no-misused-promises test file
* Update (use jest to generate) no-misused-promises rules snapshot
* refactor: map(...).flat() => flatMap(...)
* docs: restructure no-misused-promises doc
- checksVoidReturn sub-options each get their own subsection
- option descriptions go under Options subsection instead of Examples
* chore: remove my unit-test-labeling comments
* rename `heritageTypes` suboption to `inheritedMethods`
* Style nitty nit - not worrying about strict-boolean-expressions,
condensing undefined and 0 check into one
---------
Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
Copy file name to clipboardExpand all lines: packages/eslint-plugin/docs/rules/no-misused-promises.mdx
+98-53Lines changed: 98 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -37,10 +37,89 @@ If you don't want to check conditionals, you can configure the rule with `"check
37
37
38
38
Doing so prevents the rule from looking at code like `if (somePromise)`.
39
39
40
-
Examples of code for this rule with `checksConditionals: true`:
40
+
### `checksVoidReturn`
41
+
42
+
Likewise, if you don't want to check functions that return promises where a void return is
43
+
expected, your configuration will look like this:
44
+
45
+
```json
46
+
{
47
+
"@typescript-eslint/no-misused-promises": [
48
+
"error",
49
+
{
50
+
"checksVoidReturn": false
51
+
}
52
+
]
53
+
}
54
+
```
55
+
56
+
You can disable selective parts of the `checksVoidReturn` option by providing an object that disables specific checks. For example, if you don't mind that passing a `() => Promise<void>` to a `() => void` parameter or JSX attribute can lead to a floating unhandled Promise:
57
+
58
+
```json
59
+
{
60
+
"@typescript-eslint/no-misused-promises": [
61
+
"error",
62
+
{
63
+
"checksVoidReturn": {
64
+
"arguments": false,
65
+
"attributes": false
66
+
}
67
+
}
68
+
]
69
+
}
70
+
```
71
+
72
+
The following sub-options are supported:
73
+
74
+
#### `arguments`
75
+
76
+
Disables checking an asynchronous function passed as argument where the parameter type expects a function that returns `void`.
77
+
78
+
#### `attributes`
79
+
80
+
Disables checking an asynchronous function passed as a JSX attribute expected to be a function that returns `void`.
81
+
82
+
#### `inheritedMethods`
83
+
84
+
Disables checking an asynchronous method in a type that extends or implements another type expecting that method to return `void`.
85
+
86
+
:::note
87
+
For now, `no-misused-promises` only checks _named_ methods against extended/implemented types: that is, call/construct/index signatures are ignored. Call signatures are not required in TypeScript to be consistent with one another, and construct signatures cannot be `async` in the first place. Index signature checking may be implemented in the future.
88
+
:::
89
+
90
+
#### `properties`
91
+
92
+
Disables checking an asynchronous function passed as an object property expected to be a function that returns `void`.
93
+
94
+
#### `returns`
95
+
96
+
Disables checking an asynchronous function returned in a function whose return type is a function that returns `void`.
97
+
98
+
#### `variables`
99
+
100
+
Disables checking an asynchronous function used as a variable whose return type is a function that returns `void`.
101
+
102
+
### `checksSpreads`
103
+
104
+
If you don't want to check object spreads, you can add this configuration:
105
+
106
+
```json
107
+
{
108
+
"@typescript-eslint/no-misused-promises": [
109
+
"error",
110
+
{
111
+
"checksSpreads": false
112
+
}
113
+
]
114
+
}
115
+
```
41
116
42
117
## Examples
43
118
119
+
### `checksConditionals`
120
+
121
+
Examples of code for this rule with `checksConditionals: true`:
122
+
44
123
<Tabs>
45
124
<TabItemvalue="❌ Incorrect">
46
125
@@ -81,45 +160,6 @@ while (await promise) {
81
160
82
161
### `checksVoidReturn`
83
162
84
-
Likewise, if you don't want to check functions that return promises where a void return is
85
-
expected, your configuration will look like this:
86
-
87
-
```json
88
-
{
89
-
"@typescript-eslint/no-misused-promises": [
90
-
"error",
91
-
{
92
-
"checksVoidReturn": false
93
-
}
94
-
]
95
-
}
96
-
```
97
-
98
-
You can disable selective parts of the `checksVoidReturn` option by providing an object that disables specific checks.
99
-
The following options are supported:
100
-
101
-
-`arguments`: Disables checking an asynchronous function passed as argument where the parameter type expects a function that returns `void`
102
-
-`attributes`: Disables checking an asynchronous function passed as a JSX attribute expected to be a function that returns `void`
103
-
-`properties`: Disables checking an asynchronous function passed as an object property expected to be a function that returns `void`
104
-
-`returns`: Disables checking an asynchronous function returned in a function whose return type is a function that returns `void`
105
-
-`variables`: Disables checking an asynchronous function used as a variable whose return type is a function that returns `void`
106
-
107
-
For example, if you don't mind that passing a `() => Promise<void>` to a `() => void` parameter or JSX attribute can lead to a floating unhandled Promise:
108
-
109
-
```json
110
-
{
111
-
"@typescript-eslint/no-misused-promises": [
112
-
"error",
113
-
{
114
-
"checksVoidReturn": {
115
-
"arguments": false,
116
-
"attributes": false
117
-
}
118
-
}
119
-
]
120
-
}
121
-
```
122
-
123
163
Examples of code for this rule with `checksVoidReturn: true`:
0 commit comments