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

Weird formatting of long nested ternary expression #76

Open
JStonevalley opened this issue Feb 20, 2019 · 21 comments
Open

Weird formatting of long nested ternary expression #76

JStonevalley opened this issue Feb 20, 2019 · 21 comments

Comments

@JStonevalley
Copy link

JStonevalley commented Feb 20, 2019

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

@sheerun
Copy link
Owner

sheerun commented Feb 20, 2019

This would be solved after brodycj/prettierx#44 is fixed

@brodycj
Copy link

brodycj commented Feb 20, 2019

brodybits/prettierx#44

I hope to resolve it soon, no promises right now.

@Astra-RX
Copy link

Any updates? still broken.

@sheerun
Copy link
Owner

sheerun commented Jun 24, 2019

I think @brodybits removed this issue from prettierx but it still needs to be fixed there. If anyone would like to see it fixed, please controbute to prettierx repository

@brodycj
Copy link

brodycj commented Jun 24, 2019

The issue was moved to brodycj/prettierx#13 (original repository was renamed, issue was transferred, but link did not seem to redirect). And I think the issue was already fixed by @aMarCruz.

@sheerun, I think this repo needs to use a newer version of prettierx.

@sheerun
Copy link
Owner

sheerun commented Jun 24, 2019

@brodybits Good to know, I'll try to upgrade this repo

@aMarCruz
Copy link

aMarCruz commented 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

@sheerun
Copy link
Owner

sheerun commented Jun 25, 2019

Ternary expressions should be fine in 10.0.0

@sheerun
Copy link
Owner

sheerun commented Jun 25, 2019

Hmm I need to revert because there are unexpected issues with other syntax

@sheerun sheerun reopened this Jun 25, 2019
@sheerun
Copy link
Owner

sheerun commented Jun 25, 2019

Here's an example how prettier-standard formats right now:

const Component = () => (
  <ul className='has-text-centered'>
    {landmarks.hits.length > 0
      ? landmarks.hits.map(({ name, objectID }) => (
          <li key={name}>
            <a href={`${process.env.DOMAIN}` + `/city/${objectID}`}>{name}</a>
          </li>
        ))
      : null}
  </ul>
)

And here's how after fixing ternary expressions:

const Component = () => (
  <ul className='has-text-centered'>
    {landmarks.hits.length > 0
      ? landmarks.hits.map(({ name, objectID }) => (
        <li key={name}>
          <a href={`${process.env.DOMAIN}` + `/city/${objectID}`}>{name}</a>
        </li>
      ))
      : null}
  </ul>
)

As you can see there's missing indent even though there's no second ternary expression.

@Astra-RX

This comment has been minimized.

@Astra-RX
Copy link

And here's how after fixing ternary expressions:

const Component = () => (
  <ul className='has-text-centered'>
    {landmarks.hits.length > 0
      ? landmarks.hits.map(({ name, objectID }) => (
        <li key={name}>
          <a href={`${process.env.DOMAIN}` + `/city/${objectID}`}>{name}</a>
        </li>
      ))
      : null}
  </ul>
)

@sheerun It's the same result as current version of standard --fix, so let's take this step and feedback the upstream I guess?

@aMarCruz
Copy link

Should be. I based my PR on the ESLint rule code, which is the one that Standard uses.

@sheerun
Copy link
Owner

sheerun commented Jun 27, 2019

I'm sorry that this issue is this deep, but I think it should be fixed in standard first. Nested ternary expressions are far less common than single ternary with JSX code or map (as shown above) and I think this change would make formatting less readable. Formatting my project which this change re-formats tenths of files where nested ternary is not used.

@sheerun
Copy link
Owner

sheerun commented Jun 27, 2019

Here is another example of inconsistent formatting:

standard --fix

const Component = () => (
  <ul className='has-text-centered'>
    {landmarks.hits.length > 0
      ? landmarks.hits.map(({ name, objectID }) => {
        return someverylongvariablelololololololololololololololol
      }).map(({ name, objectID }) => {
        return <foo />
      })
      : null}
  </ul>
)

But similar code with standard --fix (with new line after before .map) yields:

const Component = () => (
  <ul className='has-text-centered'>
    {landmarks.hits.length > 0
      ? landmarks.hits
        .map(({ name, objectID }) => {
          return someverylongvariablelololololololololololololololol
        }).map(({ name, objectID }) => {
          return <foo />
        })
      : null}
  </ul>
)

On the other hand currently prettier-standard formats both examples consistently (it doesn't matter where .map is):

const Component = () => (
  <ul className='has-text-centered'>
    {landmarks.hits.length > 0
      ? landmarks.hits
          .map(({ name, objectID }) => {
            return someverylongvariablelololololololololololololololol
          })
          .map(({ name, objectID }) => {
            return <foo />
          })
      : null}
  </ul>
)

@sheerun
Copy link
Owner

sheerun commented Jun 27, 2019

Also, here's original example formatted with standard --fix if .map is on new line:

const Component = () => (
  <ul className='has-text-centered'>
    {landmarks.hits.length > 0
      ? landmarks.hits
        .map(({ name, objectID }) => (
          <li key={name}>
            <a href={`${process.env.DOMAIN}` + `/city/${objectID}`}>{name}</a>
          </li>
        ))
      : null}
  </ul>
)

@bsiddiqui
Copy link

@sheerun any update on this?

@sheerun
Copy link
Owner

sheerun commented Nov 6, 2019

No, someone needs to fix this at prettierx

@brodycj
Copy link

brodycj commented Nov 6, 2019

We already made a fix before, see brodycj/prettierx#13.

In case of troubles, someone please raise a new issue in https://github.com/brodybits/prettierx/issues with a minimal, reproducible example ([1]).

[1] https://stackoverflow.com/help/minimal-reproducible-example

@sheerun
Copy link
Owner

sheerun commented Nov 7, 2019

I've described issue in more detail in brodycj/prettierx#41

So in short probably standard.js would need to be convinced first to make this change...

@sheerun sheerun changed the title Weird formatting of long trenary expression Weird formatting of long ternary expression Nov 7, 2019
@sheerun
Copy link
Owner

sheerun commented 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: New: Add offsetTernaryExpressions option for indent rule eslint/eslint#12556

  2. Then I need to convince standard to use this rule as default: Indentation/alignment within ternary branches (consequent/alternate) standard/standard#927

  3. Finally I need to convince prettierx to change how --no-align-ternary-line works:
    Inconsistent formatting of nested ternary expressions brodycj/prettierx#41

@sheerun sheerun changed the title Weird formatting of long ternary expression Weird formatting of long nested ternary expression Dec 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants