2 * @fileoverview Comma style - enforces comma styles of two types: last and first
3 * @author Vignesh Anand aka vegetableman
8 const astUtils
= require("./utils/ast-utils");
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
19 description
: "enforce consistent comma style",
21 url
: "https://eslint.org/docs/rules/comma-style"
28 enum: ["first", "last"]
35 additionalProperties
: {
40 additionalProperties
: false
45 unexpectedLineBeforeAndAfterComma
: "Bad line breaking before and after ','.",
46 expectedCommaFirst
: "',' should be placed first.",
47 expectedCommaLast
: "',' should be placed last."
52 const style
= context
.options
[0] || "last",
53 sourceCode
= context
.getSourceCode();
56 ArrowFunctionExpression
: true,
58 FunctionDeclaration
: true,
59 FunctionExpression
: true,
60 ImportDeclaration
: true,
65 if (context
.options
.length
=== 2 && Object
.prototype.hasOwnProperty
.call(context
.options
[1], "exceptions")) {
66 const keys
= Object
.keys(context
.options
[1].exceptions
);
68 for (let i
= 0; i
< keys
.length
; i
++) {
69 exceptions
[keys
[i
]] = context
.options
[1].exceptions
[keys
[i
]];
73 //--------------------------------------------------------------------------
75 //--------------------------------------------------------------------------
78 * Modified text based on the style
79 * @param {string} styleType Style type
80 * @param {string} text Source code text
81 * @returns {string} modified text
84 function getReplacedText(styleType
, text
) {
87 return `,${text.replace(astUtils.LINEBREAK_MATCHER, "")}`;
101 * Determines the fixer function for a given style.
102 * @param {string} styleType comma style
103 * @param {ASTNode} previousItemToken The token to check.
104 * @param {ASTNode} commaToken The token to check.
105 * @param {ASTNode} currentItemToken The token to check.
106 * @returns {Function} Fixer function
109 function getFixerFunction(styleType
, previousItemToken
, commaToken
, currentItemToken
) {
111 sourceCode
.text
.slice(previousItemToken
.range
[1], commaToken
.range
[0]) +
112 sourceCode
.text
.slice(commaToken
.range
[1], currentItemToken
.range
[0]);
113 const range
= [previousItemToken
.range
[1], currentItemToken
.range
[0]];
115 return function(fixer
) {
116 return fixer
.replaceTextRange(range
, getReplacedText(styleType
, text
));
121 * Validates the spacing around single items in lists.
122 * @param {Token} previousItemToken The last token from the previous item.
123 * @param {Token} commaToken The token representing the comma.
124 * @param {Token} currentItemToken The first token of the current item.
125 * @param {Token} reportItem The item to use when reporting an error.
129 function validateCommaItemSpacing(previousItemToken
, commaToken
, currentItemToken
, reportItem
) {
132 if (astUtils
.isTokenOnSameLine(commaToken
, currentItemToken
) &&
133 astUtils
.isTokenOnSameLine(previousItemToken
, commaToken
)) {
137 } else if (!astUtils
.isTokenOnSameLine(commaToken
, currentItemToken
) &&
138 !astUtils
.isTokenOnSameLine(previousItemToken
, commaToken
)) {
140 const comment
= sourceCode
.getCommentsAfter(commaToken
)[0];
141 const styleType
= comment
&& comment
.type
=== "Block" && astUtils
.isTokenOnSameLine(commaToken
, comment
)
149 messageId
: "unexpectedLineBeforeAndAfterComma",
150 fix
: getFixerFunction(styleType
, previousItemToken
, commaToken
, currentItemToken
)
153 } else if (style
=== "first" && !astUtils
.isTokenOnSameLine(commaToken
, currentItemToken
)) {
158 messageId
: "expectedCommaFirst",
159 fix
: getFixerFunction(style
, previousItemToken
, commaToken
, currentItemToken
)
162 } else if (style
=== "last" && astUtils
.isTokenOnSameLine(commaToken
, currentItemToken
)) {
167 messageId
: "expectedCommaLast",
168 fix
: getFixerFunction(style
, previousItemToken
, commaToken
, currentItemToken
)
174 * Checks the comma placement with regards to a declaration/property/element
175 * @param {ASTNode} node The binary expression node to check
176 * @param {string} property The property of the node containing child nodes.
180 function validateComma(node
, property
) {
181 const items
= node
[property
],
182 arrayLiteral
= (node
.type
=== "ArrayExpression" || node
.type
=== "ArrayPattern");
184 if (items
.length
> 1 || arrayLiteral
) {
187 let previousItemToken
= sourceCode
.getFirstToken(node
);
189 items
.forEach(item
=> {
190 const commaToken
= item
? sourceCode
.getTokenBefore(item
) : previousItemToken
,
191 currentItemToken
= item
? sourceCode
.getFirstToken(item
) : sourceCode
.getTokenAfter(commaToken
),
192 reportItem
= item
|| currentItemToken
;
195 * This works by comparing three token locations:
196 * - previousItemToken is the last token of the previous item
197 * - commaToken is the location of the comma before the current item
198 * - currentItemToken is the first token of the current item
200 * These values get switched around if item is undefined.
201 * previousItemToken will refer to the last token not belonging
202 * to the current item, which could be a comma or an opening
203 * square bracket. currentItemToken could be a comma.
205 * All comparisons are done based on these tokens directly, so
206 * they are always valid regardless of an undefined item.
208 if (astUtils
.isCommaToken(commaToken
)) {
209 validateCommaItemSpacing(previousItemToken
, commaToken
, currentItemToken
, reportItem
);
213 const tokenAfterItem
= sourceCode
.getTokenAfter(item
, astUtils
.isNotClosingParenToken
);
215 previousItemToken
= tokenAfterItem
216 ? sourceCode
.getTokenBefore(tokenAfterItem
)
217 : sourceCode
.ast
.tokens
[sourceCode
.ast
.tokens
.length
- 1];
219 previousItemToken
= currentItemToken
;
224 * Special case for array literals that have empty last items, such
225 * as [ 1, 2, ]. These arrays only have two items show up in the
226 * AST, so we need to look at the token to verify that there's no
231 const lastToken
= sourceCode
.getLastToken(node
),
232 nextToLastToken
= sourceCode
.getTokenBefore(lastToken
);
234 if (astUtils
.isCommaToken(nextToLastToken
)) {
235 validateCommaItemSpacing(
236 sourceCode
.getTokenBefore(nextToLastToken
),
246 //--------------------------------------------------------------------------
248 //--------------------------------------------------------------------------
252 if (!exceptions
.VariableDeclaration
) {
253 nodes
.VariableDeclaration = function(node
) {
254 validateComma(node
, "declarations");
257 if (!exceptions
.ObjectExpression
) {
258 nodes
.ObjectExpression = function(node
) {
259 validateComma(node
, "properties");
262 if (!exceptions
.ObjectPattern
) {
263 nodes
.ObjectPattern = function(node
) {
264 validateComma(node
, "properties");
267 if (!exceptions
.ArrayExpression
) {
268 nodes
.ArrayExpression = function(node
) {
269 validateComma(node
, "elements");
272 if (!exceptions
.ArrayPattern
) {
273 nodes
.ArrayPattern = function(node
) {
274 validateComma(node
, "elements");
277 if (!exceptions
.FunctionDeclaration
) {
278 nodes
.FunctionDeclaration = function(node
) {
279 validateComma(node
, "params");
282 if (!exceptions
.FunctionExpression
) {
283 nodes
.FunctionExpression = function(node
) {
284 validateComma(node
, "params");
287 if (!exceptions
.ArrowFunctionExpression
) {
288 nodes
.ArrowFunctionExpression = function(node
) {
289 validateComma(node
, "params");
292 if (!exceptions
.CallExpression
) {
293 nodes
.CallExpression = function(node
) {
294 validateComma(node
, "arguments");
297 if (!exceptions
.ImportDeclaration
) {
298 nodes
.ImportDeclaration = function(node
) {
299 validateComma(node
, "specifiers");
302 if (!exceptions
.NewExpression
) {
303 nodes
.NewExpression = function(node
) {
304 validateComma(node
, "arguments");