UNPKG

4.83 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils_1 = require("tsutils");
19var FAILURE_STRING = 'Suspicious comment found: ';
20var SUSPICIOUS_WORDS = ['BUG', 'HACK', 'FIXME', 'LATER', 'LATER2', 'TODO'];
21var FAILURE_STRING_OPTION = '\nTo disable this warning, the comment should include one of the following regex: ';
22var Rule = (function (_super) {
23 __extends(Rule, _super);
24 function Rule() {
25 return _super !== null && _super.apply(this, arguments) || this;
26 }
27 Rule.prototype.apply = function (sourceFile) {
28 return this.applyWithFunction(sourceFile, walk, parseOptions(this.getOptions()));
29 };
30 Rule.metadata = {
31 ruleName: 'no-suspicious-comment',
32 type: 'maintainability',
33 description: "Do not use suspicious comments, such as " + SUSPICIOUS_WORDS.join(', '),
34 options: {
35 type: 'array',
36 items: {
37 type: 'string'
38 }
39 },
40 optionsDescription: "One argument may be optionally provided: \n\n' +\n '* an array of regex that disable the warning if one or several of them\n are found in the comment text. (Example: `['https://github.com/my-org/my-project/*']`)",
41 typescriptOnly: true,
42 issueClass: 'Non-SDL',
43 issueType: 'Warning',
44 severity: 'Low',
45 level: 'Opportunity for Excellence',
46 group: 'Clarity',
47 commonWeaknessEnumeration: '546'
48 };
49 return Rule;
50}(Lint.Rules.AbstractRule));
51exports.Rule = Rule;
52function parseOptions(options) {
53 var value = [];
54 (options.ruleArguments || []).forEach(function (regexStr) {
55 value.push(new RegExp(regexStr));
56 });
57 return {
58 exceptionRegex: value
59 };
60}
61function walk(ctx) {
62 var exceptionRegex = ctx.options.exceptionRegex;
63 function cb(node) {
64 tsutils_1.forEachTokenWithTrivia(node, function (text, tokenSyntaxKind, range) {
65 if (tokenSyntaxKind === ts.SyntaxKind.SingleLineCommentTrivia || tokenSyntaxKind === ts.SyntaxKind.MultiLineCommentTrivia) {
66 scanCommentForSuspiciousWords(range.pos, text.substring(range.pos, range.end));
67 }
68 });
69 }
70 return ts.forEachChild(ctx.sourceFile, cb);
71 function scanCommentForSuspiciousWords(startPosition, commentText) {
72 if (commentContainsExceptionRegex(exceptionRegex, commentText)) {
73 return;
74 }
75 SUSPICIOUS_WORDS.forEach(function (suspiciousWord) {
76 scanCommentForSuspiciousWord(suspiciousWord, commentText, startPosition);
77 });
78 }
79 function scanCommentForSuspiciousWord(suspiciousWord, commentText, startPosition) {
80 var regexExactCaseNoColon = new RegExp('\\b' + suspiciousWord + '\\b');
81 var regexCaseInsensistiveWithColon = new RegExp('\\b' + suspiciousWord + '\\b:', 'i');
82 if (regexExactCaseNoColon.test(commentText) || regexCaseInsensistiveWithColon.test(commentText)) {
83 foundSuspiciousComment(startPosition, commentText, suspiciousWord);
84 }
85 }
86 function foundSuspiciousComment(startPosition, commentText, suspiciousWord) {
87 var errorMessage = FAILURE_STRING + suspiciousWord;
88 if (exceptionRegex.length > 0) {
89 errorMessage += '.' + getFailureMessageWithExceptionRegexOption();
90 }
91 ctx.addFailureAt(startPosition, commentText.length, errorMessage);
92 }
93 function commentContainsExceptionRegex(exceptionRegexes, commentText) {
94 for (var _i = 0, exceptionRegexes_1 = exceptionRegexes; _i < exceptionRegexes_1.length; _i++) {
95 var regex = exceptionRegexes_1[_i];
96 if (regex.test(commentText)) {
97 return true;
98 }
99 }
100 return false;
101 }
102 function getFailureMessageWithExceptionRegexOption() {
103 var message = FAILURE_STRING_OPTION;
104 exceptionRegex.forEach(function (regex) {
105 message += regex.toString();
106 });
107 return message;
108 }
109}
110//# sourceMappingURL=noSuspiciousCommentRule.js.map
\No newline at end of file