no-multi-spaces.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "layout",
  14. docs: {
  15. description: "Disallow multiple spaces",
  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. messages: {
  42. multipleSpaces: "Multiple spaces found before '{{displayValue}}'."
  43. }
  44. },
  45. create(context) {
  46. const sourceCode = context.getSourceCode();
  47. const options = context.options[0] || {};
  48. const ignoreEOLComments = options.ignoreEOLComments;
  49. const exceptions = Object.assign({ Property: true }, options.exceptions);
  50. const hasExceptions = Object.keys(exceptions).some(key => exceptions[key]);
  51. /**
  52. * Formats value of given comment token for error message by truncating its length.
  53. * @param {Token} token comment token
  54. * @returns {string} formatted value
  55. * @private
  56. */
  57. function formatReportedCommentValue(token) {
  58. const valueLines = token.value.split("\n");
  59. const value = valueLines[0];
  60. const formattedValue = `${value.slice(0, 12)}...`;
  61. return valueLines.length === 1 && value.length <= 12 ? value : formattedValue;
  62. }
  63. //--------------------------------------------------------------------------
  64. // Public
  65. //--------------------------------------------------------------------------
  66. return {
  67. Program() {
  68. sourceCode.tokensAndComments.forEach((leftToken, leftIndex, tokensAndComments) => {
  69. if (leftIndex === tokensAndComments.length - 1) {
  70. return;
  71. }
  72. const rightToken = tokensAndComments[leftIndex + 1];
  73. // Ignore tokens that don't have 2 spaces between them or are on different lines
  74. if (
  75. !sourceCode.text.slice(leftToken.range[1], rightToken.range[0]).includes(" ") ||
  76. leftToken.loc.end.line < rightToken.loc.start.line
  77. ) {
  78. return;
  79. }
  80. // Ignore comments that are the last token on their line if `ignoreEOLComments` is active.
  81. if (
  82. ignoreEOLComments &&
  83. astUtils.isCommentToken(rightToken) &&
  84. (
  85. leftIndex === tokensAndComments.length - 2 ||
  86. rightToken.loc.end.line < tokensAndComments[leftIndex + 2].loc.start.line
  87. )
  88. ) {
  89. return;
  90. }
  91. // Ignore tokens that are in a node in the "exceptions" object
  92. if (hasExceptions) {
  93. const parentNode = sourceCode.getNodeByRangeIndex(rightToken.range[0] - 1);
  94. if (parentNode && exceptions[parentNode.type]) {
  95. return;
  96. }
  97. }
  98. let displayValue;
  99. if (rightToken.type === "Block") {
  100. displayValue = `/*${formatReportedCommentValue(rightToken)}*/`;
  101. } else if (rightToken.type === "Line") {
  102. displayValue = `//${formatReportedCommentValue(rightToken)}`;
  103. } else {
  104. displayValue = rightToken.value;
  105. }
  106. context.report({
  107. node: rightToken,
  108. loc: { start: leftToken.loc.end, end: rightToken.loc.start },
  109. messageId: "multipleSpaces",
  110. data: { displayValue },
  111. fix: fixer => fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ")
  112. });
  113. });
  114. }
  115. };
  116. }
  117. };