no-multi-spaces.js 4.9 KB

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