8 - https://gist.github.com/isaacs/357981
13 The Comma Style rule enforces styles for comma-separated lists. There are two comma styles primarily used in JavaScript:
15 * The standard style, in which commas are placed at the end of the current line
16 * Comma First style, in which commas are placed at the start of the next line
18 One of the justifications for using Comma First style is that it can help track missing and trailing commas. These are problematic because missing commas in variable declarations can lead to the leakage of global variables and trailing commas can lead to errors in older versions of IE.
22 This rule enforce consistent comma style in array literals, object literals, and variable declarations.
24 This rule does not apply in either of the following cases:
26 * comma preceded and followed by linebreak (lone comma)
27 * single-line array literals, object literals, and variable declarations
31 This rule has a string option:
33 * `"last"` (default) requires a comma after and on the same line as an array element, object property, or variable declaration
34 * `"first"` requires a comma before and on the same line as an array element, object property, or variable declaration
36 This rule also accepts an additional `exceptions` object:
38 * `"exceptions"` has properties whose names correspond to node types in the abstract syntax tree (AST) of JavaScript code:
40 * `"ArrayExpression": true` ignores comma style in array literals
41 * `"ArrayPattern": true` ignores comma style in array patterns of destructuring
42 * `"ArrowFunctionExpression": true` ignores comma style in the parameters of arrow function expressions
43 * `"CallExpression": true` ignores comma style in the arguments of function calls
44 * `"FunctionDeclaration": true` ignores comma style in the parameters of function declarations
45 * `"FunctionExpression": true` ignores comma style in the parameters of function expressions
46 * `"ImportDeclaration": true` ignores comma style in the specifiers of import declarations
47 * `"ObjectExpression": true` ignores comma style in object literals
48 * `"ObjectPattern": true` ignores comma style in object patterns of destructuring
49 * `"VariableDeclaration": true` ignores comma style in variable declarations
50 * `"NewExpression": true` ignores comma style in the parameters of constructor expressions
52 A way to determine the node types as defined by [ESTree](https://github.com/estree/estree) is to use [AST Explorer](https://astexplorer.net/) with the espree parser.
56 Examples of **incorrect** code for this rule with the default `"last"` option:
61 /*eslint comma-style: ["error", "last"]*/
83 Examples of **correct** code for this rule with the default `"last"` option:
88 /*eslint comma-style: ["error", "last"]*/
110 Examples of **incorrect** code for this rule with the `"first"` option:
115 /*eslint comma-style: ["error", "first"]*/
133 Examples of **correct** code for this rule with the `"first"` option:
138 /*eslint comma-style: ["error", "first"]*/
140 var foo = 1, bar = 2;
160 An example use case is to enforce comma style *only* in var statements.
162 Examples of **incorrect** code for this rule with sample `"first", { "exceptions": { … } }` options:
167 /*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/
175 Examples of **correct** code for this rule with sample `"first", { "exceptions": { … } }` options:
180 /*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/
190 ## When Not To Use It
192 This rule can safely be turned off if your project does not care about enforcing a consistent comma style.