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: no double-negation in boolean context #557

Closed
michaelficarra opened this issue Jan 25, 2014 · 6 comments
Closed

rule: no double-negation in boolean context #557

michaelficarra opened this issue Jan 25, 2014 · 6 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@michaelficarra
Copy link
Member

Double negation (!!<expr>) is often used for casting to bool, but in a boolean context this is unnecessary. Boolean contexts are anywhere a value will be coerced to boolean.

  • if(<bool>) ...
  • <bool> ? ... : ...
  • do ... while(<bool>)
  • while(<bool>) ...
  • for(...; <bool>; ...) ...
  • Boolean(<bool>)
  • new Boolean(<bool>)
  • !<bool>

A similar proposal was recently submitted to jshint: jshint/jshint#1487.

@btmills
Copy link
Member

btmills commented Feb 2, 2014

Working on this.

The rule name no-double-negation doesn't seem right because double negation can be okay in some contexts. What about no-redundant-boolean-cast?

@michaelficarra
Copy link
Member Author

That name sounds good to me.

@nzakas
Copy link
Member

nzakas commented Feb 2, 2014

I'd go no-extra-Boolean-cast

We tend to use "extra" in a lot of rules

@btmills
Copy link
Member

btmills commented Feb 3, 2014

Couple of on-the-fly decisions I've made and hope you can confirm:

  1. We'd also want to add <bool> && <bool> and <bool> || <bool> to this rule?
  2. This would be considered an error and on by default?

@michaelficarra
Copy link
Member Author

@btmills: First one: nope, those values are both observable. Remember, && and || do not coerce the result to bool. Second: yes, I'd say so.

@btmills
Copy link
Member

btmills commented Feb 3, 2014

@michaelficarra Thanks, I forgot to consider the result of the expression.

Should have a PR here in a few minutes.

btmills added a commit to btmills/eslint that referenced this issue Feb 3, 2014
Disallows double-negation Boolean casts in contexts where the result is
already coerced to a Boolean. Proposed in eslint#557 by @michaelficarra.
@nzakas nzakas closed this as completed in 29dfd1d Feb 3, 2014
nzakas added a commit that referenced this issue Feb 3, 2014
New Rule: no-extra-boolean-cast (fixes #557)
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@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 Feb 7, 2018
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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

3 participants