UNPKG

3.31 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var tsutils_1 = require("tsutils");
16var FAILURE_STRING = 'Replace block comment with a single-line comment';
17var Rule = (function (_super) {
18 __extends(Rule, _super);
19 function Rule() {
20 return _super !== null && _super.apply(this, arguments) || this;
21 }
22 Rule.prototype.apply = function (sourceFile) {
23 return this.applyWithWalker(new NoSingleLineBlockCommentRuleWalker(sourceFile, this.getOptions()));
24 };
25 Rule.metadata = {
26 ruleName: 'no-single-line-block-comment',
27 type: 'maintainability',
28 description: 'Avoid single line block comments; use single line comments instead',
29 options: null,
30 optionsDescription: '',
31 typescriptOnly: true,
32 issueClass: 'Non-SDL',
33 issueType: 'Warning',
34 severity: 'Low',
35 level: 'Opportunity for Excellence',
36 group: 'Whitespace',
37 commonWeaknessEnumeration: '710'
38 };
39 return Rule;
40}(Lint.Rules.AbstractRule));
41exports.Rule = Rule;
42var NoSingleLineBlockCommentRuleWalker = (function (_super) {
43 __extends(NoSingleLineBlockCommentRuleWalker, _super);
44 function NoSingleLineBlockCommentRuleWalker() {
45 return _super !== null && _super.apply(this, arguments) || this;
46 }
47 NoSingleLineBlockCommentRuleWalker.prototype.visitSourceFile = function (node) {
48 var _this = this;
49 tsutils_1.forEachTokenWithTrivia(node, function (fullText, tokenSyntaxKind, range) {
50 var tokenText = fullText.substring(range.pos, range.end);
51 if (tokenSyntaxKind === ts.SyntaxKind.MultiLineCommentTrivia
52 && _this.isSingleLineComment(tokenText)
53 && !_this.isTsLintSuppression(tokenText)
54 && !_this.isFollowedByMoreCodeOnSameLine(fullText, range)) {
55 _this.addFailureAt(range.pos, range.end - range.pos, FAILURE_STRING);
56 }
57 });
58 };
59 NoSingleLineBlockCommentRuleWalker.prototype.isSingleLineComment = function (commentText) {
60 var lines = commentText.split(/\r?\n/);
61 return lines.length === 1;
62 };
63 NoSingleLineBlockCommentRuleWalker.prototype.isTsLintSuppression = function (commentText) {
64 return /\/*\s*tslint:(enable|disable):.*/.test(commentText);
65 };
66 NoSingleLineBlockCommentRuleWalker.prototype.isFollowedByMoreCodeOnSameLine = function (fullText, range) {
67 var restOfText = fullText.substring(range.end);
68 return /^\s*\r?\n/.test(restOfText) === false;
69 };
70 return NoSingleLineBlockCommentRuleWalker;
71}(Lint.RuleWalker));
72//# sourceMappingURL=noSingleLineBlockCommentRule.js.map
\No newline at end of file