Eslint-plugin-react: Can't seem to properly indent ? : statements

Created on 6 Jun 2016  路  11Comments  路  Source: yannickcr/eslint-plugin-react

In our codebase we use quite a bit of this type of if/else rendering:

      {
          condition
          ? <div>
              <div> something </div>
            </div>
          : <div>
              <div> something </div>
            </div>
        }

Whatever indentation I try I always seem to get a jsx-indent error. Is there a way to format this without the linter complaining?

enhancement question rule

Most helpful comment

Seems related to #540

All 11 comments

Seems related to #540

540 has been closed - did it fix this? There was no test for it.

{condition ? (
  <div>
    <div> something </div>
  </div>
) : (
  <div>
    <div> something </div>
  </div>
)}

or

{condition
  ? (
    <div>
      <div> something </div>
    </div>
  )
  : (
    <div>
      <div> something </div>
    </div>
  )
}

?

Neither -

{condition
  ? <div>
      <div> something </div>
    </div>
  : <div>
      <div> something </div>
    </div>
}

Right, I'm suggesting two forms that should satisfy the rule.

If those work, and your example doesn't, then it seems like an issue when parens are omitted.

I would also love to be able to use:

condition
? <div>
     <div> something </div>
  </div>
: <div />

rather than

condition
? <div>
    <div> something </div>
</div>
: <div />

The closing

is aligned to the opening tags, rather than the ? and :.

Would this be possible to add as an option?

Is this ever going to get fixed? To this day, still have to keep jsx-indent turned off because this doesn't work as expected (or at least have an option).

Made a new issue since @ljharb perhaps indicated it's an issue with lacking parens (but that seems like it should be a valid use case?).

To clarify, I think it should be a supported use case, but I don't think it's a good style :-)

@ljharb Thanks for the clarification! That's good to hear. In your opinion should it be the default (when parens are omitted, that is) or an added option?

I think that the default should be identical when parens are omitted or not - iow, delete the parens out of https://github.com/yannickcr/eslint-plugin-react/issues/625#issuecomment-253309000 for the style without them.

The more compact style requested here should be an option (not the default)

Was this page helpful?
0 / 5 - 0 ratings