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
Add no-unnecessary-negation
rule
#1922
Add no-unnecessary-negation
rule
#1922
Conversation
@@ -0,0 +1,36 @@ | |||
# This rule checks if conditions can be simpified |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a valid title. Look at the other titles for inspiration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about: Use as few negations as possible
?
Or: Use as few negations as possible and remove unnecessary Boolean casting
.
|
||
🔧 _This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems)._ | ||
|
||
<!-- /RULE_NOTICE --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to update this per 7d804e3
|
||
<!-- /RULE_NOTICE --> | ||
|
||
Dissallow unnecessary negations when using `Boolean` or `!`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
## Fail | ||
|
||
```js | ||
!(a != b); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!(a != b); | |
!(a !== b); |
|
||
a == b; | ||
!a; | ||
!a; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicates
|
||
const MESSAGE_ID = 'no-unnecessary-negation'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Expression can be simpified.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too vague.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about: Expression can use less negations.
?
The problem here is that there are also cases like if (Boolean(a)) { … }
that will show this message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, but I would suggest Expression can use fewer negations.
(fewer
instead of less
) as the number of negations is an unambiguous integer.
I just realized that there is no-extra-boolean-cast. |
Resolves #856.
This does not cover swapping for if-statements and ternary expressions, nor does it cover binary algebra optimization.