beautifier / js-beautify

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

Ability to have newlines inside array? #379

Open BubonicPestilence opened 10 years ago

BubonicPestilence commented 10 years ago

Hello guys.

I'll be glad to see option "elements of array on new line" in close future.

Example:

Source:
{"explicitMods": ["Adds 7-15 Physical Damage", "+27 to Dexterity", "7% increased Fire Damage", "+16 to Evasion Rating", "+4 Life gained on Kill"]}

Current Output:

{
  "explicitMods": ["Adds 7-15 Physical Damage", "+27 to Dexterity", "7% increased Fire Damage", "+16 to Evasion Rating", "+4 Life gained on Kill"]
}

What I want to see:

{
  "explicitMods": [
    "Adds 7-15 Physical Damage",
    "+27 to Dexterity",
    "7% increased Fire Damage",
    "+16 to Evasion Rating",
    "+4 Life gained on Kill"
  ]
}
evocateur commented 10 years ago

Interestingly, passing 1 to the --wrap-line-length flag (-w for short) achieves what you're asking for.

If your source is located in file.js, then js-beautify -s 2 -w 1 -f file.js outputs this:

{
  "explicitMods": [
    "Adds 7-15 Physical Damage",
    "+27 to Dexterity",
    "7% increased Fire Damage",
    "+16 to Evasion Rating",
    "+4 Life gained on Kill"
  ]
}

I certainly didn't expect that. I'm not sure what effect passing such a low (non-zero) value would have on other types of code, but it's probably undesirable.

BubonicPestilence commented 10 years ago

tyvm, i noticed this option, but didn't tried. Also i think it will affect some other things...

evocateur commented 10 years ago

Indeed, -w is only a workaround for the specific example of code you gave us, and not a general fix. The issue remains open.

On Mon, Jan 6, 2014 at 3:52 PM, Anton Berezhkov notifications@github.com wrote:

tyvm, i noticed this option, but didn't tried.

Also i think it will affect some other things...

Reply to this email directly or view it on GitHub: https://github.com/einars/js-beautify/issues/379#issuecomment-31700413

bitwiseman commented 10 years ago

Also note, that the input below maps to the same output:

{
    "explicitMods": [
        "Adds 7-15 Physical Damage", "+27 to Dexterity", "7% increased Fire Damage",
        "+16 to Evasion Rating", "+4 Life gained on Kill"
    ]
}

But this also gets you:

// input, set wordwrap to 80
{
    "explicitMods": [
        "Adds 7-15 Physical Damage", "+27 to Dexterity", "7% increased Fire Damage", "+16 to Evasion Rating",
        "+4 Life gained on Kill"
    ]
}

// output
{
    "explicitMods": [
        "Adds 7-15 Physical Damage", "+27 to Dexterity",
        "7% increased Fire Damage", "+16 to Evasion Rating",
        "+4 Life gained on Kill"
    ]
}

What I think would make sense is this:

// input, output is the same 
{
    "explicitMods": [ "a", "c", "d"]
}

// input 
{
    "explicitMods": [
      "a", "c", "d"]
}

// output - once we see a in the input newline inside an array, wrap all remaining items
{
    "explicitMods": [
        "a",
        "c",
        "d"
    ]
}

// input 
{
    "explicitMods": ["a", "b",
      "c", "d"]
}

// output (not ideal) - once we see a newline inside an array, wrap all remaining items
// This is the example of where that logic falls down... It's not great, but it allows for the previous input to work
{
    "explicitMods": ["a", "b",
        "c",
        "d"
    ]
}
// input, set wordwrap to 80
{
    "explicitMods": [
        "Adds 7-15 Physical Damage", "+27 to Dexterity", "7% increased Fire Damage", "+16 to Evasion Rating",
        "+4 Life gained on Kill"
    ]
}

// output (ideally) - if we start wrapping an array, go back and put each item on a separate line.
// this part would be hard due to #200 and that we don't back track well 
{
    "explicitMods": [
        "Adds 7-15 Physical Damage",
        "+27 to Dexterity",
        "7% increased Fire Damage",
        "+16 to Evasion Rating",
        "+4 Life gained on Kill"
    ]
}
drastick commented 9 years ago

+1