项目原始demo,不改动
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.
 
 
 
 

188 Zeilen
6.3 KiB

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