Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-floating-promises.mdx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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('...', () => {});
```

</TabItem>
</Tabs>

## When Not To Use It

This rule can be difficult to enable on large existing projects that set up many floating Promises.
Expand Down
23 changes: 22 additions & 1 deletionpackages/eslint-plugin/src/rules/no-floating-promises.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@ type Options = [
ignoreVoid?: boolean;
ignoreIIFE?: boolean;
allowForKnownSafePromises?: TypeOrValueSpecifier[];
allowForKnownSafeCalls?: TypeOrValueSpecifier[];
},
];

Expand DownExpand Up@@ -85,6 +86,7 @@ export default createRule<Options, MessageId>({
type: 'boolean',
},
allowForKnownSafePromises: readonlynessOptionsSchema.properties.allow,
allowForKnownSafeCalls: readonlynessOptionsSchema.properties.allow,
},
additionalProperties: false,
},
Expand All@@ -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-next-line @typescript-eslint/no-non-null-assertion
/* 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 {
Expand All@@ -118,6 +123,10 @@ export default createRule<Options, MessageId>({
expression = expression.expression;
}

if (isKnownSafePromiseReturn(expression)) {
return;
}

const { isUnhandled, nonFunctionHandler, promiseArray } =
isUnhandledPromise(checker, expression);

Expand DownExpand Up@@ -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
Expand Down
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -698,6 +698,27 @@ myTag\`abc\`;
{ allowForKnownSafePromises: [{ from: 'file', name: 'SafePromise' }] },
],
},
{
code: `
declare function it(...args: unknown[]): Promise<void>;

it('...', () => {});
`,
options: [
{
allowForKnownSafeCalls: [
{
from: 'file',
name: 'it',
// https://github.com/typescript-eslint/typescript-eslint/pull/9234/files#r1626465054
path: process.env.TYPESCRIPT_ESLINT_PROJECT_SERVICE
? 'file.ts'
: 'tests/fixtures/file.ts',
},
],
},
],
},
{
code: `
declare const myTag: (strings: TemplateStringsArray) => Promise<void>;
Expand DownExpand Up@@ -2181,5 +2202,71 @@ myTag\`abc\`;
options: [{ allowForKnownSafePromises: [{ from: 'file', name: 'Foo' }] }],
errors: [{ line: 4, messageId: 'floatingVoid' }],
},
{
code: `
declare function unsafe(...args: unknown[]): Promise<void>;

unsafe('...', () => {});
`,
errors: [{ line: 4, messageId: 'floatingVoid' }],
options: [
{
allowForKnownSafeCalls: [
{
from: 'file',
name: 'it',
// https://github.com/typescript-eslint/typescript-eslint/pull/9234/files#r1626465054
path: process.env.TYPESCRIPT_ESLINT_PROJECT_SERVICE
? 'file.ts'
: 'tests/fixtures/file.ts',
},
],
},
],
},
{
code: `
declare function it(...args: unknown[]): Promise<void>;

it('...', () => {}).then(() => {});
`,
errors: [{ line: 4, messageId: 'floatingVoid' }],
options: [
{
allowForKnownSafeCalls: [
{
from: 'file',
name: 'it',
// https://github.com/typescript-eslint/typescript-eslint/pull/9234/files#r1626465054
path: process.env.TYPESCRIPT_ESLINT_PROJECT_SERVICE
? 'file.ts'
: 'tests/fixtures/file.ts',
},
],
},
],
},
{
code: `
declare function it(...args: unknown[]): Promise<void>;

it('...', () => {}).finally(() => {});
`,
errors: [{ line: 4, messageId: 'floatingVoid' }],
options: [
{
allowForKnownSafeCalls: [
{
from: 'file',
name: 'it',
// https://github.com/typescript-eslint/typescript-eslint/pull/9234/files#r1626465054
path: process.env.TYPESCRIPT_ESLINT_PROJECT_SERVICE
? 'file.ts'
: 'tests/fixtures/file.ts',
},
],
},
],
},
],
});
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp