Rule suggestion: disallow !a instanceof b
See original GitHub issueAs the existing rule no-negated-in-lhs, it seems like it would be good to also disallow
if (!a instanceof b) // do something
since the developer certainly meant to write
if (!(a instanceof b)) // do something
instead. This prevents errors, and there’s no legitimate case for the former that I can think of, so I think this could be enabled by default as with no-negated-in-lhs. I’ve thrown together a quick copy and paste job of no-negated-in-lhs which works; will attach PR.
<bountysource-plugin>
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:36 (31 by maintainers)
Top Results From Across the Web
no-unsafe-negation - ESLint - Pluggable JavaScript Linter
This rule disallows negating the left operand of the following relational operators: in operator. instanceof operator. Examples of incorrect code for this rule:....
Read more >How to avoid 'instanceof' when implementing factory design ...
From what I've read on Stack Overflow, using 'instanceof' is a code smell. Is there a better way to check the type of...
Read more >False-Positive of rule "Expressions should not be too complex ...
In my opinion the rule “squid:S1067: Expressions should not be too complex” creates a false-positive in case of just multiple instanceof ...
Read more >Pattern matching for instanceof (Preview)
A variable of an interface type can hold a null reference or a reference to an instance of any class that implements the...
Read more >Java instanceof and Clean architecture
The problem is the use of instanceof in the save method of EntityRepository . If we want to prevent using instanceof, one solution...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@nzakas I think this is a rule to prevent enough common problem and this is accepted.
Accepted issues are good start points for new contributors, but until now, rule’s deprecation process is not clear; I guess it was a barrier to contributing to this issue. Just now, we are making some rules deprecated. I think we can go to advance this as well. (I can work on this later)
For this, we are strictly talking about using
!
within
andinstanceof
, though. I’m not sure it makes sense to extend the rule further than that.I’m still thinking that
no-unsafe-negation
is clearest for this use case.