Proposal: Require trailing commas for multiline objects
robertrossmann opened this issue · 4 comments
I propose that we start requiring trailing commas for multiline objects. Currently trailing commas are optional. Single-line object literals will not be affected by this change.
This rule can be auto-fixed by ESLint, therefore no manual changes are required in your code.
Current
const obj = {
first: 1,
second: 2 // Here, the comma is optional
}New
const obj = {
first: 1,
second: 2, // Here, the comma is required
}Why?
- Every time you want to add a new property to an object, you have to remember to put the trailing comma to the preceding property, otherwise you get a syntax error. Enforcing the comma to already be there, you mitigate this completely.
git diffs will look more clean, because you now do not have to edit an unrelated line to introduce your changes.- It is easier to sort the object's properties with just
F5or similar editor tool
Please vote 👍 or 👎 via reaction or leave a comment if you have questions or suggestions. Thanks!
Counter proposal:
const obj = {
first: 1
,second: 2
,third: 3
,forth: 4
}
P.S I am serious
I think this will not help with lines sorting :(
@Milan4e I actually wrote code like that some time ago! I personally liked that style, but the problem was with tooling - some tools (ie. docblock parsers) do not cope well with the comma being at the next line. Example:
const obj = {
/**
* Document for first
* @type {String}
*/
first: 'test'
/**
* A trailing docblock for first!
* @type {String}
*/
, second: 'test again'
}Next major version (6.0) of js-coding-standards will require trailing commas for multiline object/array expressions.
Please note that this rule can be autofixed by ESLint itself - updating your codebase should therefore be as trivial as running node_modules/.bin/eslint --fix . from your project's directory.
Thank you all for your feedback!