项目原始demo,不改动
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

4 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @fileoverview Disallow use of multiple spaces.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. const astUtils = require("../ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: "disallow multiple spaces",
  14. category: "Best Practices",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-multi-spaces"
  17. },
  18. fixable: "whitespace",
  19. schema: [
  20. {
  21. type: "object",
  22. properties: {
  23. exceptions: {
  24. type: "object",
  25. patternProperties: {
  26. "^([A-Z][a-z]*)+$": {
  27. type: "boolean"
  28. }
  29. },
  30. additionalProperties: false
  31. },
  32. ignoreEOLComments: {
  33. type: "boolean"
  34. }
  35. },
  36. additionalProperties: false
  37. }
  38. ]
  39. },
  40. create(context) {
  41. const sourceCode = context.getSourceCode();
  42. const options = context.options[0] || {};
  43. const ignoreEOLComments = options.ignoreEOLComments;
  44. const exceptions = Object.assign({ Property: true }, options.exceptions);
  45. const hasExceptions = Object.keys(exceptions).filter(key => exceptions[key]).length > 0;
  46. /**
  47. * Formats value of given comment token for error message by truncating its length.
  48. * @param {Token} token comment token
  49. * @returns {string} formatted value
  50. * @private
  51. */
  52. function formatReportedCommentValue(token) {
  53. const valueLines = token.value.split("\n");
  54. const value = valueLines[0];
  55. const formattedValue = `${value.slice(0, 12)}...`;
  56. return valueLines.length === 1 && value.length <= 12 ? value : formattedValue;
  57. }
  58. //--------------------------------------------------------------------------
  59. // Public
  60. //--------------------------------------------------------------------------
  61. return {
  62. Program() {
  63. sourceCode.tokensAndComments.forEach((leftToken, leftIndex, tokensAndComments) => {
  64. if (leftIndex === tokensAndComments.length - 1) {
  65. return;
  66. }
  67. const rightToken = tokensAndComments[leftIndex + 1];
  68. // Ignore tokens that don't have 2 spaces between them or are on different lines
  69. if (
  70. !sourceCode.text.slice(leftToken.range[1], rightToken.range[0]).includes(" ") ||
  71. leftToken.loc.end.line < rightToken.loc.start.line
  72. ) {
  73. return;
  74. }
  75. // Ignore comments that are the last token on their line if `ignoreEOLComments` is active.
  76. if (
  77. ignoreEOLComments &&
  78. astUtils.isCommentToken(rightToken) &&
  79. (
  80. leftIndex === tokensAndComments.length - 2 ||
  81. rightToken.loc.end.line < tokensAndComments[leftIndex + 2].loc.start.line
  82. )
  83. ) {
  84. return;
  85. }
  86. // Ignore tokens that are in a node in the "exceptions" object
  87. if (hasExceptions) {
  88. const parentNode = sourceCode.getNodeByRangeIndex(rightToken.range[0] - 1);
  89. if (parentNode && exceptions[parentNode.type]) {
  90. return;
  91. }
  92. }
  93. let displayValue;
  94. if (rightToken.type === "Block") {
  95. displayValue = `/*${formatReportedCommentValue(rightToken)}*/`;
  96. } else if (rightToken.type === "Line") {
  97. displayValue = `//${formatReportedCommentValue(rightToken)}`;
  98. } else {
  99. displayValue = rightToken.value;
  100. }
  101. context.report({
  102. node: rightToken,
  103. loc: rightToken.loc.start,
  104. message: "Multiple spaces found before '{{displayValue}}'.",
  105. data: { displayValue },
  106. fix: fixer => fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ")
  107. });
  108. });
  109. }
  110. };
  111. }
  112. };