array-bracket-spacing.js 8.9 KB

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