Skip to content

Commit

Permalink
Refrain from renaming the type-util, instead add an optional fourth p…
Browse files Browse the repository at this point in the history
…aram.
  • Loading branch information
ericbf committed Feb 10, 2023
1 parent ea34d23 commit be9531d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
Expand Up @@ -46,6 +46,7 @@ async function functionReturnsPromise() {
return Promise.resolve('value');
}

// An explicit return type that is not Promise means this function cannot be made async, so it is ignored by the rule
function functionReturnsUnionWithPromiseExplicitly(
p: boolean,
): string | Promise<string> {
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/promise-function-async.ts
Expand Up @@ -111,12 +111,12 @@ export default util.createRule<Options, MessageIds>({
const returnType = checker.getReturnTypeOfSignature(signatures[0]);

if (
!util.containsTypesByName(
!util.containsAllTypesByName(
returnType,
allowAny!,
allAllowedPromiseNames,
// Only if a return type is explicitly declared must it match all
Boolean(node.returnType),
// If no return type is explicitly set, we check if any parts of the return type match a Promise (instead of requiring all to match).
node.returnType == null,
)
) {
// Return type is not a promise
Expand Down
Expand Up @@ -7,14 +7,14 @@ import { isTypeFlagSet } from './typeFlagUtils';
* @param type Type being checked by name.
* @param allowAny Whether to consider `any` and `unknown` to match.
* @param allowedNames Symbol names checking on the type.
* @param mustMatchAll Whether all parts have to match, as opposed to any parts matching.
* @param matchAnyInstead Whether to instead just check if any parts match, rather than all parts.
* @returns Whether the type is, extends, or contains the allowed names (or all matches the allowed names, if mustMatchAll is true).
*/
export function containsTypesByName(
export function containsAllTypesByName(
type: ts.Type,
allowAny: boolean,
allowedNames: Set<string>,
mustMatchAll: boolean,
matchAnyInstead = false,
): boolean {
if (isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return !allowAny;
Expand All @@ -30,20 +30,20 @@ export function containsTypesByName(
}

const predicate = (t: ts.Type): boolean =>
containsTypesByName(t, allowAny, allowedNames, mustMatchAll);
containsAllTypesByName(t, allowAny, allowedNames, matchAnyInstead);

if (isUnionOrIntersectionType(type)) {
return mustMatchAll
? type.types.every(predicate)
: type.types.some(predicate);
return matchAnyInstead
? type.types.some(predicate)
: type.types.every(predicate);
}

const bases = type.getBaseTypes();

return (
typeof bases !== 'undefined' &&
(mustMatchAll
? bases.length > 0 && bases.every(predicate)
: bases.some(predicate))
(matchAnyInstead
? bases.some(predicate)
: bases.length > 0 && bases.every(predicate))
);
}
2 changes: 1 addition & 1 deletion packages/type-utils/src/index.ts
@@ -1,4 +1,4 @@
export * from './containsTypesByName';
export * from './containsAllTypesByName';
export * from './getConstrainedTypeAtLocation';
export * from './getContextualType';
export * from './getDeclaration';
Expand Down

0 comments on commit be9531d

Please sign in to comment.