[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Comma spacing - validates spacing before and after comma
|
---|
| 3 | * @author Vignesh Anand aka vegetableman.
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const astUtils = require("./utils/ast-utils");
|
---|
| 9 |
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 | // Rule Definition
|
---|
| 12 | //------------------------------------------------------------------------------
|
---|
| 13 |
|
---|
| 14 | /** @type {import('../shared/types').Rule} */
|
---|
| 15 | module.exports = {
|
---|
| 16 | meta: {
|
---|
| 17 | deprecated: true,
|
---|
| 18 | replacedBy: [],
|
---|
| 19 | type: "layout",
|
---|
| 20 |
|
---|
| 21 | docs: {
|
---|
| 22 | description: "Enforce consistent spacing before and after commas",
|
---|
| 23 | recommended: false,
|
---|
| 24 | url: "https://eslint.org/docs/latest/rules/comma-spacing"
|
---|
| 25 | },
|
---|
| 26 |
|
---|
| 27 | fixable: "whitespace",
|
---|
| 28 |
|
---|
| 29 | schema: [
|
---|
| 30 | {
|
---|
| 31 | type: "object",
|
---|
| 32 | properties: {
|
---|
| 33 | before: {
|
---|
| 34 | type: "boolean",
|
---|
| 35 | default: false
|
---|
| 36 | },
|
---|
| 37 | after: {
|
---|
| 38 | type: "boolean",
|
---|
| 39 | default: true
|
---|
| 40 | }
|
---|
| 41 | },
|
---|
| 42 | additionalProperties: false
|
---|
| 43 | }
|
---|
| 44 | ],
|
---|
| 45 |
|
---|
| 46 | messages: {
|
---|
| 47 | missing: "A space is required {{loc}} ','.",
|
---|
| 48 | unexpected: "There should be no space {{loc}} ','."
|
---|
| 49 | }
|
---|
| 50 | },
|
---|
| 51 |
|
---|
| 52 | create(context) {
|
---|
| 53 |
|
---|
| 54 | const sourceCode = context.sourceCode;
|
---|
| 55 | const tokensAndComments = sourceCode.tokensAndComments;
|
---|
| 56 |
|
---|
| 57 | const options = {
|
---|
| 58 | before: context.options[0] ? context.options[0].before : false,
|
---|
| 59 | after: context.options[0] ? context.options[0].after : true
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | //--------------------------------------------------------------------------
|
---|
| 63 | // Helpers
|
---|
| 64 | //--------------------------------------------------------------------------
|
---|
| 65 |
|
---|
| 66 | // list of comma tokens to ignore for the check of leading whitespace
|
---|
| 67 | const commaTokensToIgnore = [];
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Reports a spacing error with an appropriate message.
|
---|
| 71 | * @param {ASTNode} node The binary expression node to report.
|
---|
| 72 | * @param {string} loc Is the error "before" or "after" the comma?
|
---|
| 73 | * @param {ASTNode} otherNode The node at the left or right of `node`
|
---|
| 74 | * @returns {void}
|
---|
| 75 | * @private
|
---|
| 76 | */
|
---|
| 77 | function report(node, loc, otherNode) {
|
---|
| 78 | context.report({
|
---|
| 79 | node,
|
---|
| 80 | fix(fixer) {
|
---|
| 81 | if (options[loc]) {
|
---|
| 82 | if (loc === "before") {
|
---|
| 83 | return fixer.insertTextBefore(node, " ");
|
---|
| 84 | }
|
---|
| 85 | return fixer.insertTextAfter(node, " ");
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 | let start, end;
|
---|
| 89 | const newText = "";
|
---|
| 90 |
|
---|
| 91 | if (loc === "before") {
|
---|
| 92 | start = otherNode.range[1];
|
---|
| 93 | end = node.range[0];
|
---|
| 94 | } else {
|
---|
| 95 | start = node.range[1];
|
---|
| 96 | end = otherNode.range[0];
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | return fixer.replaceTextRange([start, end], newText);
|
---|
| 100 |
|
---|
| 101 | },
|
---|
| 102 | messageId: options[loc] ? "missing" : "unexpected",
|
---|
| 103 | data: {
|
---|
| 104 | loc
|
---|
| 105 | }
|
---|
| 106 | });
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
|
---|
| 111 | * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
|
---|
| 112 | * @returns {void}
|
---|
| 113 | */
|
---|
| 114 | function addNullElementsToIgnoreList(node) {
|
---|
| 115 | let previousToken = sourceCode.getFirstToken(node);
|
---|
| 116 |
|
---|
| 117 | node.elements.forEach(element => {
|
---|
| 118 | let token;
|
---|
| 119 |
|
---|
| 120 | if (element === null) {
|
---|
| 121 | token = sourceCode.getTokenAfter(previousToken);
|
---|
| 122 |
|
---|
| 123 | if (astUtils.isCommaToken(token)) {
|
---|
| 124 | commaTokensToIgnore.push(token);
|
---|
| 125 | }
|
---|
| 126 | } else {
|
---|
| 127 | token = sourceCode.getTokenAfter(element);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | previousToken = token;
|
---|
| 131 | });
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | //--------------------------------------------------------------------------
|
---|
| 135 | // Public
|
---|
| 136 | //--------------------------------------------------------------------------
|
---|
| 137 |
|
---|
| 138 | return {
|
---|
| 139 | "Program:exit"() {
|
---|
| 140 | tokensAndComments.forEach((token, i) => {
|
---|
| 141 |
|
---|
| 142 | if (!astUtils.isCommaToken(token)) {
|
---|
| 143 | return;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | const previousToken = tokensAndComments[i - 1];
|
---|
| 147 | const nextToken = tokensAndComments[i + 1];
|
---|
| 148 |
|
---|
| 149 | if (
|
---|
| 150 | previousToken &&
|
---|
| 151 | !astUtils.isCommaToken(previousToken) && // ignore spacing between two commas
|
---|
| 152 |
|
---|
| 153 | /*
|
---|
| 154 | * `commaTokensToIgnore` are ending commas of `null` elements (array holes/elisions).
|
---|
| 155 | * In addition to spacing between two commas, this can also ignore:
|
---|
| 156 | *
|
---|
| 157 | * - Spacing after `[` (controlled by array-bracket-spacing)
|
---|
| 158 | * Example: [ , ]
|
---|
| 159 | * ^
|
---|
| 160 | * - Spacing after a comment (for backwards compatibility, this was possibly unintentional)
|
---|
| 161 | * Example: [a, /* * / ,]
|
---|
| 162 | * ^
|
---|
| 163 | */
|
---|
| 164 | !commaTokensToIgnore.includes(token) &&
|
---|
| 165 |
|
---|
| 166 | astUtils.isTokenOnSameLine(previousToken, token) &&
|
---|
| 167 | options.before !== sourceCode.isSpaceBetweenTokens(previousToken, token)
|
---|
| 168 | ) {
|
---|
| 169 | report(token, "before", previousToken);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (
|
---|
| 173 | nextToken &&
|
---|
| 174 | !astUtils.isCommaToken(nextToken) && // ignore spacing between two commas
|
---|
| 175 | !astUtils.isClosingParenToken(nextToken) && // controlled by space-in-parens
|
---|
| 176 | !astUtils.isClosingBracketToken(nextToken) && // controlled by array-bracket-spacing
|
---|
| 177 | !astUtils.isClosingBraceToken(nextToken) && // controlled by object-curly-spacing
|
---|
| 178 | !(!options.after && nextToken.type === "Line") && // special case, allow space before line comment
|
---|
| 179 | astUtils.isTokenOnSameLine(token, nextToken) &&
|
---|
| 180 | options.after !== sourceCode.isSpaceBetweenTokens(token, nextToken)
|
---|
| 181 | ) {
|
---|
| 182 | report(token, "after", nextToken);
|
---|
| 183 | }
|
---|
| 184 | });
|
---|
| 185 | },
|
---|
| 186 | ArrayExpression: addNullElementsToIgnoreList,
|
---|
| 187 | ArrayPattern: addNullElementsToIgnoreList
|
---|
| 188 |
|
---|
| 189 | };
|
---|
| 190 |
|
---|
| 191 | }
|
---|
| 192 | };
|
---|