]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/lib/rules/comma-spacing.js
import 8.23.1 source
[pve-eslint.git] / eslint / lib / rules / comma-spacing.js
index 23a517528149833b48d0fce1820c328b446ffbf2..76d5dc46b9c8f17721a8346257b1d81059c22583 100644 (file)
@@ -16,7 +16,7 @@ module.exports = {
         type: "layout",
 
         docs: {
-            description: "enforce consistent spacing before and after commas",
+            description: "Enforce consistent spacing before and after commas",
             recommended: false,
             url: "https://eslint.org/docs/rules/comma-spacing"
         },
@@ -103,38 +103,6 @@ module.exports = {
             });
         }
 
-        /**
-         * Validates the spacing around a comma token.
-         * @param {Object} tokens The tokens to be validated.
-         * @param {Token} tokens.comma The token representing the comma.
-         * @param {Token} [tokens.left] The last token before the comma.
-         * @param {Token} [tokens.right] The first token after the comma.
-         * @param {Token|ASTNode} reportItem The item to use when reporting an error.
-         * @returns {void}
-         * @private
-         */
-        function validateCommaItemSpacing(tokens, reportItem) {
-            if (tokens.left && astUtils.isTokenOnSameLine(tokens.left, tokens.comma) &&
-                    (options.before !== sourceCode.isSpaceBetweenTokens(tokens.left, tokens.comma))
-            ) {
-                report(reportItem, "before", tokens.left);
-            }
-
-            if (tokens.right && astUtils.isClosingParenToken(tokens.right)) {
-                return;
-            }
-
-            if (tokens.right && !options.after && tokens.right.type === "Line") {
-                return;
-            }
-
-            if (tokens.right && astUtils.isTokenOnSameLine(tokens.comma, tokens.right) &&
-                    (options.after !== sourceCode.isSpaceBetweenTokens(tokens.comma, tokens.right))
-            ) {
-                report(reportItem, "after", tokens.right);
-            }
-        }
-
         /**
          * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
          * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
@@ -172,18 +140,44 @@ module.exports = {
                         return;
                     }
 
-                    if (token && token.type === "JSXText") {
-                        return;
-                    }
-
                     const previousToken = tokensAndComments[i - 1];
                     const nextToken = tokensAndComments[i + 1];
 
-                    validateCommaItemSpacing({
-                        comma: token,
-                        left: astUtils.isCommaToken(previousToken) || commaTokensToIgnore.includes(token) ? null : previousToken,
-                        right: astUtils.isCommaToken(nextToken) ? null : nextToken
-                    }, token);
+                    if (
+                        previousToken &&
+                        !astUtils.isCommaToken(previousToken) && // ignore spacing between two commas
+
+                        /*
+                         * `commaTokensToIgnore` are ending commas of `null` elements (array holes/elisions).
+                         * In addition to spacing between two commas, this can also ignore:
+                         *
+                         *   - Spacing after `[` (controlled by array-bracket-spacing)
+                         *       Example: [ , ]
+                         *                 ^
+                         *   - Spacing after a comment (for backwards compatibility, this was possibly unintentional)
+                         *       Example: [a, /* * / ,]
+                         *                          ^
+                         */
+                        !commaTokensToIgnore.includes(token) &&
+
+                        astUtils.isTokenOnSameLine(previousToken, token) &&
+                        options.before !== sourceCode.isSpaceBetweenTokens(previousToken, token)
+                    ) {
+                        report(token, "before", previousToken);
+                    }
+
+                    if (
+                        nextToken &&
+                        !astUtils.isCommaToken(nextToken) && // ignore spacing between two commas
+                        !astUtils.isClosingParenToken(nextToken) && // controlled by space-in-parens
+                        !astUtils.isClosingBracketToken(nextToken) && // controlled by array-bracket-spacing
+                        !astUtils.isClosingBraceToken(nextToken) && // controlled by object-curly-spacing
+                        !(!options.after && nextToken.type === "Line") && // special case, allow space before line comment
+                        astUtils.isTokenOnSameLine(token, nextToken) &&
+                        options.after !== sourceCode.isSpaceBetweenTokens(token, nextToken)
+                    ) {
+                        report(token, "after", nextToken);
+                    }
                 });
             },
             ArrayExpression: addNullElementsToIgnoreList,