org.sonar.l10n.javascript.rules.javascript.S3812.html Maven / Gradle / Ivy
Mixing up the order of operations will almost always yield unexpected results.
Similarly, mis-applied negation will also yield bad results. For instance consider the difference between !key in dict
and !(key
in dict)
. The first looks for a boolean value (!key
) in dict
, and the other looks for a string and inverts the
result. !obj instanceof SomeClass
has the same problem.
This rule raises an issue when the left operand of an in
or instanceof
operator is negated.
Noncompliant Code Example
if (!"prop" in myObj) { // Noncompliant; "in" operator is checking property "false"
doTheThing(); // this block will be never executed
}
if (!foo instanceof MyClass) { // Noncompliant; "!foo" returns a boolean, which is not an instance of anything
doTheOtherThing(); // this block is never executed
}
Compliant Solution
if (!("prop" in myObj)) {
doTheThing();
}
if (!(foo instanceof MyClass)) {
doTheOtherThing();
}