Xo: Lax comma-dangle

Created on 3 Jul 2016  ยท  9Comments  ยท  Source: xojs/xo

ESLint 3.0.0 just disabled this rule in their recommended config:

comma-dangle used to be recommended because Internet Explorer 8 and earlier threw a syntax error when it found a dangling comma on object literal properties. However, Internet Explorer 8 was end-of-lifed in January 2016 and all other active browsers allow dangling commas. As such, we consider dangling commas to now be a stylistic issue instead of a possible error.

I'd suggest to follow suit and drop the error. Dangling commas are supported in arrays, object, and hopefully soon even in function parameters. They also make git diffs smaller:

[
    "one"
]

add "two":

[
-   "one"
+   "one",
+   "two",
]

but with a dangling comma:

[
    "one",
]

becomes:

[
    "one",
+   "two",
]

For this reason I'd prefer the option ["error", "always-multiline"] but ["error", "only-multiline"] would be _lax_ enough.

Edit: more reasons below

Most helpful comment

@kevva the point of this issue is to change your opinion on whether you should use the dangle or not.

Besides git, this is also what you can do:

  • move items freely

move

  • remove items freely

remove

  • Consistently edit multiple lines at once

multi-line actions

Many practical reasons to use dangling commas, yet we're stuck with "I don't like the way it looks"

All 9 comments

Personally, I'm ๐Ÿ‘Ž .

I find comma dangling to be weird, and I don't feel the occasional line saved in a diff is worth peppering my code with weirdness. ESLint only removed it from the recommended config because it's changed from a potential error to an entirely stylistic choice. XO enforces tons of stylistic choices on it's users.

If we do decide to do this, then we should go all in with always-multiline. Allowing a mixture in your codebase is just anarchy.

You can always override this in your package.json:

{
  "xo": {
    "rules" : {
      "comma-dangle": ["error", "always-multiline"]
    }
  }
}

Yeah, I'm fine with stylistic choices, I'm only hoping that it was done to avoid syntax errors.

More than just diffs, it's about being able to _copy_, _paste_, _remove_, _add_, _move_ any line, without having to worry about missing commas here and there, which always makes me waste 2-10 seconds. It's part of the reason why I dislike editing JSON

I'm not a fan of dangling comma's either so I'm ๐Ÿ‘Ž on this one. But I'm always open for discussion :).

Dangling commas might look a bit weird at first, it's true, but I thought of this: (forget code style preconceptions for a moment)

// pardon the lack of spaces
[0,0,0,0]

^ No dangling comma. Looks beautiful, commas are symmetrical and natural.

[0,0,0,0,]

^ hmmm. So-so.

[
    0
]

^ Multiline, single element. You wouldn't do that but it's still beautiful, symmetrical and it's valid in xo.

[
    0,
    0,
    0,
    0,
    0
]

^ Multiline, multiple elements, no dangle. That final element feels off. It doesn't match the lines above. It looks like you forgot the period at the end of a sentence or the semicolon at the end of a line.

[
    0,
    0,
    0,
    0,
    0,
]

^ Read them line by line and forget the brackets. Finally they all match! ๐ŸŽ‰

So:

// โŒ
[0,0,0,0,]

// โœ…
[
    0,
    0,
    0,
    0,
    0,
]

Now, always-multiline means that this also needs a comma:

[
    0,
]

So maybe I'd go for only-multiline or perhaps suggest an option like always-multiline-multielements to eslint

@SamVerschueren could you explain why?

@bfred-it
always-mulitline is there specifically against diffs
The only reason someone would have a single value multi line array or object is for later additions.
Exactly the reason for it.

let array = [
  'world!',
];

Later...

let array = [
  'Hello',
  'world!',
];

The only change will be of adding 'Hello', as it should be.
Styling should not constantly be haunting git commits.

I'm ๐Ÿ‘Ž on "comma-dangle": ["error", "always-multiline"] or disabling it since I'd still want xo to tell me if I've left a comma dangle somewhere.

The only non stylistic argument for it are git diffs which I personally never minded.

I'm with @kevva on this one.

@kevva the point of this issue is to change your opinion on whether you should use the dangle or not.

Besides git, this is also what you can do:

  • move items freely

move

  • remove items freely

remove

  • Consistently edit multiple lines at once

multi-line actions

Many practical reasons to use dangling commas, yet we're stuck with "I don't like the way it looks"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EdJoPaTo picture EdJoPaTo  ยท  3Comments

niftylettuce picture niftylettuce  ยท  6Comments

Igor-Scekic picture Igor-Scekic  ยท  3Comments

SamVerschueren picture SamVerschueren  ยท  4Comments

tony-kerz picture tony-kerz  ยท  3Comments