Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

case-style-ternaries: format nested ternaries in case style #272

Open
4 tasks done
arthur-fontaine opened this issue Jan 26, 2024 · 1 comment
Open
4 tasks done
Labels
enhancement New feature or request

Comments

@arthur-fontaine
Copy link

arthur-fontaine commented Jan 26, 2024

Clear and concise description of the problem

I find that nested ternaries are very difficult to read, even with the multiline-ternary rule enabled.

Example:

const animalName =
  pet.canBark() ?
    pet.isScary() ?
      'wolf'
    : 'dog'
  : pet.canMeow() ? 'cat'
  : 'probably a bunny';

Suggested solution

In their article, Prettier suggests to format the code above would be formatted as follows:

const animalName =
  pet.isScary() ? 'wolf'
  : pet.canBark() ? 'dog'
  : pet.canMeow() ? 'cat'
  : 'probably a bunny';

I find it more clear and readable.

This feature could be implemented with a new rule.

I think this is a stylistic issue in some point of view, but I am not sure this is a stylistic issue in the point of view of eslint-stylistic, so this issue is maybe out of concern...

Alternative

flatTernaryExpressions: true, option on indent can result to something similar, but does not refacto the logic. In fact, flatTernaryExpressions: true should be used with this rule to get the wanted result.

Validations

Contributes

  • If this feature request is accepted, I am willing to submit a PR to fix this issue
@arthur-fontaine arthur-fontaine added the enhancement New feature or request label Jan 26, 2024
@arthur-fontaine arthur-fontaine changed the title nested-ternaries: format nested ternaries as the new experimental-ternaries option of Prettier does case-style-ternaries: format nested ternaries in case style Jan 26, 2024
@kecrily
Copy link
Member

kecrily commented Jan 28, 2024

I prefer to add it when it becomes Prettier's default behavior, just in case it doesn't end up being adopted. Of course if you want to add it, that's fine with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants