comma-spacing.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @fileoverview Comma spacing - validates spacing before and after comma
  3. * @author Vignesh Anand aka vegetableman.
  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 before and after commas",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/comma-spacing"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. {
  22. type: "object",
  23. properties: {
  24. before: {
  25. type: "boolean",
  26. default: false
  27. },
  28. after: {
  29. type: "boolean",
  30. default: true
  31. }
  32. },
  33. additionalProperties: false
  34. }
  35. ],
  36. messages: {
  37. missing: "A space is required {{loc}} ','.",
  38. unexpected: "There should be no space {{loc}} ','."
  39. }
  40. },
  41. create(context) {
  42. const sourceCode = context.getSourceCode();
  43. const tokensAndComments = sourceCode.tokensAndComments;
  44. const options = {
  45. before: context.options[0] ? context.options[0].before : false,
  46. after: context.options[0] ? context.options[0].after : true
  47. };
  48. //--------------------------------------------------------------------------
  49. // Helpers
  50. //--------------------------------------------------------------------------
  51. // list of comma tokens to ignore for the check of leading whitespace
  52. const commaTokensToIgnore = [];
  53. /**
  54. * Reports a spacing error with an appropriate message.
  55. * @param {ASTNode} node The binary expression node to report.
  56. * @param {string} loc Is the error "before" or "after" the comma?
  57. * @param {ASTNode} otherNode The node at the left or right of `node`
  58. * @returns {void}
  59. * @private
  60. */
  61. function report(node, loc, otherNode) {
  62. context.report({
  63. node,
  64. fix(fixer) {
  65. if (options[loc]) {
  66. if (loc === "before") {
  67. return fixer.insertTextBefore(node, " ");
  68. }
  69. return fixer.insertTextAfter(node, " ");
  70. }
  71. let start, end;
  72. const newText = "";
  73. if (loc === "before") {
  74. start = otherNode.range[1];
  75. end = node.range[0];
  76. } else {
  77. start = node.range[1];
  78. end = otherNode.range[0];
  79. }
  80. return fixer.replaceTextRange([start, end], newText);
  81. },
  82. messageId: options[loc] ? "missing" : "unexpected",
  83. data: {
  84. loc
  85. }
  86. });
  87. }
  88. /**
  89. * Validates the spacing around a comma token.
  90. * @param {Object} tokens The tokens to be validated.
  91. * @param {Token} tokens.comma The token representing the comma.
  92. * @param {Token} [tokens.left] The last token before the comma.
  93. * @param {Token} [tokens.right] The first token after the comma.
  94. * @param {Token|ASTNode} reportItem The item to use when reporting an error.
  95. * @returns {void}
  96. * @private
  97. */
  98. function validateCommaItemSpacing(tokens, reportItem) {
  99. if (tokens.left && astUtils.isTokenOnSameLine(tokens.left, tokens.comma) &&
  100. (options.before !== sourceCode.isSpaceBetweenTokens(tokens.left, tokens.comma))
  101. ) {
  102. report(reportItem, "before", tokens.left);
  103. }
  104. if (tokens.right && astUtils.isClosingParenToken(tokens.right)) {
  105. return;
  106. }
  107. if (tokens.right && !options.after && tokens.right.type === "Line") {
  108. return;
  109. }
  110. if (tokens.right && astUtils.isTokenOnSameLine(tokens.comma, tokens.right) &&
  111. (options.after !== sourceCode.isSpaceBetweenTokens(tokens.comma, tokens.right))
  112. ) {
  113. report(reportItem, "after", tokens.right);
  114. }
  115. }
  116. /**
  117. * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
  118. * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
  119. * @returns {void}
  120. */
  121. function addNullElementsToIgnoreList(node) {
  122. let previousToken = sourceCode.getFirstToken(node);
  123. node.elements.forEach(element => {
  124. let token;
  125. if (element === null) {
  126. token = sourceCode.getTokenAfter(previousToken);
  127. if (astUtils.isCommaToken(token)) {
  128. commaTokensToIgnore.push(token);
  129. }
  130. } else {
  131. token = sourceCode.getTokenAfter(element);
  132. }
  133. previousToken = token;
  134. });
  135. }
  136. //--------------------------------------------------------------------------
  137. // Public
  138. //--------------------------------------------------------------------------
  139. return {
  140. "Program:exit"() {
  141. tokensAndComments.forEach((token, i) => {
  142. if (!astUtils.isCommaToken(token)) {
  143. return;
  144. }
  145. if (token && token.type === "JSXText") {
  146. return;
  147. }
  148. const previousToken = tokensAndComments[i - 1];
  149. const nextToken = tokensAndComments[i + 1];
  150. validateCommaItemSpacing({
  151. comma: token,
  152. left: astUtils.isCommaToken(previousToken) || commaTokensToIgnore.indexOf(token) > -1 ? null : previousToken,
  153. right: astUtils.isCommaToken(nextToken) ? null : nextToken
  154. }, token);
  155. });
  156. },
  157. ArrayExpression: addNullElementsToIgnoreList,
  158. ArrayPattern: addNullElementsToIgnoreList
  159. };
  160. }
  161. };