Skip to content

feat(eslint-plugin): [await-thenable] check Promise.all #10282

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions packages/eslint-plugin/src/rules/await-thenable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import type * as ts from 'typescript';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';

import {
Expand All @@ -8,6 +10,7 @@
getParserServices,
isAwaitKeyword,
isTypeAnyType,
isTypeReferenceType,
isTypeUnknownType,
nullThrows,
NullThrowsReasons,
Expand All @@ -19,6 +22,7 @@
| 'awaitUsingOfNonAsyncDisposable'
| 'convertToOrdinaryFor'
| 'forAwaitOfNonAsyncIterable'
| 'notPromises'
| 'removeAwait';

export default createRule<[], MessageId>({
Expand All @@ -38,6 +42,7 @@
convertToOrdinaryFor: 'Convert to an ordinary `for...of` loop.',
forAwaitOfNonAsyncIterable:
'Unexpected `for await...of` of a value that is not async iterable.',
notPromises: 'Unexpected non-promise input to Promise.{methodName}.',
removeAwait: 'Remove unnecessary `await`.',
},
schema: [],
Expand Down Expand Up @@ -167,6 +172,53 @@
}
}
},

// Check for e.g. `Promise.all(nonPromises)`
CallExpression(node): void {
if (
node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.type === AST_NODE_TYPES.Identifier &&
node.callee.object.name === 'Promise' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type verification part probably needs to be reviewed, ...

[Bug] Good question. isBuiltinSymbolLike is our standard utility for this. You'll want to use that, and add tests to make sure things coincidentally named Array or Promise don't erroneously trigger this rule.

Copy link
Member

@kirkwaiblinger kirkwaiblinger Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node.callee.property.type === AST_NODE_TYPES.Identifier &&
(node.callee.property.name === 'all' ||
node.callee.property.name === 'allSettled' ||
node.callee.property.name === 'race')
) {
// Get the type of the thing being used in the method call.
const argument = node.arguments[0];
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
Comment on lines +188 to +189
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Style] 💡 hint:

Suggested change
const argument = node.arguments[0];
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const argument = node.arguments.at(0);

if (argument === undefined) {
return;

Check warning on line 191 in packages/eslint-plugin/src/rules/await-thenable.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin/src/rules/await-thenable.ts#L191

Added line #L191 was not covered by tests
}

const tsNode = services.esTreeNodeToTSNodeMap.get(argument);
const type = checker.getTypeAtLocation(tsNode);
if (!isTypeReferenceType(type) || type.typeArguments === undefined) {
return;

Check warning on line 197 in packages/eslint-plugin/src/rules/await-thenable.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin/src/rules/await-thenable.ts#L197

Added line #L197 was not covered by tests
}

const typeArg = type.typeArguments[0];
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (typeArg === undefined) {
return;

Check warning on line 203 in packages/eslint-plugin/src/rules/await-thenable.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin/src/rules/await-thenable.ts#L203

Added line #L203 was not covered by tests
}

const typeName = getTypeNameSimple(type);
const typeArgName = getTypeNameSimple(typeArg);

if (typeName === 'Array' && typeArgName !== 'Promise') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context.report({
loc: node.loc,
messageId: 'notPromises',
});
}
}
},
};
},
});

/** This is a simplified version of the `getTypeName` utility function. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] Why use this the existing getTypeName, then?

This might be a moot point if you end up using isBuiltinSymbolLike or one of the functions around it.

function getTypeNameSimple(type: ts.Type): string | undefined {
return type.getSymbol()?.escapedName as string | undefined;
}
35 changes: 35 additions & 0 deletions packages/eslint-plugin/tests/rules/await-thenable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ async function iterateUsing(arr: Array<AsyncDisposable>) {
}
`,
},
{
code: `
declare const promises: Array<Promise<void>>;
await Promise.all(promises);
await Promise.allSettled(promises);
await Promise.race(promises);
`,
},
{
code: `
declare const promises: Iterable<Promise<void>>;
await Promise.all(promises);
await Promise.allSettled(promises);
await Promise.race(promises);
`,
},
],

invalid: [
Expand Down Expand Up @@ -639,5 +655,24 @@ async function foo() {
},
],
},
{
code: `
declare const booleans: boolean[];
await Promise.all(booleans);
await Promise.allSettled(booleans);
await Promise.race(booleans);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`,
errors: [
{
messageId: 'notPromises',
},
{
messageId: 'notPromises',
},
{
messageId: 'notPromises',
},
],
},
],
});
Loading