UNPKG

tslint-clean-code

Version:
116 lines 5.04 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var Lint = require("tslint"); var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker"); var tsutils_1 = require("tsutils"); var FAILURE_STRING = 'No commented out code.'; var Rule = (function (_super) { __extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.prototype.apply = function (sourceFile) { return this.applyWithWalker(new NoCommentedOutCodeRuleWalker(sourceFile, this.getOptions())); }; Rule.metadata = { ruleName: 'no-commented-out-code', type: 'maintainability', description: 'Code must not be commented out.', options: null, optionsDescription: '', optionExamples: [], typescriptOnly: false, issueClass: 'Non-SDL', issueType: 'Warning', severity: 'Low', level: 'Opportunity for Excellence', group: 'Clarity', commonWeaknessEnumeration: '', }; return Rule; }(Lint.Rules.AbstractRule)); exports.Rule = Rule; var NoCommentedOutCodeRuleWalker = (function (_super) { __extends(NoCommentedOutCodeRuleWalker, _super); function NoCommentedOutCodeRuleWalker() { return _super !== null && _super.apply(this, arguments) || this; } NoCommentedOutCodeRuleWalker.prototype.visitSourceFile = function (node) { var _this = this; tsutils_1.forEachTokenWithTrivia(node, function (text, tokenSyntaxKind, range) { if (tokenSyntaxKind === ts.SyntaxKind.SingleLineCommentTrivia || tokenSyntaxKind === ts.SyntaxKind.MultiLineCommentTrivia) { _this.scanCommentForCode(range.pos, text.substring(range.pos, range.end)); } }); }; NoCommentedOutCodeRuleWalker.prototype.scanCommentForCode = function (startPosition, commentText) { var trimmedCommentText = this.trimTextLines(commentText); if (this.isTextCode(trimmedCommentText)) { this.handleSuspiciousComment(startPosition, commentText); } }; NoCommentedOutCodeRuleWalker.prototype.trimTextLines = function (text) { var lines = text.split('\n'); var trimAtStart = /^\s*\/*\**\s*/; var trimAtEnd = /\s*\**\/*\s*$/; var trimmedLines = lines.map(function (line) { return line.replace(trimAtStart, '').replace(trimAtEnd, ''); }); return trimmedLines.join('\n'); }; NoCommentedOutCodeRuleWalker.prototype.isTextCode = function (text) { if (this.isTextSingleWord(text)) { return false; } if (this.isTextTsLintFlag(text)) { return false; } if (this.isTextToDoLikeNote(text)) { return false; } return this.isTextCodeWithoutErrors(text); }; NoCommentedOutCodeRuleWalker.prototype.isTextSingleWord = function (text) { var pattern = new RegExp('^([\\w-]*)$'); return pattern.test(text.trim()); }; NoCommentedOutCodeRuleWalker.prototype.isTextTsLintFlag = function (text) { return text.startsWith('tslint:'); }; NoCommentedOutCodeRuleWalker.prototype.isTextToDoLikeNote = function (text) { return /^(NOTE|TODO|FIXME|BUG|HACK|XXX):/.test(text); }; NoCommentedOutCodeRuleWalker.prototype.isTextCodeWithoutErrors = function (text) { var sourceFile = this.createSourceFileFromText(text); if (!this.hasSourceFileStatements(sourceFile)) { return false; } var sourceFileDiagnostics = this.getSourceFileDiagnostics(sourceFile); return sourceFileDiagnostics.length === 0; }; NoCommentedOutCodeRuleWalker.prototype.createSourceFileFromText = function (text) { return ts.createSourceFile('', text, ts.ScriptTarget.ES5, true); }; NoCommentedOutCodeRuleWalker.prototype.hasSourceFileStatements = function (sourceFile) { return sourceFile && sourceFile.statements.length > 0; }; NoCommentedOutCodeRuleWalker.prototype.getSourceFileDiagnostics = function (sourceFile) { return sourceFile.parseDiagnostics; }; NoCommentedOutCodeRuleWalker.prototype.handleSuspiciousComment = function (startPosition, commentText) { this.addFailureAt(startPosition, commentText.length, FAILURE_STRING); }; return NoCommentedOutCodeRuleWalker; }(ErrorTolerantWalker_1.ErrorTolerantWalker)); //# sourceMappingURL=noCommentedOutCodeRule.js.map