-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: No promise inside if condition #365
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
Comments
I think this would be caught by |
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 |
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 |
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:
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.
The text was updated successfully, but these errors were encountered: