1 | 'use strict';
|
2 |
|
3 | const containsString = require('../../utils/containsString');
|
4 | const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
|
5 | const report = require('../../utils/report');
|
6 | const ruleMessages = require('../../utils/ruleMessages');
|
7 | const validateOptions = require('../../utils/validateOptions');
|
8 | const { isRegExp, isString } = require('../../utils/validateTypes');
|
9 |
|
10 | const ruleName = 'comment-word-disallowed-list';
|
11 |
|
12 | const messages = ruleMessages(ruleName, {
|
13 | rejected: (pattern) => `Unexpected word matching pattern "${pattern}"`,
|
14 | });
|
15 |
|
16 |
|
17 | const rule = (primary) => {
|
18 | return (root, result) => {
|
19 | const validOptions = validateOptions(result, ruleName, {
|
20 | actual: primary,
|
21 | possible: [isString, isRegExp],
|
22 | });
|
23 |
|
24 | if (!validOptions) {
|
25 | return;
|
26 | }
|
27 |
|
28 | root.walkComments((comment) => {
|
29 | const text = comment.text;
|
30 | const rawComment = comment.toString();
|
31 | const firstFourChars = rawComment.substr(0, 4);
|
32 |
|
33 |
|
34 | if (firstFourChars === '/*# ') {
|
35 | return;
|
36 | }
|
37 |
|
38 | const matchesWord = matchesStringOrRegExp(text, primary) || containsString(text, primary);
|
39 |
|
40 | if (!matchesWord) {
|
41 | return;
|
42 | }
|
43 |
|
44 | report({
|
45 | message: messages.rejected(matchesWord.pattern),
|
46 | node: comment,
|
47 | result,
|
48 | ruleName,
|
49 | });
|
50 | });
|
51 | };
|
52 | };
|
53 |
|
54 | rule.primaryOptionArray = true;
|
55 |
|
56 | rule.ruleName = ruleName;
|
57 | rule.messages = messages;
|
58 | module.exports = rule;
|