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-warning-comments.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @fileoverview Rule that warns about used warning comments
  3. * @author Alexander Schmidt <https://github.com/lxanders>
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow specified warning terms in comments",
  15. category: "Best Practices",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-warning-comments"
  18. },
  19. schema: [
  20. {
  21. type: "object",
  22. properties: {
  23. terms: {
  24. type: "array",
  25. items: {
  26. type: "string"
  27. }
  28. },
  29. location: {
  30. enum: ["start", "anywhere"]
  31. }
  32. },
  33. additionalProperties: false
  34. }
  35. ]
  36. },
  37. create(context) {
  38. const sourceCode = context.getSourceCode(),
  39. configuration = context.options[0] || {},
  40. warningTerms = configuration.terms || ["todo", "fixme", "xxx"],
  41. location = configuration.location || "start",
  42. selfConfigRegEx = /\bno-warning-comments\b/;
  43. /**
  44. * Convert a warning term into a RegExp which will match a comment containing that whole word in the specified
  45. * location ("start" or "anywhere"). If the term starts or ends with non word characters, then the match will not
  46. * require word boundaries on that side.
  47. *
  48. * @param {string} term A term to convert to a RegExp
  49. * @returns {RegExp} The term converted to a RegExp
  50. */
  51. function convertToRegExp(term) {
  52. const escaped = term.replace(/[-/\\$^*+?.()|[\]{}]/g, "\\$&");
  53. const wordBoundary = "\\b";
  54. const eitherOrWordBoundary = `|${wordBoundary}`;
  55. let prefix;
  56. /*
  57. * If the term ends in a word character (a-z0-9_), ensure a word
  58. * boundary at the end, so that substrings do not get falsely
  59. * matched. eg "todo" in a string such as "mastodon".
  60. * If the term ends in a non-word character, then \b won't match on
  61. * the boundary to the next non-word character, which would likely
  62. * be a space. For example `/\bFIX!\b/.test('FIX! blah') === false`.
  63. * In these cases, use no bounding match. Same applies for the
  64. * prefix, handled below.
  65. */
  66. const suffix = /\w$/.test(term) ? "\\b" : "";
  67. if (location === "start") {
  68. /*
  69. * When matching at the start, ignore leading whitespace, and
  70. * there's no need to worry about word boundaries.
  71. */
  72. prefix = "^\\s*";
  73. } else if (/^\w/.test(term)) {
  74. prefix = wordBoundary;
  75. } else {
  76. prefix = "";
  77. }
  78. if (location === "start") {
  79. /*
  80. * For location "start" the regex should be
  81. * ^\s*TERM\b. This checks the word boundary
  82. * at the beginning of the comment.
  83. */
  84. return new RegExp(prefix + escaped + suffix, "i");
  85. }
  86. /*
  87. * For location "anywhere" the regex should be
  88. * \bTERM\b|\bTERM\b, this checks the entire comment
  89. * for the term.
  90. */
  91. return new RegExp(prefix + escaped + suffix + eitherOrWordBoundary + term + wordBoundary, "i");
  92. }
  93. const warningRegExps = warningTerms.map(convertToRegExp);
  94. /**
  95. * Checks the specified comment for matches of the configured warning terms and returns the matches.
  96. * @param {string} comment The comment which is checked.
  97. * @returns {Array} All matched warning terms for this comment.
  98. */
  99. function commentContainsWarningTerm(comment) {
  100. const matches = [];
  101. warningRegExps.forEach((regex, index) => {
  102. if (regex.test(comment)) {
  103. matches.push(warningTerms[index]);
  104. }
  105. });
  106. return matches;
  107. }
  108. /**
  109. * Checks the specified node for matching warning comments and reports them.
  110. * @param {ASTNode} node The AST node being checked.
  111. * @returns {void} undefined.
  112. */
  113. function checkComment(node) {
  114. if (astUtils.isDirectiveComment(node) && selfConfigRegEx.test(node.value)) {
  115. return;
  116. }
  117. const matches = commentContainsWarningTerm(node.value);
  118. matches.forEach(matchedTerm => {
  119. context.report({
  120. node,
  121. message: "Unexpected '{{matchedTerm}}' comment.",
  122. data: {
  123. matchedTerm
  124. }
  125. });
  126. });
  127. }
  128. return {
  129. Program() {
  130. const comments = sourceCode.getAllComments();
  131. comments.filter(token => token.type !== "Shebang").forEach(checkComment);
  132. }
  133. };
  134. }
  135. };