Extra indent on ternary with long expressions
See original GitHub issuePrettier 1.16.1 Playground link
Input:
function doThing() {
return arg =>
!arg
? null
: otherArg => (
<MagicThing
with="Kinda long"
attrs={{ a: 1, b: 2 }}
that={otherArg}
keep="going"
/>
)
}
Output:
function doThing() {
return arg =>
!arg
? null
: otherArg => (
<MagicThing
with="Kinda long"
attrs={{ a: 1, b: 2 }}
that={otherArg}
keep="going"
/>
);
}
Expected behavior:
Do not add extra two spaces of indent.
Comments
First let me say I’ve used Prettier for a few weeks now and really enjoy it. This has been my only hang-up after weeks of use. It’s forced me to rewrite ternaries to avoid this ascii art indentation.
The previous issue was locked due to inactivity, but I think it deserves a second look.
Some obvious reasons off the top of my head:
- Breaks expected indent with eslint
indent
rule - Inconsistent with other indentation rules used by Prettier
- Adds two-space indent even when using non-2-space indentation
- Adds mixed spaces and tabs indentation when using tabs
- Reduces readability for me, especially with code folding
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Which coding style you use for ternary operator? [closed]
I try not to use a ternary operator to write nested conditions. It defies readability and provides no extra value over using a...
Read more >Ternary operators are the worst..unless you indent them right!
There is no reason that an if expression can't be multiple lines long. If you need a condition with three or more branches,...
Read more >How to write clean nested ternaries | by Rajesh Naroth - Medium
IMO, nested ternary expressions are compact and a better alternative to using if/else. Let us take a fictitious large if else condition:
Read more >549606 – Expressions after a ternary expression not inside of ...
All expressions after a ternary expression that is not contained inside of a compound statement, indent at the same indentation level as the...
Read more >Gwydion Style Guide for Java - Carnegie Mellon University
Binary expressions may be wrapped before an operator and indented with one extra indent level from the open parenthesis. Example:
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 FreeTop 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
Top GitHub Comments
Hi!
We don’t try to conform to eslint rules.
Can you be more specific?
Yes, we’ll align the whole “alternate” (the node after
:
). In your case, beforeotherArg
there are two characters (:
and a space) and so we’ll add 2 spaces to align everything correctly.This isn’t something we’re really concerned since we still haven’t seen a valid argument against it.
I think the Prettier version is much more readable because of indentation.
I don’t like it because it creates strange-looking unbalanced indentation sometimes. Example:
Prettier’s current output (tabs replaced with 4 spaces because otherwise GitHub shows them as 8 spaces):
Expected: