Ternary formatting is a bit unexpected
See original GitHub issueI’m sure this is intentional, but wanted to point it out/ask. My source:
let foo = (bar === 0) ?
baz :
qux;
Which gets reformatted to:
let foo = (bar === 0)
? baz
: qux;
This is a little strange to me, as it seems like the first stanza is the “question” and should keep the ?
token. The second and third stanzas are “truthy” answer and “falsy” answer.
let foo = question ?
truthyAnswer :
falsyAnswer;
I can see where the current formatting is “prettier” in that the ?
and :
stack neatly, just wondering if it throws the implication of the syntax a bit.
Thx!
Issue Analytics
- State:
- Created 7 years ago
- Comments:17 (6 by maintainers)
Top Results From Across the Web
java - Unexpected type resulting from the ternary operator
The ternary operator requires both result values be the same type, ... to solve the problem with integers encoded in floating point format....
Read more >Prettier 1.12: Fixes, Features, and Formatting, Oh My!
A bug that has been open for a long time deals with the behavior around printing comments before else . Comments before the...
Read more >Rethinking the JavaScript ternary operator - James Sinclair
Lots of people treat the ternary operator with suspicion. At first glance, ternaries appear unnecessary. Nothing more than a tool for the ...
Read more >Ternary operator: break after or before? - Google Groups
The Chromium style guide and clang-format disagree about how to linebreak ... in a CL I reviewed my first impression was pleasant surprise...
Read more >I shall never give up on forcing ternary statements down my ...
Best piece of advice I had ever heard about coding is, always code for the next person ... But again, formatting probably matters...
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 Reddit Thread
No results found
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
In my experience having
?
and:
on the new line improves the readability (as it’s easier to immediately see them while scanning code). This becomes more obvious when you have something more than a single word, e.g. an example with arequire
:(In the former it kinda looks like it’s requiring both until I read the code more closely, while in the latter it’s immediately obvious while scanning the code that it’s a ternary)
Or with jsx:
I feel the latter is more obvious when quickly scanning through the code (though, I’m biased as that’s the way I’ve written them for a long time, so might just be because of experience that I scan them quicker).
Here’s an example of a ternary in my code:
And here is how Prettier reformats it:
I know it’s subjective, but I think my code is more readable. In my real code there are 9 ternaries, so the Prettier indentation gets pretty extreme.
The default indentation is 2 spaces, so why does it switch to 4-space indentation after the first ternary?