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

Conversation

scottohara
Copy link
Contributor

The @typescript-eslint/require-await rule exists to allow exceptions to the core ESLint require-await rule for async functions that return a promise (where adding an await to the return to suppress the require-await warnings would in turn trigger warnings for no-return-await).

The initial implementation of the @typescript-eslint/require-await rule allowed the following exceptions:

  • function expressions that return a promise
  • function declarations that return a promise
  • arrow function expressions with block bodies that return a promise

(the rule was only checking ReturnStatement nodes)

This PR adds support for:

  • arrow function expressions with concise bodies that return a promise

(single-line expressions that don't explicitly include a return statement)

Prior to this PR, the following code would fall through to the core ESLint require-await rule and trigger warnings:

// 1. Async arrow function with promise return
const numberOne = async (): Promise<number> => Promise.resolve(1);

// 2. Async arrow function with async function return
const getAsyncNumber = async function(x: number): Promise<number> {
  return Promise.resolve(x);
}

const numberOne = async (): Promise<number> => getAsyncNumber(1);

// 3. Async arrow function expression with no await
const numberOne = async (): Promise<number> => 1;

After this PR, only the last example should fall through to the core rule and triggers a warning:

// 3. Async arrow function expression with no await
const numberOne = async (): Promise<number> => 1;

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @scottohara!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint

@codecov
Copy link

codecov bot commented Aug 8, 2019

Codecov Report

Merging #826 into master will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #826      +/-   ##
==========================================
+ Coverage   94.07%   94.07%   +<.01%     
==========================================
  Files         113      113              
  Lines        4911     4914       +3     
  Branches     1368     1368              
==========================================
+ Hits         4620     4623       +3     
  Misses        165      165              
  Partials      126      126
Impacted Files Coverage Δ
packages/eslint-plugin/src/rules/require-await.ts 92.68% <100%> (+0.57%) ⬆️

@bradzacher bradzacher added the bug Something isn't working label Aug 9, 2019
bradzacher
bradzacher previously approved these changes Aug 21, 2019
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for this

packages/eslint-plugin/src/rules/require-await.ts Outdated Show resolved Hide resolved
@bradzacher bradzacher merged commit 29fddfd into typescript-eslint:master Aug 21, 2019
@scottohara scottohara deleted the require-await branch August 25, 2019 00:04
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants