beautifier / js-beautify

Beautifier for javascript
https://beautifier.io
MIT License
8.61k stars 1.38k forks source link

Array of objects in JSON #1402

Open rinu opened 6 years ago

rinu commented 6 years ago

Description

I need the beautified version not to start an array and object on the same line. This is for code folding with codemirror which only has one folding button per line. It defaults to folding the object but my intent was to fold the entire array. An annoying workaround is to enter a new line manually before folding. The current version looks prettier so I would like it to be an option rather than the default.

Input

The code looked like this before beautification:

{"stuff":[{"thing":"other thing"},{"thing":"other thing"}]}

Expected Output

The code should have looked like this after beautification:

{
  "stuff": [
    {
      "thing": "other thing"
    }, {
      "thing": "other thing"
    }
  ]
}

Actual Output

The code actually looked like this after beautification:

{
  "stuff": [{
    "thing": "other thing"
  }, {
    "thing": "other thing"
  }]
}

Steps to Reproduce

I used indent_size: 2 as the only option. Can be reproduced on http://jsbeautifier.org/ I'm using js-beautify 1.7.5 from npm.

bitwiseman commented 6 years ago

If you set brace-style to expand, you get closer to what you're looking for, but still not quite it:

{
  "stuff": [
  {
    "thing": "other thing"
  },
  {
    "thing": "other thing"
  }]
}

But actually, this is a bug, because expand should produce the expected output.

rinu commented 6 years ago

Even if the bug gets fixed, expand is still not perfect for this use case. Because braces always start on a new line, even if there is no array around. This I assume is not a bug but expected for expand

{"key1":{"i": "o"}}

Becomes

{
  "key1":
  {
    "i": "o"
  }
}

Rather than

{
  "key1": {
    "i": "o"
  }
}
bitwiseman commented 6 years ago

@rinu Yes, you're correct. What you're describing is an enhancement.

Huachao commented 5 years ago

@bitwiseman any updates on this issue?

bitwiseman commented 5 years ago

@Huachao The linked bug provides interesting context for this issue. Thanks! This enhancement is not high priority for the project. If you are interested in it, your help would be appreciated.

stevekanter commented 4 years ago

Does anyone expect this issue to ever get fixed? It's the ONE issue I have with js-beautify, unfortunately.

bitwiseman commented 4 years ago

@stevekanter-arcticleaf Nothing has changed. This project currently has very few contributors, so issues remain unfixed. If you'd like to take a swing at fixing this issue, come to the gitter channel and lets talk about what you'd need to do to make this happen.