UNPKG

2.85 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 = 'Replace block comment with a single-line comment';
20var Rule = (function (_super) {
21 __extends(Rule, _super);
22 function Rule() {
23 return _super !== null && _super.apply(this, arguments) || this;
24 }
25 Rule.prototype.apply = function (sourceFile) {
26 return this.applyWithFunction(sourceFile, walk);
27 };
28 Rule.metadata = {
29 ruleName: 'no-single-line-block-comment',
30 type: 'maintainability',
31 description: 'Avoid single line block comments; use single line comments instead',
32 options: null,
33 optionsDescription: '',
34 typescriptOnly: true,
35 issueClass: 'Non-SDL',
36 issueType: 'Warning',
37 severity: 'Low',
38 level: 'Opportunity for Excellence',
39 group: 'Whitespace',
40 commonWeaknessEnumeration: '710'
41 };
42 return Rule;
43}(Lint.Rules.AbstractRule));
44exports.Rule = Rule;
45function walk(ctx) {
46 function cb(node) {
47 tsutils_1.forEachTokenWithTrivia(node, function (fullText, tokenSyntaxKind, range) {
48 var tokenText = fullText.substring(range.pos, range.end);
49 if (tokenSyntaxKind === ts.SyntaxKind.MultiLineCommentTrivia &&
50 isSingleLineComment(tokenText) &&
51 !isTsLintSuppression(tokenText) &&
52 !isFollowedByMoreCodeOnSameLine(fullText, range)) {
53 ctx.addFailureAt(range.pos, range.end - range.pos, FAILURE_STRING);
54 }
55 });
56 }
57 return ts.forEachChild(ctx.sourceFile, cb);
58 function isSingleLineComment(commentText) {
59 var lines = commentText.split(/\r?\n/);
60 return lines.length === 1;
61 }
62 function isTsLintSuppression(commentText) {
63 return /\/*\s*tslint:(enable|disable):.*/.test(commentText);
64 }
65 function isFollowedByMoreCodeOnSameLine(fullText, range) {
66 var restOfText = fullText.substring(range.end);
67 return /^\s*\r?\n/.test(restOfText) === false;
68 }
69}
70//# sourceMappingURL=noSingleLineBlockCommentRule.js.map
\No newline at end of file