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

New rule: No promise inside if condition #11525

Closed
joshuaavalon opened this issue Mar 18, 2019 · 4 comments
Closed

New rule: No promise inside if condition #11525

joshuaavalon opened this issue Mar 18, 2019 · 4 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules

Comments

@joshuaavalon
Copy link

Please describe what the rule should do:

The rule should warn about putting promise in a if condition because it always resolve to true.

In most of the cases, we want to use the result of the promise instead.

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Enforces code style (layout)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

async function isValid() {
  return false;
}

if (isValid()) { // This is most likely an error because promise always resolves to true
  // Do work
}

Why should this rule be included in ESLint (instead of a plugin)?

This is a common mistake instead of code style. I think it should be included in ESLint to avoid this mistake.

Are you willing to submit a pull request to implement this rule?

No, I have do not any experience with ESLint code base.

@joshuaavalon joshuaavalon added feature This change adds a new feature to ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Mar 18, 2019
@platinumazure
Copy link
Member

Hi @joshuaavalon, thanks for the issue!

I like the idea, but I don't think this is something we could do reliably unless the function in question is in the same file (so we can statically determine that it is async). There would be no way to tell for imported functions, unless you're using Typescript or similar.

I'm on the fence about including a rule in core for the limited case. If the team doesn't accept this proposal, maybe an issue could be raised in eslint-plugin-promise to support this case.

@platinumazure platinumazure added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Mar 18, 2019
@joshuaavalon
Copy link
Author

@platinumazure How about async function instead of Promise? async function always return a Promise.

@platinumazure
Copy link
Member

@joshuaavalon Yes, if the function can be identified as async via static analysis it could be flagged easily if it is invoked in an if condition without an await. However, if the function is imported from another file, there's no way to tell.

@joshuaavalon
Copy link
Author

The limitation of JavaScript prevents this rule to be useful.

I am closing this issue and created a proposal at typescript-eslint/typescript-eslint#365.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 16, 2019
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Sep 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

2 participants