Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-multi-spaces.js 4.8KB

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