Skip to content
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

fix(eslint-plugin): [require-await] Allow concise arrow function bodies #826

Merged
merged 4 commits into from Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions packages/eslint-plugin/src/rules/require-await.ts
Expand Up @@ -63,6 +63,15 @@ export default util.createRule<Options, MessageIds>({

case AST_NODE_TYPES.ArrowFunctionExpression:
rules.ArrowFunctionExpression(node);

// If body type is not BlockStatment, we need to check the return type here
if (!(node.body.type === 'BlockStatement')) {
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
const expression = parserServices.esTreeNodeToTSNodeMap.get(
node.body,
);
scopeInfo.returnsPromise = isThenableType(expression);
}

break;
}
}
Expand Down Expand Up @@ -101,6 +110,18 @@ export default util.createRule<Options, MessageIds>({
}
}

/**
* Checks if the node returns a thenable type
*
* @param {ASTNode} node - The node to check
* @returns {boolean}
*/
function isThenableType(node: ts.Node) {
const type = checker.getTypeAtLocation(node);

return tsutils.isThenableType(checker, node, type);
}

return {
'FunctionDeclaration[async = true]': enterFunction,
'FunctionExpression[async = true]': enterFunction,
Expand All @@ -121,10 +142,7 @@ export default util.createRule<Options, MessageIds>({
return;
}

const type = checker.getTypeAtLocation(expression);
if (tsutils.isThenableType(checker, expression, type)) {
scopeInfo.returnsPromise = true;
}
scopeInfo.returnsPromise = isThenableType(expression);
},

AwaitExpression: rules.AwaitExpression as TSESLint.RuleFunction<
Expand Down
42 changes: 40 additions & 2 deletions packages/eslint-plugin/tests/rules/require-await.test.ts
Expand Up @@ -41,9 +41,15 @@ ruleTester.run('require-await', rule, {
}`,
},
{
// Non-async arrow function expression
// Non-async arrow function expression (concise-body)
code: `const numberOne = (): number => 1;`,
},
{
// Non-async arrow function expression (block-body)
code: `const numberOne = (): number => {
return 1;
};`,
},
{
// Async function declaration with await
code: `async function numberOne(): Promise<number> {
Expand All @@ -57,9 +63,15 @@ ruleTester.run('require-await', rule, {
}`,
},
{
// Async arrow function expression with await
// Async arrow function expression with await (concise-body)
code: `const numberOne = async (): Promise<number> => await 1;`,
},
{
// Async arrow function expression with await (block-body)
code: `const numberOne = async (): Promise<number> => {
return await 1;
};`,
},
{
// Async function declaration with promise return
code: `async function numberOne(): Promise<number> {
Expand All @@ -72,6 +84,16 @@ ruleTester.run('require-await', rule, {
return Promise.resolve(1);
}`,
},
{
// Async arrow function with promise return (concise-body)
code: `const numberOne = async (): Promise<number> => Promise.resolve(1);`,
},
{
// Async arrow function with promise return (block-body)
code: `const numberOne = async (): Promise<number> => {
return Promise.resolve(1);
};`,
},
{
// Async function declaration with async function return
code: `async function numberOne(): Promise<number> {
Expand All @@ -90,6 +112,22 @@ ruleTester.run('require-await', rule, {
return Promise.resolve(x);
}`,
},
{
// Async arrow function with async function return (concise-body)
code: `const numberOne = async (): Promise<number> => getAsyncNumber(1);
const getAsyncNumber = async function(x: number): Promise<number> {
return Promise.resolve(x);
}`,
},
{
// Async arrow function with async function return (block-body)
code: `const numberOne = async (): Promise<number> => {
return getAsyncNumber(1);
};
const getAsyncNumber = async function(x: number): Promise<number> {
return Promise.resolve(x);
}`,
},
],

invalid: [
Expand Down