array-bracket-spacing.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. const astUtils = require("./utils/ast-utils");
  8. //------------------------------------------------------------------------------
  9. // Rule Definition
  10. //------------------------------------------------------------------------------
  11. /** @type {import('../types').Rule.RuleModule} */
  12. module.exports = {
  13. meta: {
  14. deprecated: {
  15. message: "Formatting rules are being moved out of ESLint core.",
  16. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  17. deprecatedSince: "8.53.0",
  18. availableUntil: "11.0.0",
  19. replacedBy: [
  20. {
  21. message:
  22. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  23. url: "https://eslint.style/guide/migration",
  24. plugin: {
  25. name: "@stylistic/eslint-plugin",
  26. url: "https://eslint.style",
  27. },
  28. rule: {
  29. name: "array-bracket-spacing",
  30. url: "https://eslint.style/rules/array-bracket-spacing",
  31. },
  32. },
  33. ],
  34. },
  35. type: "layout",
  36. docs: {
  37. description: "Enforce consistent spacing inside array brackets",
  38. recommended: false,
  39. url: "https://eslint.org/docs/latest/rules/array-bracket-spacing",
  40. },
  41. fixable: "whitespace",
  42. schema: [
  43. {
  44. enum: ["always", "never"],
  45. },
  46. {
  47. type: "object",
  48. properties: {
  49. singleValue: {
  50. type: "boolean",
  51. },
  52. objectsInArrays: {
  53. type: "boolean",
  54. },
  55. arraysInArrays: {
  56. type: "boolean",
  57. },
  58. },
  59. additionalProperties: false,
  60. },
  61. ],
  62. messages: {
  63. unexpectedSpaceAfter:
  64. "There should be no space after '{{tokenValue}}'.",
  65. unexpectedSpaceBefore:
  66. "There should be no space before '{{tokenValue}}'.",
  67. missingSpaceAfter: "A space is required after '{{tokenValue}}'.",
  68. missingSpaceBefore: "A space is required before '{{tokenValue}}'.",
  69. },
  70. },
  71. create(context) {
  72. const spaced = context.options[0] === "always",
  73. sourceCode = context.sourceCode;
  74. /**
  75. * Determines whether an option is set, relative to the spacing option.
  76. * If spaced is "always", then check whether option is set to false.
  77. * If spaced is "never", then check whether option is set to true.
  78. * @param {Object} option The option to exclude.
  79. * @returns {boolean} Whether or not the property is excluded.
  80. */
  81. function isOptionSet(option) {
  82. return context.options[1]
  83. ? context.options[1][option] === !spaced
  84. : false;
  85. }
  86. const options = {
  87. spaced,
  88. singleElementException: isOptionSet("singleValue"),
  89. objectsInArraysException: isOptionSet("objectsInArrays"),
  90. arraysInArraysException: isOptionSet("arraysInArrays"),
  91. };
  92. //--------------------------------------------------------------------------
  93. // Helpers
  94. //--------------------------------------------------------------------------
  95. /**
  96. * Reports that there shouldn't be a space after the first token
  97. * @param {ASTNode} node The node to report in the event of an error.
  98. * @param {Token} token The token to use for the report.
  99. * @returns {void}
  100. */
  101. function reportNoBeginningSpace(node, token) {
  102. const nextToken = sourceCode.getTokenAfter(token);
  103. context.report({
  104. node,
  105. loc: { start: token.loc.end, end: nextToken.loc.start },
  106. messageId: "unexpectedSpaceAfter",
  107. data: {
  108. tokenValue: token.value,
  109. },
  110. fix(fixer) {
  111. return fixer.removeRange([
  112. token.range[1],
  113. nextToken.range[0],
  114. ]);
  115. },
  116. });
  117. }
  118. /**
  119. * Reports that there shouldn't be a space before the last token
  120. * @param {ASTNode} node The node to report in the event of an error.
  121. * @param {Token} token The token to use for the report.
  122. * @returns {void}
  123. */
  124. function reportNoEndingSpace(node, token) {
  125. const previousToken = sourceCode.getTokenBefore(token);
  126. context.report({
  127. node,
  128. loc: { start: previousToken.loc.end, end: token.loc.start },
  129. messageId: "unexpectedSpaceBefore",
  130. data: {
  131. tokenValue: token.value,
  132. },
  133. fix(fixer) {
  134. return fixer.removeRange([
  135. previousToken.range[1],
  136. token.range[0],
  137. ]);
  138. },
  139. });
  140. }
  141. /**
  142. * Reports that there should be a space after the first token
  143. * @param {ASTNode} node The node to report in the event of an error.
  144. * @param {Token} token The token to use for the report.
  145. * @returns {void}
  146. */
  147. function reportRequiredBeginningSpace(node, token) {
  148. context.report({
  149. node,
  150. loc: token.loc,
  151. messageId: "missingSpaceAfter",
  152. data: {
  153. tokenValue: token.value,
  154. },
  155. fix(fixer) {
  156. return fixer.insertTextAfter(token, " ");
  157. },
  158. });
  159. }
  160. /**
  161. * Reports that there should be a space before the last token
  162. * @param {ASTNode} node The node to report in the event of an error.
  163. * @param {Token} token The token to use for the report.
  164. * @returns {void}
  165. */
  166. function reportRequiredEndingSpace(node, token) {
  167. context.report({
  168. node,
  169. loc: token.loc,
  170. messageId: "missingSpaceBefore",
  171. data: {
  172. tokenValue: token.value,
  173. },
  174. fix(fixer) {
  175. return fixer.insertTextBefore(token, " ");
  176. },
  177. });
  178. }
  179. /**
  180. * Determines if a node is an object type
  181. * @param {ASTNode} node The node to check.
  182. * @returns {boolean} Whether or not the node is an object type.
  183. */
  184. function isObjectType(node) {
  185. return (
  186. node &&
  187. (node.type === "ObjectExpression" ||
  188. node.type === "ObjectPattern")
  189. );
  190. }
  191. /**
  192. * Determines if a node is an array type
  193. * @param {ASTNode} node The node to check.
  194. * @returns {boolean} Whether or not the node is an array type.
  195. */
  196. function isArrayType(node) {
  197. return (
  198. node &&
  199. (node.type === "ArrayExpression" ||
  200. node.type === "ArrayPattern")
  201. );
  202. }
  203. /**
  204. * Validates the spacing around array brackets
  205. * @param {ASTNode} node The node we're checking for spacing
  206. * @returns {void}
  207. */
  208. function validateArraySpacing(node) {
  209. if (options.spaced && node.elements.length === 0) {
  210. return;
  211. }
  212. const first = sourceCode.getFirstToken(node),
  213. second = sourceCode.getFirstToken(node, 1),
  214. last = node.typeAnnotation
  215. ? sourceCode.getTokenBefore(node.typeAnnotation)
  216. : sourceCode.getLastToken(node),
  217. penultimate = sourceCode.getTokenBefore(last),
  218. firstElement = node.elements[0],
  219. lastElement = node.elements.at(-1);
  220. const openingBracketMustBeSpaced =
  221. (options.objectsInArraysException &&
  222. isObjectType(firstElement)) ||
  223. (options.arraysInArraysException &&
  224. isArrayType(firstElement)) ||
  225. (options.singleElementException && node.elements.length === 1)
  226. ? !options.spaced
  227. : options.spaced;
  228. const closingBracketMustBeSpaced =
  229. (options.objectsInArraysException &&
  230. isObjectType(lastElement)) ||
  231. (options.arraysInArraysException && isArrayType(lastElement)) ||
  232. (options.singleElementException && node.elements.length === 1)
  233. ? !options.spaced
  234. : options.spaced;
  235. if (astUtils.isTokenOnSameLine(first, second)) {
  236. if (
  237. openingBracketMustBeSpaced &&
  238. !sourceCode.isSpaceBetweenTokens(first, second)
  239. ) {
  240. reportRequiredBeginningSpace(node, first);
  241. }
  242. if (
  243. !openingBracketMustBeSpaced &&
  244. sourceCode.isSpaceBetweenTokens(first, second)
  245. ) {
  246. reportNoBeginningSpace(node, first);
  247. }
  248. }
  249. if (
  250. first !== penultimate &&
  251. astUtils.isTokenOnSameLine(penultimate, last)
  252. ) {
  253. if (
  254. closingBracketMustBeSpaced &&
  255. !sourceCode.isSpaceBetweenTokens(penultimate, last)
  256. ) {
  257. reportRequiredEndingSpace(node, last);
  258. }
  259. if (
  260. !closingBracketMustBeSpaced &&
  261. sourceCode.isSpaceBetweenTokens(penultimate, last)
  262. ) {
  263. reportNoEndingSpace(node, last);
  264. }
  265. }
  266. }
  267. //--------------------------------------------------------------------------
  268. // Public
  269. //--------------------------------------------------------------------------
  270. return {
  271. ArrayPattern: validateArraySpacing,
  272. ArrayExpression: validateArraySpacing,
  273. };
  274. },
  275. };