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

jsx-curly-spacing should support "consistent" option #1493

Closed
kaiyoma opened this issue Oct 23, 2017 · 10 comments · Fixed by #2240
Closed

jsx-curly-spacing should support "consistent" option #1493

kaiyoma opened this issue Oct 23, 2017 · 10 comments · Fixed by #2240

Comments

@kaiyoma
Copy link

kaiyoma commented Oct 23, 2017

Similar to how other eslint rules provide a "consistent" option for square brackets, curly braces, and parens, there should be a similar option for the curly braces in JSX. Basically, the absence or presence of a newline after the opening curly brace should be matched at the closing curly brace.

@ljharb
Copy link
Member

ljharb commented Oct 23, 2017

Can you provide some code samples that it should warn on, and what it would autofix to?

@kaiyoma
Copy link
Author

kaiyoma commented Oct 24, 2017

No newlines at either end is good:

  <div>
    {data.message ? <span>You have an unread message</span> : <Placeholder/>}
  </div>

Newlines at both ends is also good:

  <div>
    {
      data.message ?
        <span>You have an unread message</span> :
        <Placeholder/>
    }
  </div>

But mixing and matching is bad:

  <div>
    {
      data.message ?
        <span>You have an unread message</span> :
        <Placeholder/>}
  </div>
  <div>
    {data.message ?
        <span>You have an unread message</span> :
        <Placeholder/>
    }
  </div>

Related eslint rules that support "consistent":

@ljharb
Copy link
Member

ljharb commented Oct 24, 2017

Sounds like a great addition! The airbnb config would use this option.

@kamronbatman
Copy link

kamronbatman commented Mar 9, 2018

Definitely need this.

There is also no rule to enforce a newline if the JSX curly brace block is multiline. Optionally I would not mind a rule that enforces curly brace blocks to be on their own lines too (the second example would be changed to Bad).

Bad:

<MyTag>{ conditional ?
  true :
  false  }
</MyTag>

Good:

<MyTag>{
  conditional ?
   true :
   false
}</MyTag>

Good:

<MyTag>
  {
    conditional ?
     true :
     false
  }
</MyTag>

Not sure if it is possible, but inheriting the config settings from object-curly-newline is fine with me.

@ljharb
Copy link
Member

ljharb commented Mar 9, 2018

The third example is the one I'd want.

@kamronbatman
Copy link

I agree with you @ljharb. Currently I cannot enforce that with eslint-plugin-react. I can try and find some time to do a dirty addition, but I am not familiar with writing eslint rules. It might go faster with someone else who is more familiar.

@viktor-equinix
Copy link

Is anybody found the way how to configure eslint for 3rd example above?

@kamronbatman
Copy link

kamronbatman commented Jun 27, 2018

This rule effectively doesn't exist. :(

@JBallin
Copy link
Contributor

JBallin commented Jan 21, 2019

Should also enforce consistent spacing around content.

BAD (missing space at end)
<h3>{ `Error: ${error}`}</h3>

GOOD (space before + after)
<h3>{ `Error: ${error}` }</h3>

GOOD? (no spaces before/after)
<h3>{`Error: ${error}`}</h3>

@ljharb
Copy link
Member

ljharb commented Jan 21, 2019

A PR to implement this is most welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

5 participants