]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-warning-comments.js
23e3da35e146726b4bbd2249aacc7f442ea4cc4c
2 * @fileoverview Rule that warns about used warning comments
3 * @author Alexander Schmidt <https://github.com/lxanders>
8 const escapeRegExp
= require("escape-string-regexp");
9 const astUtils
= require("./utils/ast-utils");
11 const CHAR_LIMIT
= 40;
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
22 description
: "disallow specified warning terms in comments",
24 url
: "https://eslint.org/docs/rules/no-warning-comments"
38 enum: ["start", "anywhere"]
41 additionalProperties
: false
46 unexpectedComment
: "Unexpected '{{matchedTerm}}' comment: '{{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
114 eitherOrWordBoundary
+
121 const warningRegExps
= warningTerms
.map(convertToRegExp
);
124 * Checks the specified comment for matches of the configured warning terms and returns the matches.
125 * @param {string} comment The comment which is checked.
126 * @returns {Array} All matched warning terms for this comment.
128 function commentContainsWarningTerm(comment
) {
131 warningRegExps
.forEach((regex
, index
) => {
132 if (regex
.test(comment
)) {
133 matches
.push(warningTerms
[index
]);
141 * Checks the specified node for matching warning comments and reports them.
142 * @param {ASTNode} node The AST node being checked.
143 * @returns {void} undefined.
145 function checkComment(node
) {
146 const comment
= node
.value
;
149 astUtils
.isDirectiveComment(node
) &&
150 selfConfigRegEx
.test(comment
)
155 const matches
= commentContainsWarningTerm(comment
);
157 matches
.forEach(matchedTerm
=> {
158 let commentToDisplay
= "";
159 let truncated
= false;
161 for (const c
of comment
.trim().split(/\s+/u)) {
162 const tmp
= commentToDisplay
? `${commentToDisplay} ${c}` : c
;
164 if (tmp
.length
<= CHAR_LIMIT
) {
165 commentToDisplay
= tmp
;
174 messageId
: "unexpectedComment",
177 comment
: `${commentToDisplay}${
178 truncated ? "..." : ""
187 const comments
= sourceCode
.getAllComments();
190 .filter(token
=> token
.type
!== "Shebang")
191 .forEach(checkComment
);