esformatter-jsx icon indicating copy to clipboard operation
esformatter-jsx copied to clipboard

Parenthesize multi-line jsx

Open aaronjensen opened this issue 9 years ago • 18 comments

Is it already possible or would this plugin be the right place to automatically parenthesize multi-line jsx?

In other words:

return <div>
  Hello World
</div>;

should become:

return (
  <div>
    Hello World
  </div>
);

aaronjensen avatar Oct 01 '15 20:10 aaronjensen

it would be possible to be added as an option. And yes it can be done in this plugin

royriojas avatar Oct 01 '15 21:10 royriojas

that said, pull requests welcome!

royriojas avatar Oct 01 '15 21:10 royriojas

Hello! I've managed to wrap multi-line JSX into parens. But the problem is the closing parenthesis, there's no any ident or whitespace token before it which I could remove. How can I do this?

return (
  <div>
    Hello World
  </div>
  );

roman01la avatar Jan 05 '16 18:01 roman01la

+1 for this

nerfologist avatar Jan 23 '16 16:01 nerfologist

+1

afc163 avatar Jun 20 '16 03:06 afc163

@roman01la do you still have the source for this?

chenglou avatar Sep 09 '16 09:09 chenglou

@chenglou I think it's here https://github.com/roman01la/html-to-react-components/blob/master/lib/format.js#L226 I'm using it in a tool to convert HTML to React components. You can find how to run it in readme.

roman01la avatar Sep 09 '16 09:09 roman01la

Very nice @roman01la. I will try to see if I find time to add this to esformatter-jsx directly.

royriojas avatar Sep 09 '16 19:09 royriojas

@royriojas Hey! Any updates on this issue? :)

roman01la avatar Dec 08 '16 09:12 roman01la

So sorry @roman01la I haven't had the time yet. Will try to see if I can get something done over the weekend. On the other hand, since you do have an idea of how to fix this, please don't hesitate to make a PR. Will be more than thankful for your help.

royriojas avatar Dec 08 '16 13:12 royriojas

Also, just in case you're also using eslint, the eslint-plugin-react has this rule https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-wrap-multilines.md that might help with this issue.

royriojas avatar Dec 08 '16 14:12 royriojas

I'm now using eslint & eslint-plugin-react with auto-fix for all my formatting needs.

DaleLJefferson avatar Dec 10 '16 08:12 DaleLJefferson

@DaleJefferson it seems like the auto fix simply wraps it in parens, so it ends up looking like this:

  render() {
    return (<div className={css(styles.avatar)}>
      {initials(this.props.user)}
    </div>)
  }

is that what you're seeing too?

aaronjensen avatar Dec 10 '16 20:12 aaronjensen

@aaronjensen Yes it's not perfect but I find ESLint --fix does 90% of the formatting, having a separate formatter seems redundant, I don't think there is an ESLint rule for new line after parens but i'm sure it would be fairly simple to write.

DaleLJefferson avatar Dec 11 '16 08:12 DaleLJefferson

@DaleJefferson me too, I tend to agree with that. I can see how eslint can be converted to a formatter as well in the future at least.

royriojas avatar Dec 12 '16 02:12 royriojas

@DaleJefferson - sorry if this is off topic... Do you know if it is possible to use auto-fix for ESLint to auto format in Sublime 3 ?

swinston1000 avatar Jan 04 '17 16:01 swinston1000

@swinston1000 I execute eslint --fix from the command line, not from Sublime. But probably there is an option for that.

royriojas avatar Jan 04 '17 16:01 royriojas

@royriojas - many thanks! I found it - https://github.com/TheSavior/ESLint-Formatter :-)

swinston1000 avatar Jan 04 '17 16:01 swinston1000