tslint-ban-snippets
Version:
A custom tslint rule to ban configurable lists of code snippets.
91 lines • 3.9 kB
JavaScript
;
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rule = void 0;
var Lint = require("tslint");
var ts = require("typescript");
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());
return this.applyWithFunction(sourceFile, walk, config);
};
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var walk = function (ctx) {
return ts.forEachChild(ctx.sourceFile, checkNode);
function checkNode(node) {
if ([
ts.SyntaxKind.ReturnStatement,
ts.SyntaxKind.BinaryExpression,
ts.SyntaxKind.CallExpression,
ts.SyntaxKind.DebuggerStatement
].includes(node.kind)) {
visitSomeNode(node, ctx);
}
return ts.forEachChild(node, checkNode);
}
};
function visitSomeNode(node, ctx) {
var text = node.getText();
var relevantBanned = getRelevantBanned(ctx);
relevantBanned.forEach(function (banned) {
if (banned.snippets) {
checkBannedSnippet(banned.snippets, text, node, ctx, banned.message);
}
else if (banned.regexSnippets) {
checkRegexBannedSnippet(banned.regexSnippets, text, node, ctx, banned.message);
}
else {
throw new Error("Invalid config? No snippets and no regex-snippets");
}
});
}
function checkBannedSnippet(snippets, text, node, ctx, message) {
var bannedCodeFound = snippets.filter(function (bannedSnippet) { return text.indexOf(bannedSnippet) >= 0; });
if (bannedCodeFound.length > 0) {
failRule(node, bannedCodeFound, ctx, message);
}
}
function checkRegexBannedSnippet(regexSnippets, text, node, ctx, message) {
var bannedCodeFound = regexSnippets.filter(function (regexSnippet) {
var regex = new RegExp(regexSnippet);
return regex.test(text);
});
if (bannedCodeFound.length > 0) {
failRule(node, bannedCodeFound, ctx, message);
}
}
function failRule(node, bannedCodeFound, ctx, message) {
var failureNode = node.getFirstToken() || node;
ctx.addFailureAtNode(failureNode, GeneralRuleUtils_1.GeneralRuleUtils.buildFailureString(message || "Do not use banned code '".concat(bannedCodeFound.join("' or '"), "'."), ruleIds_1.BAN_SNIPPETS_RULE_ID));
}
function getRelevantBanned(ctx) {
var sourceFilePath = ctx.sourceFile.fileName;
return ctx.options.banned.filter(function (b) {
return (!b.includePaths || GeneralRuleUtils_1.GeneralRuleUtils.isFileInPaths(sourceFilePath, b.includePaths)) &&
(!b.excludePaths || !GeneralRuleUtils_1.GeneralRuleUtils.isFileInPaths(sourceFilePath, b.excludePaths));
});
}
//# sourceMappingURL=tslBanSnippetsRule.js.map