array-bracket-even-spacing.js 6.8 KB

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