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.

Prevent confusing if-statement usage

See original GitHub issue

Is there a way to configure eslint to prevent confusing if-statements like the following?

if (condition) {
  // stuff
} if (anotherUnrelatedCondition) {
  // more
} 

This is confusing because at first glance it looks like the second if is an else if, but it’s not.

I couldn’t find a rule to enforce this. It seems that this could potentially be part of the keyword-spacing rule. Perhaps an option to require newlines, not just a space, before certain keywords.

But I’m not sure this is the right approach. Thoughts?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Nov 3, 2017

Closing because this should now be solved by padding-line-between-statements.

1reaction
dcousenscommented, Feb 9, 2017

Maybe no-stalking-if

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Confusing If Statement? - Stack Overflow
Then the design is faulty. You should avoid inherently negative bools: use isOkay instead of isFaulty, hasMembers instead of hasNoMembers, ...
Read more >
Developer insists if statements shouldn't have negated ...
Explicit else block. The first rule just pollutes the code and makes it neither more readable, nor less error-prone.
Read more >
if / else errors - learn how to fix these - Codecademy
A typical if () statement looks like this. Depending on what you are checking for, the `else if` and `else` blocks may not...
Read more >
Else Clauses on Loop Statements
If we call the else clause on an if statement a “conditional else”, ... The simplest approach for any new language to take...
Read more >
4. More Control Flow Tools — Python 3.11.1 documentation
Besides the while statement just introduced, Python uses the usual flow ... Look closely: the else clause belongs to the for loop, not...
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