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

Enforce conditional expression styles #1424

Closed
mightyiam opened this issue Sep 26, 2019 · 2 comments
Closed

Enforce conditional expression styles #1424

mightyiam opened this issue Sep 26, 2019 · 2 comments
Labels

Comments

@mightyiam
Copy link
Member

The conditional expression can use some decisions regarding where lines are broken and where indentation is placed, I feel. For example, the following different styles are currently possible:

/* global env */
const prefix =
      env === 'prod' ? ''
        : env === 'local' ? 'local-'
          : 'dev-'

console.log(prefix)

This, as well:

/* global env */
const prefix =
      env === 'prod'
        ? ''
        : env === 'local'
          ? 'local-'
          : 'dev-'

console.log(prefix)

The way I see it, we already allow too many different styles. Ideally, according to the mission of StandardJS, we'd decide on a certain style and enforce it.

Ternary expressions seem a bit complicated in terms of possibilities of where to place the line breaks and how much to indent in which case. And I don't know whether we have the theory of that figured out. I assume @feross has his own habits.

I think a good first task would be to break down the various options into a list and post it here, with examples.

The Conditional Expression has the following syntax:
Screenshot from 2019-09-26 09-29-04

Simplified: condition ? then : else
These 5 parts:

  1. condition
  2. ?
  3. then
  4. :
  5. else

If we are to solve the styling of the ternary, perhaps we should first limit the ternary in some ways, using the #1422 plugin. That way, we have less things to think about in the scope of line breaks and indentation. So, I'm going to mark this as blocked by #1422.

@Alexsey
Copy link

Alexsey commented Sep 27, 2019

Both provided examples looks good depends on parts length: use first when condition and then are short and use second if condition and then are long so you want to split them in to separate lines, so it would be easier to read. The same logic as for any other binary operator e.g. ||:

const a = foo || getA()
const b = await (Promise.all(bar.map(fetch).find(v => v % 15)) ||
  generateBValueFunctionWithVeryLongName())

@feross
Copy link
Member

feross commented Oct 31, 2020

I shipped a rule in standard 16 which enforces ternary formatting. See: #1558

The two accepted styles are:

cond
  ? a
  : b

cond ? a : b

@feross feross closed this as completed Oct 31, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Archived in project
Development

No branches or pull requests

3 participants