comma-spacing.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "layout",
  14. docs: {
  15. description: "Enforce consistent spacing before and after commas",
  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. * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
  90. * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
  91. * @returns {void}
  92. */
  93. function addNullElementsToIgnoreList(node) {
  94. let previousToken = sourceCode.getFirstToken(node);
  95. node.elements.forEach(element => {
  96. let token;
  97. if (element === null) {
  98. token = sourceCode.getTokenAfter(previousToken);
  99. if (astUtils.isCommaToken(token)) {
  100. commaTokensToIgnore.push(token);
  101. }
  102. } else {
  103. token = sourceCode.getTokenAfter(element);
  104. }
  105. previousToken = token;
  106. });
  107. }
  108. //--------------------------------------------------------------------------
  109. // Public
  110. //--------------------------------------------------------------------------
  111. return {
  112. "Program:exit"() {
  113. tokensAndComments.forEach((token, i) => {
  114. if (!astUtils.isCommaToken(token)) {
  115. return;
  116. }
  117. const previousToken = tokensAndComments[i - 1];
  118. const nextToken = tokensAndComments[i + 1];
  119. if (
  120. previousToken &&
  121. !astUtils.isCommaToken(previousToken) && // ignore spacing between two commas
  122. /*
  123. * `commaTokensToIgnore` are ending commas of `null` elements (array holes/elisions).
  124. * In addition to spacing between two commas, this can also ignore:
  125. *
  126. * - Spacing after `[` (controlled by array-bracket-spacing)
  127. * Example: [ , ]
  128. * ^
  129. * - Spacing after a comment (for backwards compatibility, this was possibly unintentional)
  130. * Example: [a, /* * / ,]
  131. * ^
  132. */
  133. !commaTokensToIgnore.includes(token) &&
  134. astUtils.isTokenOnSameLine(previousToken, token) &&
  135. options.before !== sourceCode.isSpaceBetweenTokens(previousToken, token)
  136. ) {
  137. report(token, "before", previousToken);
  138. }
  139. if (
  140. nextToken &&
  141. !astUtils.isCommaToken(nextToken) && // ignore spacing between two commas
  142. !astUtils.isClosingParenToken(nextToken) && // controlled by space-in-parens
  143. !astUtils.isClosingBracketToken(nextToken) && // controlled by array-bracket-spacing
  144. !astUtils.isClosingBraceToken(nextToken) && // controlled by object-curly-spacing
  145. !(!options.after && nextToken.type === "Line") && // special case, allow space before line comment
  146. astUtils.isTokenOnSameLine(token, nextToken) &&
  147. options.after !== sourceCode.isSpaceBetweenTokens(token, nextToken)
  148. ) {
  149. report(token, "after", nextToken);
  150. }
  151. });
  152. },
  153. ArrayExpression: addNullElementsToIgnoreList,
  154. ArrayPattern: addNullElementsToIgnoreList
  155. };
  156. }
  157. };