question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check `!foo === bar` in `no-unsafe-negation` rule

See original GitHub issue

What rule do you want to change?

Forbid use !foo === bar and !foo !== bar

Does this change cause the rule to produce more or fewer warnings?

more

How will the change be implemented? (New option, new default behavior, etc.)?

Not sure

Please provide some example code that this change will affect:

if (!foo === bar);

What does the rule currently do for this code?

Nothing

What will the rule do after it’s changed?

Forbid it.

Are you willing to submit a pull request to implement this change?

Maybe.


I understand !foo === bar maybe just coverting falsely value to compare to a boolean, but most time it’s a mistake,

I guess we should allow

// it's type-casting
if (!!foo === bar);
if (!(foo === bar));
if ((!foo) === bar);

I understand that stylistic rules are frozen, but still worth to make a proposal. I can understand if you decide not to accept.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:21 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Oct 17, 2021

Looking at this more closely, I’m not sure adding checks for equality operators make sense. The point of this rule is to warn when negating a value will cause an unexpected result. It makes sense for the “in” and “instanceof” operators because you likely mistyped. It makes sense for comparison operators because you can’t really compare Boolean values.

I agree with @nzakas on this. !a in b or !a < b almost never make sense, but !a === b is valid code if b is a boolean. Since we don’t have type info, an error reported on such code would be a false positive, or could be interpreted as stylistic enforcement which isn’t the intent of this rule.

I was in favor of this option in no-unsafe-negation, but now I think that we should report !a === b as a potential problem only if we’re sure that the right side is not a boolean, so this rather belongs to no-constant-binary-expression rule (proposal https://github.com/eslint/eslint/issues/13752, there’s !foo == null example in the original post), which will be based on determining types of the operands when possible. That would catch https://github.com/prettier/prettier/commit/c7e354e38cfd051d3f64b56aeda35e9a0a669de6#r47946398 as <boolean> === <string>.

1reaction
fiskercommented, Jul 16, 2021

@coderaiser Stop, please!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Foobar Challenge. What You Should Know | by
Check that your imports are correct. The Foobar Challenge uses Java 8, which is pretty ancient. Check that you don't use any Java...
Read more >
Rules - JavaScript Standard Style
This is a summary of the standard JavaScript rules. The best way to learn about standard is to just install it and give...
Read more >
Rule overview - Deno
Deno supports the triple-slash reference directives of types , path , lib , and no-default-lib . This lint rule checks if there is...
Read more >
The Foobar challenge: Google's hidden test for developers
by Daniel Simmons The Foobar challenge: Google's hidden test for developers You're just sitting at your desk, minding your own business, ...
Read more >
Code Issues - Embold Help Center
We have discovered some checks and rules for C#. ... if a constructor Foo() calls a private method bar() that calls a public...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found