57 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
if (!("prop" in myObj)) {
doTheThing();
}
if (!(foo instanceof MyClass)) {
doTheOtherThing();
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add parentheses to disambiguate this expression.
=== Highlighting
minus operator
endif::env-github,rspecator-view[]