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-trailing-spaces.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * @fileoverview Disallow trailing spaces at the end of lines.
  3. * @author Nodeca Team <https://github.com/nodeca>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "disallow trailing whitespace at the end of lines",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-trailing-spaces"
  21. },
  22. fixable: "whitespace",
  23. schema: [
  24. {
  25. type: "object",
  26. properties: {
  27. skipBlankLines: {
  28. type: "boolean"
  29. },
  30. ignoreComments: {
  31. type: "boolean"
  32. }
  33. },
  34. additionalProperties: false
  35. }
  36. ]
  37. },
  38. create(context) {
  39. const sourceCode = context.getSourceCode();
  40. const BLANK_CLASS = "[ \t\u00a0\u2000-\u200b\u3000]",
  41. SKIP_BLANK = `^${BLANK_CLASS}*$`,
  42. NONBLANK = `${BLANK_CLASS}+$`;
  43. const options = context.options[0] || {},
  44. skipBlankLines = options.skipBlankLines || false,
  45. ignoreComments = typeof options.ignoreComments === "boolean" && options.ignoreComments;
  46. /**
  47. * Report the error message
  48. * @param {ASTNode} node node to report
  49. * @param {int[]} location range information
  50. * @param {int[]} fixRange Range based on the whole program
  51. * @returns {void}
  52. */
  53. function report(node, location, fixRange) {
  54. /*
  55. * Passing node is a bit dirty, because message data will contain big
  56. * text in `source`. But... who cares :) ?
  57. * One more kludge will not make worse the bloody wizardry of this
  58. * plugin.
  59. */
  60. context.report({
  61. node,
  62. loc: location,
  63. message: "Trailing spaces not allowed.",
  64. fix(fixer) {
  65. return fixer.removeRange(fixRange);
  66. }
  67. });
  68. }
  69. /**
  70. * Given a list of comment nodes, return the line numbers for those comments.
  71. * @param {Array} comments An array of comment nodes.
  72. * @returns {number[]} An array of line numbers containing comments.
  73. */
  74. function getCommentLineNumbers(comments) {
  75. const lines = new Set();
  76. comments.forEach(comment => {
  77. for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) {
  78. lines.add(i);
  79. }
  80. });
  81. return lines;
  82. }
  83. //--------------------------------------------------------------------------
  84. // Public
  85. //--------------------------------------------------------------------------
  86. return {
  87. Program: function checkTrailingSpaces(node) {
  88. /*
  89. * Let's hack. Since Espree does not return whitespace nodes,
  90. * fetch the source code and do matching via regexps.
  91. */
  92. const re = new RegExp(NONBLANK),
  93. skipMatch = new RegExp(SKIP_BLANK),
  94. lines = sourceCode.lines,
  95. linebreaks = sourceCode.getText().match(astUtils.createGlobalLinebreakMatcher()),
  96. comments = sourceCode.getAllComments(),
  97. commentLineNumbers = getCommentLineNumbers(comments);
  98. let totalLength = 0,
  99. fixRange = [];
  100. for (let i = 0, ii = lines.length; i < ii; i++) {
  101. const matches = re.exec(lines[i]);
  102. /*
  103. * Always add linebreak length to line length to accommodate for line break (\n or \r\n)
  104. * Because during the fix time they also reserve one spot in the array.
  105. * Usually linebreak length is 2 for \r\n (CRLF) and 1 for \n (LF)
  106. */
  107. const linebreakLength = linebreaks && linebreaks[i] ? linebreaks[i].length : 1;
  108. const lineLength = lines[i].length + linebreakLength;
  109. if (matches) {
  110. const location = {
  111. line: i + 1,
  112. column: matches.index
  113. };
  114. const rangeStart = totalLength + location.column;
  115. const rangeEnd = totalLength + lineLength - linebreakLength;
  116. const containingNode = sourceCode.getNodeByRangeIndex(rangeStart);
  117. if (containingNode && containingNode.type === "TemplateElement" &&
  118. rangeStart > containingNode.parent.range[0] &&
  119. rangeEnd < containingNode.parent.range[1]) {
  120. totalLength += lineLength;
  121. continue;
  122. }
  123. /*
  124. * If the line has only whitespace, and skipBlankLines
  125. * is true, don't report it
  126. */
  127. if (skipBlankLines && skipMatch.test(lines[i])) {
  128. totalLength += lineLength;
  129. continue;
  130. }
  131. fixRange = [rangeStart, rangeEnd];
  132. if (!ignoreComments || !commentLineNumbers.has(location.line)) {
  133. report(node, location, fixRange);
  134. }
  135. }
  136. totalLength += lineLength;
  137. }
  138. }
  139. };
  140. }
  141. };