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 'allowForKnownSafeCalls' option#9234
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
1d3d648
9c15479
e0fcd88
54d380c
c6da9d9
7eb4c3d
ed8b7d9
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 |
---|---|---|
@@ -176,6 +176,44 @@ returnsSafePromise(); | ||
</TabItem> | ||
</Tabs> | ||
### `allowForKnownSafeCalls` | ||
This option allows marking specific functions as "safe" to be called to create floating Promises. | ||
For example, you may need to do this in the case of libraries whose APIs may be called without handling the resultant Promises. | ||
This option takes the same array format as [`allowForKnownSafePromises`](#allowForKnownSafePromises). | ||
Examples of code for this rule with: | ||
```json | ||
{ | ||
"allowForKnownSafeCalls": [ | ||
{ "from": "file", "name": "safe", "path": "input.ts" } | ||
] | ||
} | ||
``` | ||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}' | ||
declare function unsafe(...args: unknown[]): Promise<void>; | ||
unsafe('...', () => {}); | ||
``` | ||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}' skipValidation | ||
declare function safe(...args: unknown[]): Promise<void>; | ||
safe('...', () => {}); | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
## When Not To Use It | ||
This rule can be difficult to enable on large existing projects that set up many floating Promises. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -19,6 +19,7 @@ type Options = [ | ||
ignoreVoid?: boolean; | ||
ignoreIIFE?: boolean; | ||
allowForKnownSafePromises?: TypeOrValueSpecifier[]; | ||
allowForKnownSafeCalls?: TypeOrValueSpecifier[]; | ||
}, | ||
]; | ||
@@ -85,6 +86,7 @@ export default createRule<Options, MessageId>({ | ||
type: 'boolean', | ||
}, | ||
allowForKnownSafePromises: readonlynessOptionsSchema.properties.allow, | ||
allowForKnownSafeCalls: readonlynessOptionsSchema.properties.allow, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
@@ -96,15 +98,18 @@ export default createRule<Options, MessageId>({ | ||
ignoreVoid: true, | ||
ignoreIIFE: false, | ||
allowForKnownSafePromises: readonlynessOptionsDefaults.allow, | ||
allowForKnownSafeCalls: readonlynessOptionsDefaults.allow, | ||
}, | ||
], | ||
create(context, [options]) { | ||
const services = getParserServices(context); | ||
const checker = services.program.getTypeChecker(); | ||
// TODO: #5439 | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
const allowForKnownSafePromises = options.allowForKnownSafePromises!; | ||
const allowForKnownSafeCalls = options.allowForKnownSafeCalls!; | ||
/* eslint-enable @typescript-eslint/no-non-null-assertion */ | ||
return { | ||
ExpressionStatement(node): void { | ||
@@ -118,6 +123,10 @@ export default createRule<Options, MessageId>({ | ||
expression = expression.expression; | ||
} | ||
if (isKnownSafePromiseReturn(expression)) { | ||
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return; | ||
} | ||
const { isUnhandled, nonFunctionHandler, promiseArray } = | ||
isUnhandledPromise(checker, expression); | ||
@@ -197,6 +206,18 @@ export default createRule<Options, MessageId>({ | ||
}, | ||
}; | ||
function isKnownSafePromiseReturn(node: TSESTree.Node): boolean { | ||
if (node.type !== AST_NODE_TYPES.CallExpression) { | ||
return false; | ||
} | ||
const type = services.getTypeAtLocation(node.callee); | ||
return allowForKnownSafeCalls.some(allowedType => | ||
typeMatchesSpecifier(type, allowedType, services.program), | ||
); | ||
} | ||
function isHigherPrecedenceThanUnary(node: ts.Node): boolean { | ||
const operator = ts.isBinaryExpression(node) | ||
? node.operatorToken.kind | ||
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.
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.