You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just as developers might type -a + b when they mean -(a + b) for the negative of a sum, they might type !key in object by mistake when they almost certainly mean !(key in object) to test that a key is not in an object. !obj instanceof Ctor is similar.
/*eslint no-unsafe-negation: "error"*/if(!keyinobject){// operator precedence makes it equivalent to (!key) in object// and type conversion makes it equivalent to (key ? "false" : "true") in object}if(!objinstanceofCtor){// operator precedence makes it equivalent to (!obj) instanceof Ctor// and it equivalent to always false since boolean values are not objects.}
Examples of correct code for this rule:
/*eslint no-unsafe-negation: "error"*/if(!(keyinobject)){// key is not in object}if(!(objinstanceofCtor)){// obj is not an instance of Ctor}if((""+!key)inobject){// make operator precedence and type conversion explicit// in a rare situation when that is the intended meaning}
Just as developers might type
-a + b
when they mean-(a + b)
for the negative of a sum, they might type!key in object
by mistake when they almost certainly mean!(key in object)
to test that a key is not in an object.!obj instanceof Ctor
is similar.Rule Details
This rule disallows negating the left operand of Relational Operators.
Relational Operators are:
in
operator.instanceof
operator.Examples of incorrect code for this rule:
Examples of correct code for this rule:
http://eslint.org/docs/rules/no-unsafe-negation
Likely to catch bugs, and there's basically never a reason to write code that this would flag, so I'll merge this into the standard v8 beta.
The text was updated successfully, but these errors were encountered: