title | layout | edit_link |
---|---|---|
no-unsafe-negation - Fixes and Suggestions | doc |
no-unsafe-negation
This rule disallows negating the left operand of relational operators.
Error Message
Unexpected negating the left operand of [insert operator here] operator.
This error message is displayed when there is an unexpected negating of the left operand of relational operators (in
, instanceof
).
Example of incorrect code for this rule:
/*eslint no-unsafe-negation: "error"*/ !a in b (!a in b) !(a) in b !a instanceof b (!a instanceof b) !(a) instanceof b
If the rule's enforceForOrderingRelations
option is set to true
, then the error message is also displayed for ordering relational operators (<
, >
, <=
, >=
).
Example of incorrect code for this rule, with the { "enforceForOrderingRelations": true }
option:
/*eslint no-unsafe-negation: ["error", { "enforceForOrderingRelations": true }]*/ if (! a < b) {} while (! a > b) {} foo = ! a <= b; foo = ! a >= b; ! a <= b
Suggestions
The problem reported by this rule may be fixable by either of the following editor suggestions:
-
suggestNegatedExpression
negates the expression instead of its left operand -
suggestParenthesisedNegation
wraps the negation in parenthesis to make the intention explicit
suggestNegatedExpression
This suggestion negates the operator expression instead of its left operand.
This changes the current behavior.
Examples of correct code for this rule, fixed with the suggestNegatedExpression
suggestion:
/*eslint no-unsafe-negation: "error"*/ !(a in b) //Originally: !a in b (!(a in b)) //Originally: (!a in b) !((a) in b) //Originally: !(a) in b !(a instanceof b) //Originally: !a instanceof b (!(a instanceof b)) //Originally (!a instanceof b) !((a) instanceof b) //Originally: !(a) instanceof b
Examples of correct code for this rule, with the { "enforceForOrderingRelations": true }
option, fixed with the suggestNegatedExpression
suggestion:
/*eslint no-unsafe-negation: ["error", { "enforceForOrderingRelations": true }]*/ if (!( a < b)) {} //Originally: if (! a < b) {} while (!( a > b)) {} //Originally: while (! a > b) {} foo = !( a <= b); //Originally: foo = ! a <= b; foo = !( a >= b); //Originally: foo = ! a >= b; !( a <= b) //Originally: ! a <= b
suggestParenthesisedNegation
This suggestion wraps the negation in paranthesis to make the intention explicit.
This preserves the current behavior.
Examples of correct code, fixed with the suggestParenthesisedNegation
suggestion:
/*eslint no-unsafe-negation: "error"]*/ (!a) in b //Originally: !a in b ((!a) in b) //Originally: (!a in b) (!(a)) in b //Originally: !(a) in b (!a) instanceof b //Originally: !a instanceof b ((!a) instanceof b) //Originally: (!a instanceof b) (!(a)) instanceof b //Originally: !(a) instanceof b
Examples of correct code for this rule, with the { "enforceForOrderingRelations": true }
option, fixed with the suggestParenthesisedNegation
suggestion:
/*eslint no-unsafe-negation: ["error", { "enforceForOrderingRelations": true }]*/ if ((! a) < b) {} //Originally: if (! a < b) {} while ((! a) > b) {} //Originally: while (! a > b) {} foo = (! a) <= b; //Originally: foo = ! a <= b; foo = (! a) >= b; //Originally: foo = ! a >= b; (! a) <= b //Originally: ! a <= b
Version
These suggestions were introduced in ESLint 6.8.0.