Skip to content

alternate ternary formatting #737

@mvolkmann

Description

@mvolkmann

Here is how Prettier currently reformats nested ternaries:

const value = condition1
  ? value1
  : condition2
      ? value2
      : condition3
          ? value3
          : value4;

This isn't so bad when there are only two ternaries, but I sometimes have many more because I essentially use them as an alternative to a switch statement when each case only gets a single value.

Here is how I prefer to format this case:

const value =
  condition1 ? value1 :
  condition2 ? value2 :
  condition3 ? value3 :
  value4;

I like this because it clearly associates each condition with its corresponding value and the indentation doesn't increase regardless of the number of ternaries.

Perhaps some like the current formatting because it discourages use of nested ternaries, but I think my example shows that you can have any number without making the code hard to read.

Would you consider adding an option to support this alternate formatting? Maybe something like this? --ternary=condval

Metadata

Metadata

Assignees

Labels

locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.status:has prIssues with an accompanying pull request. These issues will probably be fixed soon!status:needs discussionIssues needing discussion and a decision to be made before action can be taken

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions