UNPKG

tslint-ban-snippets

Version:

A custom tslint rule to ban configurable lists of code snippets.

79 lines 3.65 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { 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 extendStatics(d, b); } 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 Lint = require("tslint"); var ConfigFactory_1 = require("./config/ConfigFactory"); var ruleIds_1 = require("./ruleIds"); var GeneralRuleUtils_1 = require("./utils/GeneralRuleUtils"); var Rule = /** @class */ (function (_super) { __extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.prototype.apply = function (sourceFile) { var config = ConfigFactory_1.ConfigFactory.createForBanSnippetsRule(this.getOptions()); var walker = new StatementsWalker(sourceFile, this.getOptions(), config); this.applyWithWalker(walker); return walker.getFailures(); }; return Rule; }(Lint.Rules.AbstractRule)); exports.Rule = Rule; var StatementsWalker = /** @class */ (function (_super) { __extends(StatementsWalker, _super); function StatementsWalker(sourceFile, options, config) { var _this = _super.call(this, sourceFile, options) || this; _this.config = config; return _this; } StatementsWalker.prototype.visitReturnStatement = function (node) { this.visitSomeNode(node); }; StatementsWalker.prototype.visitBinaryExpression = function (node) { this.visitSomeNode(node); _super.prototype.visitBinaryExpression.call(this, node); }; StatementsWalker.prototype.visitCallExpression = function (node) { this.visitSomeNode(node); _super.prototype.visitCallExpression.call(this, node); }; StatementsWalker.prototype.visitDebuggerStatement = function (node) { this.visitSomeNode(node); _super.prototype.visitDebuggerStatement.call(this, node); }; StatementsWalker.prototype.visitSomeNode = function (node) { var _this = this; var text = node.getText(); var relevantBanned = this.getRelevantBanned(); relevantBanned.forEach(function (banned) { var bannedCodeFound = banned.snippets.filter(function (bannedSnippet) { return text.indexOf(bannedSnippet) >= 0; }); if (bannedCodeFound.length > 0) { var failureNode = node.getFirstToken() || node; _this.addFailureAtNode(failureNode, GeneralRuleUtils_1.GeneralRuleUtils.buildFailureString(banned.message || "Do not use banned code '" + bannedCodeFound.join("' or '") + "'.", ruleIds_1.BAN_SNIPPETS_RULE_ID)); } }); }; StatementsWalker.prototype.getRelevantBanned = function () { var sourceFilePath = this.getSourceFile().fileName; return this.config.banned.filter(function (b) { return (!b.includePaths || GeneralRuleUtils_1.GeneralRuleUtils.isFileInPaths(sourceFilePath, b.includePaths)) && (!b.excludePaths || !GeneralRuleUtils_1.GeneralRuleUtils.isFileInPaths(sourceFilePath, b.excludePaths)); }); }; return StatementsWalker; }(Lint.RuleWalker)); //# sourceMappingURL=tslBanSnippetsRule.js.map