All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S3812.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

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();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy