This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdisallow-keywords-in-comments.js
80 lines (73 loc) · 2.28 KB
/
disallow-keywords-in-comments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Disallows keywords in your comments, such as TODO or FIXME
*
* Types: `Boolean`, `String` or `Array`
*
* Values:
* - `true`
* - `'\b(word1|word2)\b'`
* - `['word1', 'word2']`
*
* #### Examples
*
* ```js
* "disallowKeywordsInComments": true
* "disallowKeywordsInComments": "\\b(word1|word2)\\b"
* "disallowKeywordsInComments": ["word1", "word2"]
* ```
*
* #### Invalid:
* ```
* // ToDo
* //TODO
* /** fixme *\/
* /**
* * FIXME
* *\/
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(keywords) {
this._message = 'Comments cannot contain the following keywords: ';
this._keywords = ['todo', 'fixme'];
switch (true) {
case Array.isArray(keywords):
// use the array of strings provided to build RegExp pattern
this._keywords = keywords;
/* falls through */
case keywords:
// use default keywords
this._message += this._keywords.join(', ');
this._keywordRegEx = new RegExp('\\b(' + this._keywords.join('|') + ')\\b', 'gi');
break;
case typeof keywords === 'string':
// use string passed in as the RegExp pattern
this._message = 'Comments cannot contain keywords based on the expression you provided';
this._keywordRegEx = new RegExp(keywords, 'gi');
break;
default:
assert(false, this.getOptionName() + ' option requires a true value, a string or an array');
}
},
getOptionName: function() {
return 'disallowKeywordsInComments';
},
check: function(file, errors) {
var keywordRegEx = this._keywordRegEx;
file.iterateTokensByType(['CommentLine', 'CommentBlock'], function(comment) {
var match;
// Both '//' and '/*' comment starters have offset '2'
var commentOffset = 2;
keywordRegEx.lastIndex = 0;
while ((match = keywordRegEx.exec(comment.value)) !== null) {
errors.add(
this.mesage,
comment,
match.index + commentOffset
);
}
});
}
};