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.

Weird formatting of long nested ternary expression

See original GitHub issue

Hi prettier standard formats my ternary expression without indent when breaking a line that is too long. This does not seem correct. The simplified code snippet looks as follows: expected

When formatted it looks like this: actual

My only config is: { "printWidth": 120 } Regards

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:21 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
aMarCruzcommented, Jun 25, 2019

@sheerun , use the "alignTernaryLines": false in your prettierrc.json

with alignTernaryLines as false you have this output:

image

The logic is the same as ESLint indent.flatTernaryExpressions

1reaction
sheeruncommented, Nov 11, 2019

So now the plan is as follows…

  1. First eslint needs to merge PR I’ve prepared that introduces option for indent rule that fixes this issue: https://github.com/eslint/eslint/pull/12556

  2. Then I need to convince standard to use this rule as default: https://github.com/standard/standard/issues/927

  3. Finally I need to convince prettierx to change how --no-align-ternary-line works: https://github.com/brodybits/prettierx/issues/41

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - A somewhat painful triple-nested ternary operator
If your JavaScript codebase contains nested ternary statements like the one in question, consider converting the formatting to daisy chained ternary ...
Read more >
Use nested ternary operators without confusion with better ...
Fix this with a simple formatting trick that makes long, nested ternary operators easy to understand. Instead of: const foo = a ?...
Read more >
Rethinking the JavaScript ternary operator - James Sinclair
With the ternary operator, the second and third expressions can be any type. But the interpreter will always cast the first to a...
Read more >
I shall never give up on forcing ternary statements down my ...
But again, formatting probably matters the most. ... As long as it isn't nested I think ternary operator is fine and it depends...
Read more >
Prettier 1.12: Fixes, Features, and Formatting, Oh My!
When ternaries are nested, depending on the print width and indentation level, sometimes the outer ternary could get broken across multiple ...
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