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

Add no-unnecessary-negation rule #1922

Conversation

jguddas
Copy link

@jguddas jguddas commented Oct 1, 2022

Resolves #856.

This does not cover swapping for if-statements and ternary expressions, nor does it cover binary algebra optimization.

@@ -0,0 +1,36 @@
# This rule checks if conditions can be simpified
Copy link
Owner

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.

Copy link
Author

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 -->
Copy link
Owner

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 `!`.
Copy link
Owner

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
!(a != b);
!(a !== b);


a == b;
!a;
!a;
Copy link
Owner

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.',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too vague.

Copy link
Author

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.

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.

@jguddas
Copy link
Author

jguddas commented Jan 12, 2023

I just realized that there is no-extra-boolean-cast.
I will close this for now and if I have time see if there are improvements that can me made in no-extra-boolean-cast.

@jguddas jguddas closed this Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: no-unnecessary-negation
4 participants