2 * @fileoverview Comma spacing - validates spacing before and after comma
3 * @author Vignesh Anand aka vegetableman.
7 const astUtils
= require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description
: "enforce consistent spacing before and after commas",
20 url
: "https://eslint.org/docs/rules/comma-spacing"
23 fixable
: "whitespace",
38 additionalProperties
: false
43 missing
: "A space is required {{loc}} ','.",
44 unexpected
: "There should be no space {{loc}} ','."
50 const sourceCode
= context
.getSourceCode();
51 const tokensAndComments
= sourceCode
.tokensAndComments
;
54 before
: context
.options
[0] ? context
.options
[0].before
: false,
55 after
: context
.options
[0] ? context
.options
[0].after
: true
58 //--------------------------------------------------------------------------
60 //--------------------------------------------------------------------------
62 // list of comma tokens to ignore for the check of leading whitespace
63 const commaTokensToIgnore
= [];
66 * Reports a spacing error with an appropriate message.
67 * @param {ASTNode} node The binary expression node to report.
68 * @param {string} loc Is the error "before" or "after" the comma?
69 * @param {ASTNode} otherNode The node at the left or right of `node`
73 function report(node
, loc
, otherNode
) {
78 if (loc
=== "before") {
79 return fixer
.insertTextBefore(node
, " ");
81 return fixer
.insertTextAfter(node
, " ");
87 if (loc
=== "before") {
88 start
= otherNode
.range
[1];
91 start
= node
.range
[1];
92 end
= otherNode
.range
[0];
95 return fixer
.replaceTextRange([start
, end
], newText
);
98 messageId
: options
[loc
] ? "missing" : "unexpected",
106 * Validates the spacing around a comma token.
107 * @param {Object} tokens The tokens to be validated.
108 * @param {Token} tokens.comma The token representing the comma.
109 * @param {Token} [tokens.left] The last token before the comma.
110 * @param {Token} [tokens.right] The first token after the comma.
111 * @param {Token|ASTNode} reportItem The item to use when reporting an error.
115 function validateCommaItemSpacing(tokens
, reportItem
) {
116 if (tokens
.left
&& astUtils
.isTokenOnSameLine(tokens
.left
, tokens
.comma
) &&
117 (options
.before
!== sourceCode
.isSpaceBetweenTokens(tokens
.left
, tokens
.comma
))
119 report(reportItem
, "before", tokens
.left
);
122 if (tokens
.right
&& astUtils
.isClosingParenToken(tokens
.right
)) {
126 if (tokens
.right
&& !options
.after
&& tokens
.right
.type
=== "Line") {
130 if (tokens
.right
&& astUtils
.isTokenOnSameLine(tokens
.comma
, tokens
.right
) &&
131 (options
.after
!== sourceCode
.isSpaceBetweenTokens(tokens
.comma
, tokens
.right
))
133 report(reportItem
, "after", tokens
.right
);
138 * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
139 * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
142 function addNullElementsToIgnoreList(node
) {
143 let previousToken
= sourceCode
.getFirstToken(node
);
145 node
.elements
.forEach(element
=> {
148 if (element
=== null) {
149 token
= sourceCode
.getTokenAfter(previousToken
);
151 if (astUtils
.isCommaToken(token
)) {
152 commaTokensToIgnore
.push(token
);
155 token
= sourceCode
.getTokenAfter(element
);
158 previousToken
= token
;
162 //--------------------------------------------------------------------------
164 //--------------------------------------------------------------------------
168 tokensAndComments
.forEach((token
, i
) => {
170 if (!astUtils
.isCommaToken(token
)) {
174 if (token
&& token
.type
=== "JSXText") {
178 const previousToken
= tokensAndComments
[i
- 1];
179 const nextToken
= tokensAndComments
[i
+ 1];
181 validateCommaItemSpacing({
183 left
: astUtils
.isCommaToken(previousToken
) || commaTokensToIgnore
.includes(token
) ? null : previousToken
,
184 right
: astUtils
.isCommaToken(nextToken
) ? null : nextToken
188 ArrayExpression
: addNullElementsToIgnoreList
,
189 ArrayPattern
: addNullElementsToIgnoreList