no-unsafe-negation
NOTE: this rule is part of the
recommended rule set.Enable full set in
deno.json:{
"lint": {
"rules": {
"tags": ["recommended"]
}
}
}Enable full set using the Deno CLI:
deno lint --rules-tags=recommended
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the
include or exclude array in deno.json:{
"lint": {
"rules": {
"include": ["no-unsafe-negation"],
"exclude": ["no-unsafe-negation"]
}
}
}Disallows the usage of negation operator ! as the left operand of relational
operators.
! operators appearing in the left operand of the following operators will
sometimes cause an unexpected behavior because of the operator precedence:
inoperatorinstanceofoperator
For example, when developers write a code like !key in someObject, most likely
they want it to behave just like !(key in someObject), but actually it behaves
like (!key) in someObject. This lint rule warns such usage of ! operator so
it will be less confusing.
Invalid:
if (!key in object) {}
if (!foo instanceof Foo) {}
Valid:
if (!(key in object)) {}
if (!(foo instanceof Foo)) {}
if ((!key) in object) {}
if ((!foo) instanceof Foo) {}