Comment between if/else is formatted strangely
See original GitHub issueif(x) {
}
// comment
else {
}
output:
if (x) {
} else
// comment
{
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (9 by maintainers)
Top 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 >
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 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
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:
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:
attach
, we can check if there is any syntax between the comment and thefollowingNode
. There is already code inbreakTies
that does this. This works because theelse
syntax sits between the comment and the block’s{
, so we know something special is going on.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 thecatch
statement of try/catch as well. Unfortunately it makes the comment code even more complicated, but I guess we need to do it.That already looks pretty good:
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.