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.

Comment between if/else is formatted strangely

See original GitHub issue
if(x) {
}
// comment
else {
}

output:

if (x) {
} else
  // comment
  {
  }

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jlongstercommented, Jan 30, 2017

I’m very tempted to do what @rafayepes suggests. It seems odd that you can inject comments and “stop” our styling rules. Normally we enforce this:

if() {
} else {
}

So it seems only natural do move them inside the block. It seems like a good place to enforce a consistent place for comments that are documenting the else conditions. Also, we want to implement the same behavior for all statements that have alternate blocks like try/catch.

It also removes the need for too much special-casing in our code. As long as we move the comment to the right place, it will “just work” 😃

If we are OK with that, here’s what I propose:

  • In the code that handles leading comments in attach, we can check if there is any syntax between the comment and the followingNode. There is already code in breakTies that does this. This works because the else syntax sits between the comment and the block’s {, so we know something special is going on.
  • If that’s the case, and the followingNode is a block, then we attach a leading comment to the first node inside the block instead. This moves the comment inside the block.

That’s a bit more generic than special-casing if and will work for the catch statement of try/catch as well. Unfortunately it makes the comment code even more complicated, but I guess we need to do it.

0reactions
jlongstercommented, Jan 30, 2017

That already looks pretty good:

if (a)
  doSomething();
else
  // comment
  doSomethingElse();

Basically, it forces it to break, if people really want to avoid blocks. I prefer enforcing blocks but not sure I want to do that in this project. Hard to say when we should enforce something or not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c - Formatting of if Statements
I think its pretty strange to think that someone may come along later and NOT realise they have to add the braces to...
Read more >
Strange if condition formatting · Issue #567 · prettier ...
I'm not sure about a for statement, but couldn't it be treated differently from an if? This to me looks a lot harder...
Read more >
Copying text in comments has strange formatting - Closed
When I try to copy a comment on a task it is a rich copy and includes strange margins and the dropdown arrow....
Read more >
Fix inconsistent and weird date formatting in a column in ...
I have a column which has a text string meant to represent date and time. The format of the data from the source...
Read more >
Weird behavior when formatting pages
Following @Mico's instruction from the comments above, adding \null to each empty subsection fixes the problem.
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 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