Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-floating-promises] add checkThenables option#9263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
5242c41
dfd8c41
83b264e
8813a10
d66dbea
6078b07
dac381c
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -84,6 +84,55 @@ await Promise.all([1, 2, 3].map(async x => x + 1)); | ||
## Options | ||
### `checkThenables` | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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`. | ||
Other Thenables include TypeScript's built-in `PromiseLike` interface and any custom object that happens to have a `.then()`. | ||
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. | ||
This can be useful if your code works with older `Promise` polyfills instead of the native `Promise` class. | ||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
```ts option='{"checkThenables": true}' | ||
declare function createPromiseLike(): PromiseLike<string>; | ||
createPromiseLike(); | ||
interface MyThenable { | ||
then(onFulfilled: () => void, onRejected: () => void): MyThenable; | ||
} | ||
declare function createMyThenable(): MyThenable; | ||
createMyThenable(); | ||
``` | ||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
```ts option='{"checkThenables": true}' | ||
declare function createPromiseLike(): PromiseLike<string>; | ||
await createPromiseLike(); | ||
interface MyThenable { | ||
then(onFulfilled: () => void, onRejected: () => void): MyThenable; | ||
} | ||
declare function createMyThenable(): MyThenable; | ||
await createMyThenable(); | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
:::info | ||
This option is enabled by default in v7 but will be turned off by default in v8. | ||
::: | ||
### `ignoreVoid` | ||
This option, which is `true` by default, allows you to stop the rule reporting promises consumed with void operator. | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -733,6 +733,41 @@ promise().then(() => {}); | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
interface SafePromise<T> extends Promise<T> { | ||
brand: 'safe'; | ||
} | ||
declare const createSafePromise: () => SafePromise<string>; | ||
createSafePromise(); | ||
`, | ||
options: [ | ||
{ | ||
allowForKnownSafePromises: [{ from: 'file', name: 'SafePromise' }], | ||
checkThenables: true, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
declare const createPromise: () => PromiseLike<number>; | ||
createPromise(); | ||
`, | ||
options: [{ checkThenables: false }], | ||
}, | ||
{ | ||
code: ` | ||
interface MyThenable { | ||
then(onFulfilled: () => void, onRejected: () => void): MyThenable; | ||
} | ||
declare function createMyThenable(): MyThenable; | ||
createMyThenable(); | ||
`, | ||
options: [{ checkThenables: false }], | ||
}, | ||
], | ||
invalid: [ | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
@@ -2181,5 +2216,54 @@ myTag\`abc\`; | ||
options: [{ allowForKnownSafePromises: [{ from: 'file', name: 'Foo' }] }], | ||
errors: [{ line: 4, messageId: 'floatingVoid' }], | ||
}, | ||
{ | ||
code: ` | ||
declare const createPromise: () => PromiseLike<number>; | ||
createPromise(); | ||
`, | ||
errors: [{ line: 3, messageId: 'floatingVoid' }], | ||
options: [{ checkThenables: true }], | ||
}, | ||
{ | ||
code: ` | ||
interface MyThenable { | ||
then(onFulfilled: () => void, onRejected: () => void): MyThenable; | ||
} | ||
declare function createMyThenable(): MyThenable; | ||
createMyThenable(); | ||
`, | ||
errors: [{ line: 8, messageId: 'floatingVoid' }], | ||
options: [{ checkThenables: true }], | ||
}, | ||
{ | ||
code: ` | ||
declare const createPromise: () => Promise<number>; | ||
createPromise(); | ||
`, | ||
errors: [{ line: 3, messageId: 'floatingVoid' }], | ||
options: [{ checkThenables: false }], | ||
}, | ||
{ | ||
code: ` | ||
class MyPromise<T> extends Promise<T> {} | ||
declare const createMyPromise: () => MyPromise<number>; | ||
createMyPromise(); | ||
`, | ||
errors: [{ line: 4, messageId: 'floatingVoid' }], | ||
options: [{ checkThenables: false }], | ||
}, | ||
{ | ||
code: ` | ||
class MyPromise<T> extends Promise<T> { | ||
additional: string; | ||
} | ||
declare const createMyPromise: () => MyPromise<number>; | ||
createMyPromise(); | ||
`, | ||
errors: [{ line: 6, messageId: 'floatingVoid' }], | ||
options: [{ checkThenables: false }], | ||
}, | ||
], | ||
}); |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.