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.

Extra indent on ternary with long expressions

See original GitHub issue

Prettier 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:open
  • Created 5 years ago
  • Reactions:14
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
duailibecommented, Jan 30, 2019

Hi!

Breaks expected indent with eslint indent rule

We don’t try to conform to eslint rules.

Inconsistent with other indentation rules used by Prettier

Can you be more specific?

Adds two-space indent even when using non-2-space indentation

Yes, we’ll align the whole “alternate” (the node after :). In your case, before otherArg there are two characters (: and a space) and so we’ll add 2 spaces to align everything correctly.

Adds mixed spaces and tabs indentation when using tabs

This isn’t something we’re really concerned since we still haven’t seen a valid argument against it.

Reduces readability for me, especially with code folding

I think the Prettier version is much more readable because of indentation.

3reactions
thorn0commented, Jan 30, 2019

Adds mixed spaces and tabs indentation when using tabs

This isn’t something we’re really concerned since we still haven’t seen a valid argument against it.

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):

const template =
    '<div>' +
    (isToolboxVisible
        ? '<div class="grid-toolbox with-top-line">' +
          (showCloseButton
                ? '<div class="toolbox-group pull-right"><a id="close-btn">Close</a></div>'
                : '') +
          (showApplyButton
                ? '<div class="toolbox-group pull-right"><a id="apply-btn">Apply</a></div>'
                : '') +
          '</div>'
        : '') +
    '</div>';

Expected:

const template =
    '<div>' +
    (isToolboxVisible
        ? '<div class="grid-toolbox with-top-line">' +
            (showCloseButton
                ? '<div class="toolbox-group pull-right"><a id="close-btn">Close</a></div>'
                : '') +
            (showApplyButton
                ? '<div class="toolbox-group pull-right"><a id="apply-btn">Apply</a></div>'
                : '') +
            '</div>'
        : '') +
    '</div>';
Read more comments on GitHub >

github_iconTop 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 >

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