Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upRequire trailing commas in multiline object and array literals? #240
Comments
This comment has been minimized.
This comment has been minimized.
|
Although personally I agree with the fundamental reasons that you argue for this. As it stands, this would be so much of a breaking change, it will never be accepted. |
dcousens
added
the
question
label
Aug 29, 2015
This comment has been minimized.
This comment has been minimized.
|
Thanks for the thoughtful issue! Unfortunately, it would be a lot of work for all the existing users of standard (almost 100K downloads per month) to change their code. We can't really make breaking changes at this point without a really good reason. Fortunately, this style choice isn't the most important since it doesn't affect code correctness or make programmers more likely to write bugs. At the end of the day you have to 'just pick something', and that's the whole philosophy of standard -- its a bunch of sensible 'just pick something' opinions. |
feross
closed this
Aug 30, 2015
This comment has been minimized.
This comment has been minimized.
|
Right, thanks for taking your time to read and answer! |
This comment has been minimized.
This comment has been minimized.
yanivtal
commented
Jan 8, 2016
|
I agree with the philosophy of this approach... and yet this choice will be responsible for millions of lines of diffs based on the number of downloads per month... |
This comment has been minimized.
This comment has been minimized.
|
@yanivtal is the upgrade diff cost less than than the diff cost of leaving it the same for say, 5 years? |
This comment has been minimized.
This comment has been minimized.
gimenete
commented
Jan 14, 2016
|
Instead of making it mandatory at least it could stop marking it as an error. Now it says So my suggestion is: no error if you use it and no error if you don't. What do you think guys? |
This comment has been minimized.
This comment has been minimized.
yanivtal
commented
Jan 14, 2016
|
i like that suggestion. but regardless, @dcousens good answer ;) |
This comment has been minimized.
This comment has been minimized.
gimenete
commented
Jan 15, 2016
|
As you can see here eslint/eslint#4968 I've made a PR to |
This comment has been minimized.
This comment has been minimized.
mattdesl
commented
Apr 12, 2016
Some posts on the subject. https://medium.com/@nikgraf/why-you-should-enforce-dangling-commas-for-multiline-statements-d034c98e36f8#.yndunrwhf |
This comment has been minimized.
This comment has been minimized.
|
The issue with relaxing the rule is that some places in the codebase will have it, some won't. That will lead to discussion about the issue in PRs, edit wars, etc. |
This comment has been minimized.
This comment has been minimized.
ianstormtaylor
commented
Jun 6, 2016
|
Every other rule seems super sensible, and tends towards consistency. And not opting to make trailing commas optional means that people have to hack around the defaults or adopt the poor decision in their codebases... seems weird given that it seems like everyone admits this was a poor decision from the start that would be changed if only it didn't cause extra work. This is the only rule I disagree with in |
This comment has been minimized.
This comment has been minimized.
|
If you feel that strongly about a single style rule like trailing commas, then |
This comment has been minimized.
This comment has been minimized.
jokeyrhyme
commented
Jun 27, 2016
|
Note that ESLint's |
This comment has been minimized.
This comment has been minimized.
jokeyrhyme
commented
Jun 27, 2016
|
There's a thorough discussion about this subject here: https://medium.com/@nikgraf/why-you-should-enforce-dangling-commas-for-multiline-statements-d034c98e36f8 |
This comment has been minimized.
This comment has been minimized.
and
Are simply not compatible, |
This comment has been minimized.
This comment has been minimized.
lucasrcosta
commented
Jul 13, 2016
•
|
2 cents here: the diff things is not as important for me as the Easier Code-Manipulations argument, which seems to be apart from this discussion. How many objects and arrays are multilined in everyone's code? Have you never had to change the order or sort some properties and spend time fixing the comma? I sure did. |
This comment has been minimized.
This comment has been minimized.
jokeyrhyme
commented
Jul 13, 2016
|
FYI: Upstream ESLint have moved "comma-dangle" from Possible Errors to Stylistic Issues, and also removed it from the This helps enforce the notion that this is a style thing, and Standard is a standard for code style. |
This comment has been minimized.
This comment has been minimized.
I concur, I have spent some unprecedented amount of time on this, it is especially bad when dealing with humongous JSON files that might lag out your editor. |
This comment has been minimized.
This comment has been minimized.
jokeyrhyme
commented
Jul 13, 2016
|
@dcousens the JSON specification does not allow trailing commas: http://www.json.org/ JSON !== JavaScript, and I'm not sure JavaScript Standard Code Style covers JSON. But for Objects and Arrays in JavaScript, I do not disagree with you. |
This comment has been minimized.
This comment has been minimized.
|
@jokeyrhyme true indeed! Welp. I've returned to not really caring about this. |
lydell commentedAug 29, 2015
•
edited
This is a very sensible styleguide. I don’t agree with all of it personally on a purely aesthetical level, but don’t care since I think it’s worth it because of the simplicity.
Here’s another sensible rule, that I think is a tiny bit ugly myself, but is very useful: Require trailing commas in multiline object and array literals. Why? Because it reduces diffs. When adding or removing a new item at the end of the literal you only need to modify one line instead of two.
The above examples can be extended with multiple items per literal, of course, and the same apply to array literals.
This can be implemented by setting the comma-dangle rule to
[2, "always-multiline"]. As described in the comma-dangle docs, the only caveat is IE8 in certain circumstances. In my opinion you should have your build tool take care of that. Most likely you’ll minify the code, and then trailing commas will be removed anyway to reduce size. EDIT: IE8 isn't even supported by Microsoft anymore.EDIT: As mentioned in this comment, another benefit (besides diffing and
git blame-ing) is ease of editing: Want to move the last line? Be sure to add in that comma!