]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-warning-comments.js
2 * @fileoverview Rule that warns about used warning comments
3 * @author Alexander Schmidt <https://github.com/lxanders>
8 const { escapeRegExp
} = require("lodash");
9 const astUtils
= require("./utils/ast-utils");
11 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
20 description
: "disallow specified warning terms in comments",
21 category
: "Best Practices",
23 url
: "https://eslint.org/docs/rules/no-warning-comments"
37 enum: ["start", "anywhere"]
40 additionalProperties
: false
45 unexpectedComment
: "Unexpected '{{matchedTerm}}' comment."
51 const sourceCode
= context
.getSourceCode(),
52 configuration
= context
.options
[0] || {},
53 warningTerms
= configuration
.terms
|| ["todo", "fixme", "xxx"],
54 location
= configuration
.location
|| "start",
55 selfConfigRegEx
= /\bno-warning-comments\b/u;
58 * Convert a warning term into a RegExp which will match a comment containing that whole word in the specified
59 * location ("start" or "anywhere"). If the term starts or ends with non word characters, then the match will not
60 * require word boundaries on that side.
61 * @param {string} term A term to convert to a RegExp
62 * @returns {RegExp} The term converted to a RegExp
64 function convertToRegExp(term
) {
65 const escaped
= escapeRegExp(term
);
66 const wordBoundary
= "\\b";
67 const eitherOrWordBoundary
= `|${wordBoundary}`;
71 * If the term ends in a word character (a-z0-9_), ensure a word
72 * boundary at the end, so that substrings do not get falsely
73 * matched. eg "todo" in a string such as "mastodon".
74 * If the term ends in a non-word character, then \b won't match on
75 * the boundary to the next non-word character, which would likely
76 * be a space. For example `/\bFIX!\b/.test('FIX! blah') === false`.
77 * In these cases, use no bounding match. Same applies for the
78 * prefix, handled below.
80 const suffix
= /\w$/u.test(term
) ? "\\b" : "";
82 if (location
=== "start") {
85 * When matching at the start, ignore leading whitespace, and
86 * there's no need to worry about word boundaries.
89 } else if (/^\w/u.test(term
)) {
90 prefix
= wordBoundary
;
95 if (location
=== "start") {
98 * For location "start" the regex should be
99 * ^\s*TERM\b. This checks the word boundary
100 * at the beginning of the comment.
102 return new RegExp(prefix
+ escaped
+ suffix
, "iu");
106 * For location "anywhere" the regex should be
107 * \bTERM\b|\bTERM\b, this checks the entire comment
110 return new RegExp(prefix
+ escaped
+ suffix
+ eitherOrWordBoundary
+ term
+ wordBoundary
, "iu");
113 const warningRegExps
= warningTerms
.map(convertToRegExp
);
116 * Checks the specified comment for matches of the configured warning terms and returns the matches.
117 * @param {string} comment The comment which is checked.
118 * @returns {Array} All matched warning terms for this comment.
120 function commentContainsWarningTerm(comment
) {
123 warningRegExps
.forEach((regex
, index
) => {
124 if (regex
.test(comment
)) {
125 matches
.push(warningTerms
[index
]);
133 * Checks the specified node for matching warning comments and reports them.
134 * @param {ASTNode} node The AST node being checked.
135 * @returns {void} undefined.
137 function checkComment(node
) {
138 if (astUtils
.isDirectiveComment(node
) && selfConfigRegEx
.test(node
.value
)) {
142 const matches
= commentContainsWarningTerm(node
.value
);
144 matches
.forEach(matchedTerm
=> {
147 messageId
: "unexpectedComment",
157 const comments
= sourceCode
.getAllComments();
159 comments
.filter(token
=> token
.type
!== "Shebang").forEach(checkComment
);