royriojas / esformatter-jsx

esformatter plugin: format jsx files (or js files with Facebook React JSX Syntax)
MIT License
142 stars 25 forks source link

Why not format json string expression? #103

Closed janryWang closed 7 years ago

janryWang commented 7 years ago
<MyComponent expression={{"xxx":"yyy","aaaa":"bbbbb"}} />

can not format to

<MyComponent expression={ {
                                "xxx":"yyy",
                                "aaaa":"bbbbb"
                           }} />
royriojas commented 7 years ago

have you tried setting JSXExpressionsSingleLine to false?

// config
jsx: {
  formatJSX: true,
  attrsOnSameLineAsTag: true,
  maxAttrsOnTag: 3,
  firstAttributeOnSameLine: true,
  spaceInJSXExpressionContainers: ' ',
  alignWithFirstAttribute: false,
  JSXExpressionsSingleLine: false, // this is the flag that will allow objects to be formatted like your example
  formatJSXExpressions: true,
  JSXAttributeQuotes: 'single',
  htmlOptions: {
    brace_style: 'collapse',
    indent_char: ' ',
    indent_size: 2,
    max_preserve_newlines: 2,
    preserve_newlines: true,
  },
},

given this input

const renderFn = () => {
  return <MyComponent expression={{"xxx":"yyy","aaaa":"bbbbb"}} />
};

I obtain this output

const renderFn = () => {
  return <MyComponent expression={ {
                            'xxx': 'yyy',
                            'aaaa': 'bbbbb'
                          } } />
};
janryWang commented 7 years ago

Wow! Thanks @royriojas