object-curly-even-spacing.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict'
  2. /**
  3. * @fileoverview Disallows or enforces spaces inside of object literals.
  4. * @author Jamund Ferguson
  5. * @copyright 2014 Brandyn Bennett. All rights reserved.
  6. * @copyright 2014 Michael Ficarra. No rights reserved.
  7. * @copyright 2014 Vignesh Anand. All rights reserved.
  8. * @copyright 2015 Jamund Ferguson. 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. spaced: spaced,
  34. either: either,
  35. arraysInObjectsException: isOptionSet('arraysInObjects'),
  36. objectsInObjectsException: isOptionSet('objectsInObjects')
  37. }
  38. // --------------------------------------------------------------------------
  39. // Helpers
  40. // --------------------------------------------------------------------------
  41. /**
  42. * Determines whether two adjacent tokens are have whitespace between them.
  43. * @param {Object} left - The left token object.
  44. * @param {Object} right - The right token object.
  45. * @returns {boolean} Whether or not there is space between the tokens.
  46. */
  47. function isSpaced (left, right) {
  48. return left.range[1] < right.range[0]
  49. }
  50. /**
  51. * Determines whether two adjacent tokens are on the same line.
  52. * @param {Object} left - The left token object.
  53. * @param {Object} right - The right token object.
  54. * @returns {boolean} Whether or not the tokens are on the same line.
  55. */
  56. function isSameLine (left, right) {
  57. return left.loc.start.line === right.loc.start.line
  58. }
  59. /**
  60. * Reports that there shouldn't be a space after the first token
  61. * @param {ASTNode} node - The node to report in the event of an error.
  62. * @param {Token} token - The token to use for the report.
  63. * @returns {void}
  64. */
  65. function reportNoBeginningSpace (node, token) {
  66. context.report(node, token.loc.start,
  67. "There should be no space after '" + token.value + "'")
  68. }
  69. /**
  70. * Reports that there shouldn't be a space before the last 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 reportNoEndingSpace (node, token) {
  76. context.report(node, token.loc.start,
  77. "There should be no space before '" + token.value + "'")
  78. }
  79. /**
  80. * Reports that there should be a space after the first token
  81. * @param {ASTNode} node - The node to report in the event of an error.
  82. * @param {Token} token - The token to use for the report.
  83. * @returns {void}
  84. */
  85. function reportRequiredBeginningSpace (node, token) {
  86. context.report(node, token.loc.start,
  87. "A space is required after '" + token.value + "'")
  88. }
  89. /**
  90. * Reports that there should 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 reportRequiredEndingSpace (node, token) {
  96. context.report(node, token.loc.start,
  97. "A space is required before '" + token.value + "'")
  98. }
  99. /**
  100. * Checks if a start and end brace in a node are spaced evenly
  101. * and not too long (>1 space)
  102. * @param node
  103. * @param start
  104. * @param end
  105. * @returns {boolean}
  106. */
  107. function isEvenlySpacedAndNotTooLong (node, start, end) {
  108. var expectedSpace = start[1].range[0] - start[0].range[1]
  109. var endSpace = end[1].range[0] - end[0].range[1]
  110. return endSpace === expectedSpace && endSpace <= 1
  111. }
  112. /**
  113. * Determines if spacing in curly braces is valid.
  114. * @param {ASTNode} node The AST node to check.
  115. * @param {Token} first The first token to check (should be the opening brace)
  116. * @param {Token} second The second token to check (should be first after the opening brace)
  117. * @param {Token} penultimate The penultimate token to check (should be last before closing brace)
  118. * @param {Token} last The last token to check (should be closing brace)
  119. * @returns {void}
  120. */
  121. function validateBraceSpacing (node, first, second, penultimate, last) {
  122. var closingCurlyBraceMustBeSpaced =
  123. (options.arraysInObjectsException && penultimate.value === ']') ||
  124. (options.objectsInObjectsException && penultimate.value === '}')
  125. ? !options.spaced : options.spaced
  126. // we only care about evenly spaced things
  127. if (options.either) {
  128. // newlines at any point means return
  129. if (!isSameLine(first, last)) {
  130. return
  131. }
  132. // confirm that the object expression/literal is spaced evenly
  133. if (!isEvenlySpacedAndNotTooLong(node, [first, second], [penultimate, last])) {
  134. context.report(node, 'Expected consistent spacing')
  135. }
  136. return
  137. }
  138. // { and key are on same line
  139. if (isSameLine(first, second)) {
  140. if (options.spaced && !isSpaced(first, second)) {
  141. reportRequiredBeginningSpace(node, first)
  142. }
  143. if (!options.spaced && isSpaced(first, second)) {
  144. reportNoBeginningSpace(node, first)
  145. }
  146. }
  147. // final key and } ore on the same line
  148. if (isSameLine(penultimate, last)) {
  149. if (closingCurlyBraceMustBeSpaced && !isSpaced(penultimate, last)) {
  150. reportRequiredEndingSpace(node, last)
  151. }
  152. if (!closingCurlyBraceMustBeSpaced && isSpaced(penultimate, last)) {
  153. reportNoEndingSpace(node, last)
  154. }
  155. }
  156. }
  157. // --------------------------------------------------------------------------
  158. // Public
  159. // --------------------------------------------------------------------------
  160. return {
  161. // var {x} = y
  162. ObjectPattern: function (node) {
  163. if (node.properties.length === 0) {
  164. return
  165. }
  166. var firstSpecifier = node.properties[0]
  167. var lastSpecifier = node.properties[node.properties.length - 1]
  168. var first = context.getTokenBefore(firstSpecifier)
  169. var second = context.getFirstToken(firstSpecifier)
  170. var penultimate = context.getLastToken(lastSpecifier)
  171. var last = context.getTokenAfter(lastSpecifier)
  172. // support trailing commas
  173. if (last.value === ',') {
  174. penultimate = last
  175. last = context.getTokenAfter(last)
  176. }
  177. validateBraceSpacing(node, first, second, penultimate, last)
  178. },
  179. // import {y} from 'x'
  180. ImportDeclaration: function (node) {
  181. var firstSpecifier = node.specifiers[0]
  182. var lastSpecifier = node.specifiers[node.specifiers.length - 1]
  183. // don't do anything for namespace or default imports
  184. if (firstSpecifier && lastSpecifier && firstSpecifier.type === 'ImportSpecifier' && lastSpecifier.type === 'ImportSpecifier') {
  185. var first = context.getTokenBefore(firstSpecifier)
  186. var second = context.getFirstToken(firstSpecifier)
  187. var penultimate = context.getLastToken(lastSpecifier)
  188. var last = context.getTokenAfter(lastSpecifier)
  189. validateBraceSpacing(node, first, second, penultimate, last)
  190. }
  191. },
  192. // export {name} from 'yo'
  193. ExportNamedDeclaration: function (node) {
  194. if (!node.specifiers.length) {
  195. return
  196. }
  197. var firstSpecifier = node.specifiers[0]
  198. var lastSpecifier = node.specifiers[node.specifiers.length - 1]
  199. var first = context.getTokenBefore(firstSpecifier)
  200. var second = context.getFirstToken(firstSpecifier)
  201. var penultimate = context.getLastToken(lastSpecifier)
  202. var last = context.getTokenAfter(lastSpecifier)
  203. validateBraceSpacing(node, first, second, penultimate, last)
  204. },
  205. // var y = {x: 'y'}
  206. ObjectExpression: function (node) {
  207. if (node.properties.length === 0) {
  208. return
  209. }
  210. var first = context.getFirstToken(node)
  211. var second = context.getFirstToken(node, 1)
  212. var penultimate = context.getLastToken(node, 1)
  213. var last = context.getLastToken(node)
  214. validateBraceSpacing(node, first, second, penultimate, last)
  215. }
  216. }
  217. }
  218. }