Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comma-spacing.js 6.3KB

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