object-curly-spacing.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @fileoverview Disallows or enforces spaces inside of object literals.
  3. * @author Jamund Ferguson
  4. */
  5. "use strict";
  6. const astUtils = require("./utils/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "enforce consistent spacing inside braces",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/object-curly-spacing"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. {
  22. enum: ["always", "never"]
  23. },
  24. {
  25. type: "object",
  26. properties: {
  27. arraysInObjects: {
  28. type: "boolean"
  29. },
  30. objectsInObjects: {
  31. type: "boolean"
  32. }
  33. },
  34. additionalProperties: false
  35. }
  36. ],
  37. messages: {
  38. requireSpaceBefore: "A space is required before '{{token}}'.",
  39. requireSpaceAfter: "A space is required after '{{token}}'.",
  40. unexpectedSpaceBefore: "There should be no space before '{{token}}'.",
  41. unexpectedSpaceAfter: "There should be no space after '{{token}}'."
  42. }
  43. },
  44. create(context) {
  45. const spaced = context.options[0] === "always",
  46. sourceCode = context.getSourceCode();
  47. /**
  48. * Determines whether an option is set, relative to the spacing option.
  49. * If spaced is "always", then check whether option is set to false.
  50. * If spaced is "never", then check whether option is set to true.
  51. * @param {Object} option The option to exclude.
  52. * @returns {boolean} Whether or not the property is excluded.
  53. */
  54. function isOptionSet(option) {
  55. return context.options[1] ? context.options[1][option] === !spaced : false;
  56. }
  57. const options = {
  58. spaced,
  59. arraysInObjectsException: isOptionSet("arraysInObjects"),
  60. objectsInObjectsException: isOptionSet("objectsInObjects")
  61. };
  62. //--------------------------------------------------------------------------
  63. // Helpers
  64. //--------------------------------------------------------------------------
  65. /**
  66. * Reports that there shouldn't be a space after the first token
  67. * @param {ASTNode} node The node to report in the event of an error.
  68. * @param {Token} token The token to use for the report.
  69. * @returns {void}
  70. */
  71. function reportNoBeginningSpace(node, token) {
  72. const nextToken = context.getSourceCode().getTokenAfter(token, { includeComments: true });
  73. context.report({
  74. node,
  75. loc: { start: token.loc.end, end: nextToken.loc.start },
  76. messageId: "unexpectedSpaceAfter",
  77. data: {
  78. token: token.value
  79. },
  80. fix(fixer) {
  81. return fixer.removeRange([token.range[1], nextToken.range[0]]);
  82. }
  83. });
  84. }
  85. /**
  86. * Reports that there shouldn't be a space before the last token
  87. * @param {ASTNode} node The node to report in the event of an error.
  88. * @param {Token} token The token to use for the report.
  89. * @returns {void}
  90. */
  91. function reportNoEndingSpace(node, token) {
  92. const previousToken = context.getSourceCode().getTokenBefore(token, { includeComments: true });
  93. context.report({
  94. node,
  95. loc: { start: previousToken.loc.end, end: token.loc.start },
  96. messageId: "unexpectedSpaceBefore",
  97. data: {
  98. token: token.value
  99. },
  100. fix(fixer) {
  101. return fixer.removeRange([previousToken.range[1], token.range[0]]);
  102. }
  103. });
  104. }
  105. /**
  106. * Reports that there should be a space after the first token
  107. * @param {ASTNode} node The node to report in the event of an error.
  108. * @param {Token} token The token to use for the report.
  109. * @returns {void}
  110. */
  111. function reportRequiredBeginningSpace(node, token) {
  112. context.report({
  113. node,
  114. loc: token.loc,
  115. messageId: "requireSpaceAfter",
  116. data: {
  117. token: token.value
  118. },
  119. fix(fixer) {
  120. return fixer.insertTextAfter(token, " ");
  121. }
  122. });
  123. }
  124. /**
  125. * Reports that there should be a space before the last token
  126. * @param {ASTNode} node The node to report in the event of an error.
  127. * @param {Token} token The token to use for the report.
  128. * @returns {void}
  129. */
  130. function reportRequiredEndingSpace(node, token) {
  131. context.report({
  132. node,
  133. loc: token.loc,
  134. messageId: "requireSpaceBefore",
  135. data: {
  136. token: token.value
  137. },
  138. fix(fixer) {
  139. return fixer.insertTextBefore(token, " ");
  140. }
  141. });
  142. }
  143. /**
  144. * Determines if spacing in curly braces is valid.
  145. * @param {ASTNode} node The AST node to check.
  146. * @param {Token} first The first token to check (should be the opening brace)
  147. * @param {Token} second The second token to check (should be first after the opening brace)
  148. * @param {Token} penultimate The penultimate token to check (should be last before closing brace)
  149. * @param {Token} last The last token to check (should be closing brace)
  150. * @returns {void}
  151. */
  152. function validateBraceSpacing(node, first, second, penultimate, last) {
  153. if (astUtils.isTokenOnSameLine(first, second)) {
  154. const firstSpaced = sourceCode.isSpaceBetweenTokens(first, second);
  155. if (options.spaced && !firstSpaced) {
  156. reportRequiredBeginningSpace(node, first);
  157. }
  158. if (!options.spaced && firstSpaced && second.type !== "Line") {
  159. reportNoBeginningSpace(node, first);
  160. }
  161. }
  162. if (astUtils.isTokenOnSameLine(penultimate, last)) {
  163. const shouldCheckPenultimate = (
  164. options.arraysInObjectsException && astUtils.isClosingBracketToken(penultimate) ||
  165. options.objectsInObjectsException && astUtils.isClosingBraceToken(penultimate)
  166. );
  167. const penultimateType = shouldCheckPenultimate && sourceCode.getNodeByRangeIndex(penultimate.range[0]).type;
  168. const closingCurlyBraceMustBeSpaced = (
  169. options.arraysInObjectsException && penultimateType === "ArrayExpression" ||
  170. options.objectsInObjectsException && (penultimateType === "ObjectExpression" || penultimateType === "ObjectPattern")
  171. ) ? !options.spaced : options.spaced;
  172. const lastSpaced = sourceCode.isSpaceBetweenTokens(penultimate, last);
  173. if (closingCurlyBraceMustBeSpaced && !lastSpaced) {
  174. reportRequiredEndingSpace(node, last);
  175. }
  176. if (!closingCurlyBraceMustBeSpaced && lastSpaced) {
  177. reportNoEndingSpace(node, last);
  178. }
  179. }
  180. }
  181. /**
  182. * Gets '}' token of an object node.
  183. *
  184. * Because the last token of object patterns might be a type annotation,
  185. * this traverses tokens preceded by the last property, then returns the
  186. * first '}' token.
  187. * @param {ASTNode} node The node to get. This node is an
  188. * ObjectExpression or an ObjectPattern. And this node has one or
  189. * more properties.
  190. * @returns {Token} '}' token.
  191. */
  192. function getClosingBraceOfObject(node) {
  193. const lastProperty = node.properties[node.properties.length - 1];
  194. return sourceCode.getTokenAfter(lastProperty, astUtils.isClosingBraceToken);
  195. }
  196. /**
  197. * Reports a given object node if spacing in curly braces is invalid.
  198. * @param {ASTNode} node An ObjectExpression or ObjectPattern node to check.
  199. * @returns {void}
  200. */
  201. function checkForObject(node) {
  202. if (node.properties.length === 0) {
  203. return;
  204. }
  205. const first = sourceCode.getFirstToken(node),
  206. last = getClosingBraceOfObject(node),
  207. second = sourceCode.getTokenAfter(first, { includeComments: true }),
  208. penultimate = sourceCode.getTokenBefore(last, { includeComments: true });
  209. validateBraceSpacing(node, first, second, penultimate, last);
  210. }
  211. /**
  212. * Reports a given import node if spacing in curly braces is invalid.
  213. * @param {ASTNode} node An ImportDeclaration node to check.
  214. * @returns {void}
  215. */
  216. function checkForImport(node) {
  217. if (node.specifiers.length === 0) {
  218. return;
  219. }
  220. let firstSpecifier = node.specifiers[0];
  221. const lastSpecifier = node.specifiers[node.specifiers.length - 1];
  222. if (lastSpecifier.type !== "ImportSpecifier") {
  223. return;
  224. }
  225. if (firstSpecifier.type !== "ImportSpecifier") {
  226. firstSpecifier = node.specifiers[1];
  227. }
  228. const first = sourceCode.getTokenBefore(firstSpecifier),
  229. last = sourceCode.getTokenAfter(lastSpecifier, astUtils.isNotCommaToken),
  230. second = sourceCode.getTokenAfter(first, { includeComments: true }),
  231. penultimate = sourceCode.getTokenBefore(last, { includeComments: true });
  232. validateBraceSpacing(node, first, second, penultimate, last);
  233. }
  234. /**
  235. * Reports a given export node if spacing in curly braces is invalid.
  236. * @param {ASTNode} node An ExportNamedDeclaration node to check.
  237. * @returns {void}
  238. */
  239. function checkForExport(node) {
  240. if (node.specifiers.length === 0) {
  241. return;
  242. }
  243. const firstSpecifier = node.specifiers[0],
  244. lastSpecifier = node.specifiers[node.specifiers.length - 1],
  245. first = sourceCode.getTokenBefore(firstSpecifier),
  246. last = sourceCode.getTokenAfter(lastSpecifier, astUtils.isNotCommaToken),
  247. second = sourceCode.getTokenAfter(first, { includeComments: true }),
  248. penultimate = sourceCode.getTokenBefore(last, { includeComments: true });
  249. validateBraceSpacing(node, first, second, penultimate, last);
  250. }
  251. //--------------------------------------------------------------------------
  252. // Public
  253. //--------------------------------------------------------------------------
  254. return {
  255. // var {x} = y;
  256. ObjectPattern: checkForObject,
  257. // var y = {x: 'y'}
  258. ObjectExpression: checkForObject,
  259. // import {y} from 'x';
  260. ImportDeclaration: checkForImport,
  261. // export {name} from 'yo';
  262. ExportNamedDeclaration: checkForExport
  263. };
  264. }
  265. };