no-multi-spaces.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @fileoverview Disallow use of multiple spaces.
  3. * @author Nicholas C. Zakas
  4. * @deprecated in ESLint v8.53.0
  5. */
  6. "use strict";
  7. const astUtils = require("./utils/ast-utils");
  8. //------------------------------------------------------------------------------
  9. // Rule Definition
  10. //------------------------------------------------------------------------------
  11. /** @type {import('../types').Rule.RuleModule} */
  12. module.exports = {
  13. meta: {
  14. deprecated: {
  15. message: "Formatting rules are being moved out of ESLint core.",
  16. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  17. deprecatedSince: "8.53.0",
  18. availableUntil: "11.0.0",
  19. replacedBy: [
  20. {
  21. message:
  22. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  23. url: "https://eslint.style/guide/migration",
  24. plugin: {
  25. name: "@stylistic/eslint-plugin",
  26. url: "https://eslint.style",
  27. },
  28. rule: {
  29. name: "no-multi-spaces",
  30. url: "https://eslint.style/rules/no-multi-spaces",
  31. },
  32. },
  33. ],
  34. },
  35. type: "layout",
  36. docs: {
  37. description: "Disallow multiple spaces",
  38. recommended: false,
  39. url: "https://eslint.org/docs/latest/rules/no-multi-spaces",
  40. },
  41. fixable: "whitespace",
  42. schema: [
  43. {
  44. type: "object",
  45. properties: {
  46. exceptions: {
  47. type: "object",
  48. patternProperties: {
  49. "^([A-Z][a-z]*)+$": {
  50. type: "boolean",
  51. },
  52. },
  53. additionalProperties: false,
  54. },
  55. ignoreEOLComments: {
  56. type: "boolean",
  57. default: false,
  58. },
  59. },
  60. additionalProperties: false,
  61. },
  62. ],
  63. messages: {
  64. multipleSpaces: "Multiple spaces found before '{{displayValue}}'.",
  65. },
  66. },
  67. create(context) {
  68. const sourceCode = context.sourceCode;
  69. const options = context.options[0] || {};
  70. const ignoreEOLComments = options.ignoreEOLComments;
  71. const exceptions = Object.assign(
  72. { Property: true },
  73. options.exceptions,
  74. );
  75. const hasExceptions = Object.keys(exceptions).some(
  76. key => exceptions[key],
  77. );
  78. /**
  79. * Formats value of given comment token for error message by truncating its length.
  80. * @param {Token} token comment token
  81. * @returns {string} formatted value
  82. * @private
  83. */
  84. function formatReportedCommentValue(token) {
  85. const valueLines = token.value.split("\n");
  86. const value = valueLines[0];
  87. const formattedValue = `${value.slice(0, 12)}...`;
  88. return valueLines.length === 1 && value.length <= 12
  89. ? value
  90. : formattedValue;
  91. }
  92. //--------------------------------------------------------------------------
  93. // Public
  94. //--------------------------------------------------------------------------
  95. return {
  96. Program() {
  97. sourceCode.tokensAndComments.forEach(
  98. (leftToken, leftIndex, tokensAndComments) => {
  99. if (leftIndex === tokensAndComments.length - 1) {
  100. return;
  101. }
  102. const rightToken = tokensAndComments[leftIndex + 1];
  103. // Ignore tokens that don't have 2 spaces between them or are on different lines
  104. if (
  105. !sourceCode.text
  106. .slice(leftToken.range[1], rightToken.range[0])
  107. .includes(" ") ||
  108. leftToken.loc.end.line < rightToken.loc.start.line
  109. ) {
  110. return;
  111. }
  112. // Ignore comments that are the last token on their line if `ignoreEOLComments` is active.
  113. if (
  114. ignoreEOLComments &&
  115. astUtils.isCommentToken(rightToken) &&
  116. (leftIndex === tokensAndComments.length - 2 ||
  117. rightToken.loc.end.line <
  118. tokensAndComments[leftIndex + 2].loc.start
  119. .line)
  120. ) {
  121. return;
  122. }
  123. // Ignore tokens that are in a node in the "exceptions" object
  124. if (hasExceptions) {
  125. const parentNode = sourceCode.getNodeByRangeIndex(
  126. rightToken.range[0] - 1,
  127. );
  128. if (parentNode && exceptions[parentNode.type]) {
  129. return;
  130. }
  131. }
  132. let displayValue;
  133. if (rightToken.type === "Block") {
  134. displayValue = `/*${formatReportedCommentValue(rightToken)}*/`;
  135. } else if (rightToken.type === "Line") {
  136. displayValue = `//${formatReportedCommentValue(rightToken)}`;
  137. } else {
  138. displayValue = rightToken.value;
  139. }
  140. context.report({
  141. node: rightToken,
  142. loc: {
  143. start: leftToken.loc.end,
  144. end: rightToken.loc.start,
  145. },
  146. messageId: "multipleSpaces",
  147. data: { displayValue },
  148. fix: fixer =>
  149. fixer.replaceTextRange(
  150. [leftToken.range[1], rightToken.range[0]],
  151. " ",
  152. ),
  153. });
  154. },
  155. );
  156. },
  157. };
  158. },
  159. };