Skip to content

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

@mdjermanovic

Description

@mdjermanovic

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.

Metadata

Metadata

Assignees

Labels

HacktoberfestRecommended issue for those participating in Hacktoberfest https://hacktoberfest.digitalocean.com/acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionenhancementThis change enhances an existing feature of ESLinthelp wantedThe team would welcome a contribution from the community for this issueruleRelates to ESLint's core rules

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions