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

Rule proposal: No promise inside if condition #365

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

Rule proposal: No promise inside if condition #365

joshuaavalon opened this issue Mar 19, 2019 · 4 comments
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@joshuaavalon
Copy link

This rule proposal was original created at eslint/eslint#11525. However, due to the limited information that can get from static analysis in JavaScript, this proposal is now created on TypeScript instead.

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.

@joshuaavalon joshuaavalon added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Mar 19, 2019
@ThomasdenH
Copy link
Contributor

I think this would be caught by strict-boolean-expressions from tslint, but I'm not sure whether their suggestion would be correct.

@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin and removed triage Waiting for maintainers to take a look labels Mar 20, 2019
@golopot
Copy link
Contributor

golopot commented Jun 15, 2019

Likewise function type and object type are also always truthy inside if condition. Is it better to roll all of these into one rule? This rule will be similar to the core rule no-constant-condition.

@joeytwiddle
Copy link

joeytwiddle commented Sep 10, 2019

This would be very helpful. (One of the few cases TypeScript doesn't save me!)

It would be good if it would also catch boolean operations, as well as if conditions.

// mistake
const results = isValid() && doWork();

// should be
const results = (await isValid()) && doWork();

// mistake
const shouldReject = !isValid();

// should be
const shouldReject = !(await isValid());

Also, in this comment princejef reminds us that we should not perform this test on "maybe" promises, of the type Promise<any> | undefined.

@bradzacher
Copy link
Member

I believe that this should be handled now by the rule no-misused-promises added in #612.

Additionally, we have strict-boolean-expressions which should block it as well.

There is also no-unnecessary-condition which should help check this #699.

I'm going to close this as it should be covered now.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

5 participants