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",
19 category: "Stylistic Issues",
21 url: "https://eslint.org/docs/rules/comma-spacing"
24 fixable: "whitespace",
39 additionalProperties: false
44 missing: "A space is required {{loc}} ','.",
45 unexpected: "There should be no space {{loc}} ','."
51 const sourceCode = context.getSourceCode();
52 const tokensAndComments = sourceCode.tokensAndComments;
55 before: context.options[0] ? context.options[0].before : false,
56 after: context.options[0] ? context.options[0].after : true
59 //--------------------------------------------------------------------------
61 //--------------------------------------------------------------------------
63 // list of comma tokens to ignore for the check of leading whitespace
64 const commaTokensToIgnore = [];
67 * Reports a spacing error with an appropriate message.
68 * @param {ASTNode} node The binary expression node to report.
69 * @param {string} loc Is the error "before" or "after" the comma?
70 * @param {ASTNode} otherNode The node at the left or right of `node`
74 function report(node, loc, otherNode) {
79 if (loc === "before") {
80 return fixer.insertTextBefore(node, " ");
82 return fixer.insertTextAfter(node, " ");
88 if (loc === "before") {
89 start = otherNode.range[1];
92 start = node.range[1];
93 end = otherNode.range[0];
96 return fixer.replaceTextRange([start, end], newText);
99 messageId: options[loc] ? "missing" : "unexpected",
107 * Validates the spacing around a comma token.
108 * @param {Object} tokens The tokens to be validated.
109 * @param {Token} tokens.comma The token representing the comma.
110 * @param {Token} [tokens.left] The last token before the comma.
111 * @param {Token} [tokens.right] The first token after the comma.
112 * @param {Token|ASTNode} reportItem The item to use when reporting an error.
116 function validateCommaItemSpacing(tokens, reportItem) {
117 if (tokens.left && astUtils.isTokenOnSameLine(tokens.left, tokens.comma) &&
118 (options.before !== sourceCode.isSpaceBetweenTokens(tokens.left, tokens.comma))
120 report(reportItem, "before", tokens.left);
123 if (tokens.right && astUtils.isClosingParenToken(tokens.right)) {
127 if (tokens.right && !options.after && tokens.right.type === "Line") {
131 if (tokens.right && astUtils.isTokenOnSameLine(tokens.comma, tokens.right) &&
132 (options.after !== sourceCode.isSpaceBetweenTokens(tokens.comma, tokens.right))
134 report(reportItem, "after", tokens.right);
139 * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
140 * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
143 function addNullElementsToIgnoreList(node) {
144 let previousToken = sourceCode.getFirstToken(node);
146 node.elements.forEach(element => {
149 if (element === null) {
150 token = sourceCode.getTokenAfter(previousToken);
152 if (astUtils.isCommaToken(token)) {
153 commaTokensToIgnore.push(token);
156 token = sourceCode.getTokenAfter(element);
159 previousToken = token;
163 //--------------------------------------------------------------------------
165 //--------------------------------------------------------------------------
169 tokensAndComments.forEach((token, i) => {
171 if (!astUtils.isCommaToken(token)) {
175 if (token && token.type === "JSXText") {
179 const previousToken = tokensAndComments[i - 1];
180 const nextToken = tokensAndComments[i + 1];
182 validateCommaItemSpacing({
184 left: astUtils.isCommaToken(previousToken) || commaTokensToIgnore.includes(token) ? null : previousToken,
185 right: astUtils.isCommaToken(nextToken) ? null : nextToken
189 ArrayExpression: addNullElementsToIgnoreList,
190 ArrayPattern: addNullElementsToIgnoreList