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): [promise-function-async] Allow async function getter/setter #820

Merged

Conversation

scottohara
Copy link
Contributor

This PR allows for:

  • getters that return an async function
  • setters that take an async function as a parameter

Background

promise-function-async (correctly) does not fire for getters/setters on Promises.

(This is correct is because it is not possible to apply the async keyword to getter/setter functions).

So the following code is OK:

/* eslint @typescript-eslint/promise-function-async: "error" */

const someObject = {
  get asyncGetter() {
    return new Promise<void>();
  },
  set asyncGetter(p: Promise<void>) {
    return p;
  }
}

However (prior to this PR), the rule does fire for getters/setters on async functions.

So the following code...

/* eslint @typescript-eslint/promise-function-async: "error" */

const someObject = {
  get asyncGetterFunc() {
    return async () => new Promise<void>();
  },
  set asyncGetterFunc(p: () => Promise<void>) {
    return p;
  }
}

...gives the following errors:

error  Functions that return promises must be async  @typescript-eslint/promise-function-async
error  Functions that return promises must be async  @typescript-eslint/promise-function-async

It is not possible to resolve these errors, due the aforementioned reason (that async can't be applied to getter/setters).

Changes

This PR adds an additional check:

If the parent node is of type Property, don't report an error.

@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 #820 into master will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #820      +/-   ##
==========================================
+ Coverage   94.07%   94.08%   +<.01%     
==========================================
  Files         113      113              
  Lines        4914     4917       +3     
  Branches     1368     1370       +2     
==========================================
+ Hits         4623     4626       +3     
  Misses        165      165              
  Partials      126      126
Impacted Files Coverage Δ
.../eslint-plugin/src/rules/promise-function-async.ts 100% <100%> (ø) ⬆️

@bradzacher bradzacher added the bug Something isn't working label Aug 8, 2019
@bradzacher bradzacher merged commit cddfdca into typescript-eslint:master Aug 23, 2019
@scottohara scottohara deleted the promise-function-async 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