Skip to content

fix(eslint-plugin): [promise-function-async] use a different error message for functions with promise and non-promise types #10950

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

16 changes: 14 additions & 2 deletions packages/eslint-plugin/src/rules/promise-function-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type Options = [
checkMethodDeclarations?: boolean;
},
];
export type MessageIds = 'missingAsync';
export type MessageIds = 'missingAsync' | 'missingAsyncHybridReturn';

export default createRule<Options, MessageIds>({
name: 'promise-function-async',
Expand All @@ -37,6 +37,8 @@ export default createRule<Options, MessageIds>({
fixable: 'code',
messages: {
missingAsync: 'Functions that return promises must be async.',
missingAsyncHybridReturn:
'Functions that return promises must be async. Consider adding an explicit return type annotation if the function is intended to return a union of promise and non-promise types.',
},
schema: [
{
Expand Down Expand Up @@ -164,10 +166,20 @@ export default createRule<Options, MessageIds>({
),
)
) {
const isHybridReturnType = returnTypes.some(
type =>
type.isUnion() &&
!type.types.every(part =>
containsAllTypesByName(part, true, allAllowedPromiseNames),
),
);

context.report({
loc: getFunctionHeadLoc(node, context.sourceCode),
node,
messageId: 'missingAsync',
messageId: isHybridReturnType
? 'missingAsyncHybridReturn'
: 'missingAsync',
fix: fixer => {
if (
node.parent.type === AST_NODE_TYPES.MethodDefinition ||
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ function promiseInUnionWithoutExplicitReturnType(p: boolean) {
`,
errors: [
{
messageId,
messageId: 'missingAsyncHybridReturn',
},
],
output: `
Expand All @@ -836,6 +836,65 @@ async function promiseInUnionWithoutExplicitReturnType(p: boolean) {
},
{
code: `
function test1(): 'one' | Promise<'one'>;
function test1(a: number): Promise<number>;
function test1(a?: number) {
if (a) {
return Promise.resolve(a);
}

return Math.random() > 0.5 ? 'one' : Promise.resolve('one');
}
`,
errors: [
{
messageId: 'missingAsyncHybridReturn',
},
],
output: `
function test1(): 'one' | Promise<'one'>;
function test1(a: number): Promise<number>;
async function test1(a?: number) {
if (a) {
return Promise.resolve(a);
}

return Math.random() > 0.5 ? 'one' : Promise.resolve('one');
}
`,
},
{
code: `
class PromiseType {
s?: string;
}

function promiseInUnionWithoutExplicitReturnType(p: boolean) {
return p ? new PromiseType() : 5;
}
`,
errors: [
{
messageId: 'missingAsyncHybridReturn',
},
],
options: [
{
allowedPromiseNames: ['PromiseType'],
},
],
output: `
class PromiseType {
s?: string;
}

async function promiseInUnionWithoutExplicitReturnType(p: boolean) {
return p ? new PromiseType() : 5;
}
`,
},
{
code: `
function overloadingThatCanReturnPromise(): Promise<number>;
function overloadingThatCanReturnPromise(a: boolean): Promise<string>;
function overloadingThatCanReturnPromise(
Expand Down