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.

Rule proposal: `no-unnecessary-negation`

See original GitHub issue

Description

This rule would enforce users to simplify negations: !(a === b) -> a !== b. There might be other conditions like this that could be simplified.

Fail

if (!(a > b)) {}
const foo = !(bar === baz);
const foo = !(typeof bar === 'undefined');

Pass

if (a <= b) {}
const foo = bar !== baz;
const foo = typeof bar !== 'undefined';
const foo = !Array.isArray(bar);

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
noftalycommented, Oct 23, 2022

You have to be careful simplifying expressions with - and + because of automatic type inference. There are edge cases where a - -b is not the same as a + b

> 3 - -[]
3
> 3 + []
'3'
> 3 - -{}
NaN
> 3 + {}
'3[object Object]'
> 3 - -Number
NaN
> 3 + Number
'3function Number() { [native code] }'
2reactions
jguddascommented, Oct 23, 2022

The code is not in a presentable state, but it works 🎉

https://astexplorer.net/#/gist/c922ca1591e8db763030071c09df875a/3865211cf9b06a03034a0b199d0e65f6ff8f49c5

Please let me know when you find something that does not work as you expect and generally what you have tried out, so we can include that in our test cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule proposal: no-unnecessary-falsy-condition · Issue #5592 - GitHub
My proposed rule identifies when there is a specific kind of overlap such that it is safe to shorten the code. My rule...
Read more >
Statement on Proposed Rule Regarding Best Execution
If not, then given that government regulation typically imposes costs, and often has unintended negative consequences, we should be circumspect.
Read more >
Electricians Proposed Administrative Rules
Section 90.4 of the 2020 NEC authorizes the Department to waive specific code requirements when doing so will not have a negative impact...
Read more >
A Guide to the Rulemaking Process - Federal Register
Can an agency issue a final rule without a publishing a proposed rule? ... actions under consideration; it also states why the rule...
Read more >
Rule 103. Rulings on Evidence | LII / Legal Information Institute
The subdivision answers in the negative. The judge can foreclose a particular line of testimony and counsel can protect his record without a...
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