array-bracket-even-spacing.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use strict'
  2. /**
  3. * @fileoverview Disallows or enforces spaces inside of array brackets.
  4. * @author Jamund Ferguson
  5. * @copyright 2015 Jamund Ferguson. All rights reserved.
  6. * @copyright 2014 Brandyn Bennett. All rights reserved.
  7. * @copyright 2014 Michael Ficarra. No rights reserved.
  8. * @copyright 2014 Vignesh Anand. All rights reserved.
  9. */
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: 'layout',
  16. docs: {
  17. url: 'https://github.com/standard/eslint-plugin-standard#rules-explanations'
  18. }
  19. },
  20. create: function (context) {
  21. const spaced = context.options[0] === 'always'
  22. const either = context.options[0] === 'either'
  23. /**
  24. * Determines whether an option is set, relative to the spacing option.
  25. * If spaced is "always", then check whether option is set to false.
  26. * If spaced is "never", then check whether option is set to true.
  27. * @param {Object} option - The option to exclude.
  28. * @returns {boolean} Whether or not the property is excluded.
  29. */
  30. function isOptionSet (option) {
  31. return context.options[1] != null ? context.options[1][option] === !spaced : false
  32. }
  33. const options = {
  34. either,
  35. spaced,
  36. singleElementException: isOptionSet('singleValue'),
  37. objectsInArraysException: isOptionSet('objectsInArrays'),
  38. arraysInArraysException: isOptionSet('arraysInArrays')
  39. }
  40. // --------------------------------------------------------------------------
  41. // Helpers
  42. // --------------------------------------------------------------------------
  43. /**
  44. * Determines whether two adjacent tokens are have whitespace between them.
  45. * @param {Object} left - The left token object.
  46. * @param {Object} right - The right token object.
  47. * @returns {boolean} Whether or not there is space between the tokens.
  48. */
  49. function isSpaced (left, right) {
  50. return left.range[1] < right.range[0]
  51. }
  52. /**
  53. * Determines whether two adjacent tokens are on the same line.
  54. * @param {Object} left - The left token object.
  55. * @param {Object} right - The right token object.
  56. * @returns {boolean} Whether or not the tokens are on the same line.
  57. */
  58. function isSameLine (left, right) {
  59. return left.loc.start.line === right.loc.start.line
  60. }
  61. /**
  62. * Reports that there shouldn't be a space after the first token
  63. * @param {ASTNode} node - The node to report in the event of an error.
  64. * @param {Token} token - The token to use for the report.
  65. * @returns {void}
  66. */
  67. function reportNoBeginningSpace (node, token) {
  68. context.report(node, token.loc.start,
  69. "There should be no space after '" + token.value + "'")
  70. }
  71. /**
  72. * Reports that there shouldn't be a space before the last token
  73. * @param {ASTNode} node - The node to report in the event of an error.
  74. * @param {Token} token - The token to use for the report.
  75. * @returns {void}
  76. */
  77. function reportNoEndingSpace (node, token) {
  78. context.report(node, token.loc.start,
  79. "There should be no space before '" + token.value + "'")
  80. }
  81. /**
  82. * Reports that there should be a space after the first token
  83. * @param {ASTNode} node - The node to report in the event of an error.
  84. * @param {Token} token - The token to use for the report.
  85. * @returns {void}
  86. */
  87. function reportRequiredBeginningSpace (node, token) {
  88. context.report(node, token.loc.start,
  89. "A space is required after '" + token.value + "'")
  90. }
  91. /**
  92. * Reports that there should be a space before the last token
  93. * @param {ASTNode} node - The node to report in the event of an error.
  94. * @param {Token} token - The token to use for the report.
  95. * @returns {void}
  96. */
  97. function reportRequiredEndingSpace (node, token) {
  98. context.report(node, token.loc.start,
  99. "A space is required before '" + token.value + "'")
  100. }
  101. /**
  102. * Checks if a start and end brace in a node are spaced evenly
  103. * and not too long (>1 space)
  104. * @param node
  105. * @param start
  106. * @param end
  107. * @returns {boolean}
  108. */
  109. function isEvenlySpacedAndNotTooLong (node, start, end) {
  110. const expectedSpace = start[1].range[0] - start[0].range[1]
  111. const endSpace = end[1].range[0] - end[0].range[1]
  112. return endSpace === expectedSpace && endSpace <= 1
  113. }
  114. /**
  115. * Validates the spacing around array brackets
  116. * @param {ASTNode} node - The node we're checking for spacing
  117. * @returns {void}
  118. */
  119. function validateArraySpacing (node) {
  120. if (node.elements.length === 0) {
  121. return
  122. }
  123. const first = context.getFirstToken(node)
  124. const second = context.getFirstToken(node, 1)
  125. const penultimate = context.getLastToken(node, 1)
  126. const last = context.getLastToken(node)
  127. const openingBracketMustBeSpaced =
  128. (options.objectsInArraysException && second.value === '{') ||
  129. (options.arraysInArraysException && second.value === '[') ||
  130. (options.singleElementException && node.elements.length === 1)
  131. ? !options.spaced
  132. : options.spaced
  133. const closingBracketMustBeSpaced =
  134. (options.objectsInArraysException && penultimate.value === '}') ||
  135. (options.arraysInArraysException && penultimate.value === ']') ||
  136. (options.singleElementException && node.elements.length === 1)
  137. ? !options.spaced
  138. : options.spaced
  139. // we only care about evenly spaced things
  140. if (options.either) {
  141. // newlines at any point means return
  142. if (!isSameLine(first, last)) {
  143. return
  144. }
  145. // confirm that the object expression/literal is spaced evenly
  146. if (!isEvenlySpacedAndNotTooLong(node, [first, second], [penultimate, last])) {
  147. context.report(node, 'Expected consistent spacing')
  148. }
  149. return
  150. }
  151. if (isSameLine(first, second)) {
  152. if (openingBracketMustBeSpaced && !isSpaced(first, second)) {
  153. reportRequiredBeginningSpace(node, first)
  154. }
  155. if (!openingBracketMustBeSpaced && isSpaced(first, second)) {
  156. reportNoBeginningSpace(node, first)
  157. }
  158. }
  159. if (isSameLine(penultimate, last)) {
  160. if (closingBracketMustBeSpaced && !isSpaced(penultimate, last)) {
  161. reportRequiredEndingSpace(node, last)
  162. }
  163. if (!closingBracketMustBeSpaced && isSpaced(penultimate, last)) {
  164. reportNoEndingSpace(node, last)
  165. }
  166. }
  167. }
  168. // --------------------------------------------------------------------------
  169. // Public
  170. // --------------------------------------------------------------------------
  171. return {
  172. ArrayPattern: validateArraySpacing,
  173. ArrayExpression: validateArraySpacing
  174. }
  175. }
  176. }