1 # Comma style (comma-style)
3 The Comma Style rule enforces styles for comma-separated lists. There are two comma styles primarily used in JavaScript:
5 * The standard style, in which commas are placed at the end of the current line
6 * Comma First style, in which commas are placed at the start of the next line
8 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.
12 This rule enforce consistent comma style in array literals, object literals, and variable declarations.
14 This rule does not apply in either of the following cases:
16 * comma preceded and followed by linebreak (lone comma)
17 * single-line array literals, object literals, and variable declarations
21 This rule has a string option:
23 * `"last"` (default) requires a comma after and on the same line as an array element, object property, or variable declaration
24 * `"first"` requires a comma before and on the same line as an array element, object property, or variable declaration
26 This rule also accepts an additional `exceptions` object:
28 * `"exceptions"` has properties whose names correspond to node types in the abstract syntax tree (AST) of JavaScript code:
30 * `"ArrayExpression": true` ignores comma style in array literals
31 * `"ArrayPattern": true` ignores comma style in array patterns of destructuring
32 * `"ArrowFunctionExpression": true` ignores comma style in the parameters of arrow function expressions
33 * `"CallExpression": true` ignores comma style in the arguments of function calls
34 * `"FunctionDeclaration": true` ignores comma style in the parameters of function declarations
35 * `"FunctionExpression": true` ignores comma style in the parameters of function expressions
36 * `"ImportDeclaration": true` ignores comma style in the specifiers of import declarations
37 * `"ObjectExpression": true` ignores comma style in object literals
38 * `"ObjectPattern": true` ignores comma style in object patterns of destructuring
39 * `"VariableDeclaration": true` ignores comma style in variable declarations
40 * `"NewExpression": true` ignores comma style in the parameters of constructor expressions
42 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.
46 Examples of **incorrect** code for this rule with the default `"last"` option:
49 /*eslint comma-style: ["error", "last"]*/
69 Examples of **correct** code for this rule with the default `"last"` option:
72 /*eslint comma-style: ["error", "last"]*/
92 Examples of **incorrect** code for this rule with the `"first"` option:
95 /*eslint comma-style: ["error", "first"]*/
111 Examples of **correct** code for this rule with the `"first"` option:
114 /*eslint comma-style: ["error", "first"]*/
116 var foo = 1, bar = 2;
134 An example use case is to enforce comma style *only* in var statements.
136 Examples of **incorrect** code for this rule with sample `"first", { "exceptions": { … } }` options:
139 /*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/
145 Examples of **correct** code for this rule with sample `"first", { "exceptions": { … } }` options:
148 /*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/
156 ## When Not To Use It
158 This rule can safely be turned off if your project does not care about enforcing a consistent comma style.
162 For more information on the Comma First style:
164 * [A better coding convention for lists and object literals in JavaScript by isaacs](https://gist.github.com/isaacs/357981)
165 * [npm coding style guideline](https://docs.npmjs.com/misc/coding-style)
169 * [operator-linebreak](operator-linebreak.md)