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

New option in no-unsafe-negation to report ! a < b #12163

Closed
mdjermanovic opened this issue Aug 25, 2019 · 4 comments · May be fixed by rubarb666/rails#3, rubarb666/git-sketch-plugin#2, O330oei/node#4 or O330oei/node#11

Comments

@mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Aug 25, 2019

What rule do you want to change?

no-unsafe-negation

This rule disallows negating the left operand of Relational Operators.

The rule currently targets only in and instanceof.

<, >, <=, >= are also relational operators.

! a < b is equivalent to (a ? 0 : 1) < b (I think), which is rarely the intended meaning:

function min(a, b) {
    let m = a;
    if (! a < b) {
        m = b;
    }
    return m;
}

min(1, 2); // 2

Does this change cause the rule to produce more or fewer warnings?

More if the option is set to true. Default is false.

How will the change be implemented? (New option, new default behavior, etc.)?

New option.

Please provide some example code that this change will affect:

/*eslint no-unsafe-negation: "error"*/

if (! a < b) {}

while (! a > b) {}

foo = ! a <= b;

foo = ! a >= b;

What does the rule currently do for this code?

Nothing.

What will the rule do after it's changed?

4 errors

/*eslint no-unsafe-negation: ["error", { "enforceForOrderingRelations": true }]*/

if (! a < b) {} // error

while (! a > b) {} // error

foo = ! a <= b; // error

foo = ! a >= b; // error

Are you willing to submit a pull request to implement this change?

Yes.

@kaicataldo
Copy link
Member

@kaicataldo kaicataldo commented Sep 30, 2019

This has now been accepted.

@samrae7
Copy link
Contributor

@samrae7 samrae7 commented Oct 11, 2019

@mdjermanovic do you still want to do the PR for this? If not I could take it on

@mdjermanovic
Copy link
Member Author

@mdjermanovic mdjermanovic commented Oct 11, 2019

@samrae7 feel free to claim this. PR is welcome, thanks!

@samrae7
Copy link
Contributor

@samrae7 samrae7 commented Oct 12, 2019

@mdjermanovic thank you. I will work on it over the weekend

samrae7 added a commit to samrae7/eslint that referenced this issue Oct 13, 2019
samrae7 added a commit to samrae7/eslint that referenced this issue Oct 13, 2019
samrae7 added a commit to samrae7/eslint that referenced this issue Oct 13, 2019
@mdjermanovic mdjermanovic added enhancement and removed evaluating labels Oct 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.