| [9af201e] | 1 | /**
|
|---|
| 2 | * @fileoverview Disallows or enforces spaces inside of array brackets.
|
|---|
| 3 | * @author Jamund Ferguson
|
|---|
| 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 inside array brackets",
|
|---|
| 23 | recommended: false,
|
|---|
| 24 | url: "https://eslint.org/docs/latest/rules/array-bracket-spacing"
|
|---|
| 25 | },
|
|---|
| 26 |
|
|---|
| 27 | fixable: "whitespace",
|
|---|
| 28 |
|
|---|
| 29 | schema: [
|
|---|
| 30 | {
|
|---|
| 31 | enum: ["always", "never"]
|
|---|
| 32 | },
|
|---|
| 33 | {
|
|---|
| 34 | type: "object",
|
|---|
| 35 | properties: {
|
|---|
| 36 | singleValue: {
|
|---|
| 37 | type: "boolean"
|
|---|
| 38 | },
|
|---|
| 39 | objectsInArrays: {
|
|---|
| 40 | type: "boolean"
|
|---|
| 41 | },
|
|---|
| 42 | arraysInArrays: {
|
|---|
| 43 | type: "boolean"
|
|---|
| 44 | }
|
|---|
| 45 | },
|
|---|
| 46 | additionalProperties: false
|
|---|
| 47 | }
|
|---|
| 48 | ],
|
|---|
| 49 |
|
|---|
| 50 | messages: {
|
|---|
| 51 | unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.",
|
|---|
| 52 | unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.",
|
|---|
| 53 | missingSpaceAfter: "A space is required after '{{tokenValue}}'.",
|
|---|
| 54 | missingSpaceBefore: "A space is required before '{{tokenValue}}'."
|
|---|
| 55 | }
|
|---|
| 56 | },
|
|---|
| 57 | create(context) {
|
|---|
| 58 | const spaced = context.options[0] === "always",
|
|---|
| 59 | sourceCode = context.sourceCode;
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Determines whether an option is set, relative to the spacing option.
|
|---|
| 63 | * If spaced is "always", then check whether option is set to false.
|
|---|
| 64 | * If spaced is "never", then check whether option is set to true.
|
|---|
| 65 | * @param {Object} option The option to exclude.
|
|---|
| 66 | * @returns {boolean} Whether or not the property is excluded.
|
|---|
| 67 | */
|
|---|
| 68 | function isOptionSet(option) {
|
|---|
| 69 | return context.options[1] ? context.options[1][option] === !spaced : false;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | const options = {
|
|---|
| 73 | spaced,
|
|---|
| 74 | singleElementException: isOptionSet("singleValue"),
|
|---|
| 75 | objectsInArraysException: isOptionSet("objectsInArrays"),
|
|---|
| 76 | arraysInArraysException: isOptionSet("arraysInArrays")
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | //--------------------------------------------------------------------------
|
|---|
| 80 | // Helpers
|
|---|
| 81 | //--------------------------------------------------------------------------
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Reports that there shouldn't be a space after the first token
|
|---|
| 85 | * @param {ASTNode} node The node to report in the event of an error.
|
|---|
| 86 | * @param {Token} token The token to use for the report.
|
|---|
| 87 | * @returns {void}
|
|---|
| 88 | */
|
|---|
| 89 | function reportNoBeginningSpace(node, token) {
|
|---|
| 90 | const nextToken = sourceCode.getTokenAfter(token);
|
|---|
| 91 |
|
|---|
| 92 | context.report({
|
|---|
| 93 | node,
|
|---|
| 94 | loc: { start: token.loc.end, end: nextToken.loc.start },
|
|---|
| 95 | messageId: "unexpectedSpaceAfter",
|
|---|
| 96 | data: {
|
|---|
| 97 | tokenValue: token.value
|
|---|
| 98 | },
|
|---|
| 99 | fix(fixer) {
|
|---|
| 100 | return fixer.removeRange([token.range[1], nextToken.range[0]]);
|
|---|
| 101 | }
|
|---|
| 102 | });
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Reports that there shouldn't be a space before the last token
|
|---|
| 107 | * @param {ASTNode} node The node to report in the event of an error.
|
|---|
| 108 | * @param {Token} token The token to use for the report.
|
|---|
| 109 | * @returns {void}
|
|---|
| 110 | */
|
|---|
| 111 | function reportNoEndingSpace(node, token) {
|
|---|
| 112 | const previousToken = sourceCode.getTokenBefore(token);
|
|---|
| 113 |
|
|---|
| 114 | context.report({
|
|---|
| 115 | node,
|
|---|
| 116 | loc: { start: previousToken.loc.end, end: token.loc.start },
|
|---|
| 117 | messageId: "unexpectedSpaceBefore",
|
|---|
| 118 | data: {
|
|---|
| 119 | tokenValue: token.value
|
|---|
| 120 | },
|
|---|
| 121 | fix(fixer) {
|
|---|
| 122 | return fixer.removeRange([previousToken.range[1], token.range[0]]);
|
|---|
| 123 | }
|
|---|
| 124 | });
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Reports that there should be a space after the first token
|
|---|
| 129 | * @param {ASTNode} node The node to report in the event of an error.
|
|---|
| 130 | * @param {Token} token The token to use for the report.
|
|---|
| 131 | * @returns {void}
|
|---|
| 132 | */
|
|---|
| 133 | function reportRequiredBeginningSpace(node, token) {
|
|---|
| 134 | context.report({
|
|---|
| 135 | node,
|
|---|
| 136 | loc: token.loc,
|
|---|
| 137 | messageId: "missingSpaceAfter",
|
|---|
| 138 | data: {
|
|---|
| 139 | tokenValue: token.value
|
|---|
| 140 | },
|
|---|
| 141 | fix(fixer) {
|
|---|
| 142 | return fixer.insertTextAfter(token, " ");
|
|---|
| 143 | }
|
|---|
| 144 | });
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Reports that there should be a space before the last token
|
|---|
| 149 | * @param {ASTNode} node The node to report in the event of an error.
|
|---|
| 150 | * @param {Token} token The token to use for the report.
|
|---|
| 151 | * @returns {void}
|
|---|
| 152 | */
|
|---|
| 153 | function reportRequiredEndingSpace(node, token) {
|
|---|
| 154 | context.report({
|
|---|
| 155 | node,
|
|---|
| 156 | loc: token.loc,
|
|---|
| 157 | messageId: "missingSpaceBefore",
|
|---|
| 158 | data: {
|
|---|
| 159 | tokenValue: token.value
|
|---|
| 160 | },
|
|---|
| 161 | fix(fixer) {
|
|---|
| 162 | return fixer.insertTextBefore(token, " ");
|
|---|
| 163 | }
|
|---|
| 164 | });
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | * Determines if a node is an object type
|
|---|
| 169 | * @param {ASTNode} node The node to check.
|
|---|
| 170 | * @returns {boolean} Whether or not the node is an object type.
|
|---|
| 171 | */
|
|---|
| 172 | function isObjectType(node) {
|
|---|
| 173 | return node && (node.type === "ObjectExpression" || node.type === "ObjectPattern");
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /**
|
|---|
| 177 | * Determines if a node is an array type
|
|---|
| 178 | * @param {ASTNode} node The node to check.
|
|---|
| 179 | * @returns {boolean} Whether or not the node is an array type.
|
|---|
| 180 | */
|
|---|
| 181 | function isArrayType(node) {
|
|---|
| 182 | return node && (node.type === "ArrayExpression" || node.type === "ArrayPattern");
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /**
|
|---|
| 186 | * Validates the spacing around array brackets
|
|---|
| 187 | * @param {ASTNode} node The node we're checking for spacing
|
|---|
| 188 | * @returns {void}
|
|---|
| 189 | */
|
|---|
| 190 | function validateArraySpacing(node) {
|
|---|
| 191 | if (options.spaced && node.elements.length === 0) {
|
|---|
| 192 | return;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | const first = sourceCode.getFirstToken(node),
|
|---|
| 196 | second = sourceCode.getFirstToken(node, 1),
|
|---|
| 197 | last = node.typeAnnotation
|
|---|
| 198 | ? sourceCode.getTokenBefore(node.typeAnnotation)
|
|---|
| 199 | : sourceCode.getLastToken(node),
|
|---|
| 200 | penultimate = sourceCode.getTokenBefore(last),
|
|---|
| 201 | firstElement = node.elements[0],
|
|---|
| 202 | lastElement = node.elements[node.elements.length - 1];
|
|---|
| 203 |
|
|---|
| 204 | const openingBracketMustBeSpaced =
|
|---|
| 205 | options.objectsInArraysException && isObjectType(firstElement) ||
|
|---|
| 206 | options.arraysInArraysException && isArrayType(firstElement) ||
|
|---|
| 207 | options.singleElementException && node.elements.length === 1
|
|---|
| 208 | ? !options.spaced : options.spaced;
|
|---|
| 209 |
|
|---|
| 210 | const closingBracketMustBeSpaced =
|
|---|
| 211 | options.objectsInArraysException && isObjectType(lastElement) ||
|
|---|
| 212 | options.arraysInArraysException && isArrayType(lastElement) ||
|
|---|
| 213 | options.singleElementException && node.elements.length === 1
|
|---|
| 214 | ? !options.spaced : options.spaced;
|
|---|
| 215 |
|
|---|
| 216 | if (astUtils.isTokenOnSameLine(first, second)) {
|
|---|
| 217 | if (openingBracketMustBeSpaced && !sourceCode.isSpaceBetweenTokens(first, second)) {
|
|---|
| 218 | reportRequiredBeginningSpace(node, first);
|
|---|
| 219 | }
|
|---|
| 220 | if (!openingBracketMustBeSpaced && sourceCode.isSpaceBetweenTokens(first, second)) {
|
|---|
| 221 | reportNoBeginningSpace(node, first);
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (first !== penultimate && astUtils.isTokenOnSameLine(penultimate, last)) {
|
|---|
| 226 | if (closingBracketMustBeSpaced && !sourceCode.isSpaceBetweenTokens(penultimate, last)) {
|
|---|
| 227 | reportRequiredEndingSpace(node, last);
|
|---|
| 228 | }
|
|---|
| 229 | if (!closingBracketMustBeSpaced && sourceCode.isSpaceBetweenTokens(penultimate, last)) {
|
|---|
| 230 | reportNoEndingSpace(node, last);
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | //--------------------------------------------------------------------------
|
|---|
| 236 | // Public
|
|---|
| 237 | //--------------------------------------------------------------------------
|
|---|
| 238 |
|
|---|
| 239 | return {
|
|---|
| 240 | ArrayPattern: validateArraySpacing,
|
|---|
| 241 | ArrayExpression: validateArraySpacing
|
|---|
| 242 | };
|
|---|
| 243 | }
|
|---|
| 244 | };
|
|---|