beautifier / js-beautify

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

Inline Objects/Arrays being joined with previous line in multi-arg function calls #493

Open KylePDavis opened 10 years ago

KylePDavis commented 10 years ago

In the case of multiple arguments to a function call like this:

test(
  {
    foo: "bar"
  },
  {
    baz: "qux"
  }
)

... it produces:

test({
  foo: "bar",
}, {
  baz: "qux"
})

However, when using simple values like this:

test(
  "foo",
  "bar"
)

... it leaves the multiline formatting in tact like you'd expect.

Then, things get really weird when you mix simple values and objects on multiple lines like this:

test(
  1,
  {
    foo: "bar"
  },
  3
)

... it gives something like this:

test(
  1, {
    foo: "bar"
  },
  3
)

You get similar behavior when you mix arrays in, too, so for something like this:

test(
  1,
  {
    foo: "bar"
  },
  [
    "baz",
    "qux"
  ],
  4
)

... it produces this:

test(
  1, {
    foo: "bar"
  }, [
    "baz",
    "qux"
  ],
  4
)

I'm sure that there are some people who prefer this styling but it rattles my sense of balance. I'd much prefer if I could keep the alignment of the opening and closing characters.

Maybe we could create an option to suppress this line joining behavior for multi-arg function calls.

bitwiseman commented 10 years ago

I agree it is hard to claim beautification in this case. There should be a better way and better output.

In the next release, I plan to implement "one-line objects" - #315. Some improved processing for multiline expressions like these may fall out of that effort.