UNPKG

rxjs-tslint-rules

Version:
94 lines (93 loc) 4.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Rule = void 0; var tslib_1 = require("tslib"); var Lint = tslib_1.__importStar(require("tslint")); var peer = tslib_1.__importStar(require("../support/peer")); var Rule = (function (_super) { tslib_1.__extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.prototype.apply = function (sourceFile) { return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); }; Rule.metadata = { deprecationMessage: peer.v6 && !peer.compat ? peer.v6NotNeededMessage : undefined, description: "Disallows the importation of patched observables and operators.", options: { properties: { allowObservables: { oneOf: [ { type: "boolean" }, { type: "array", items: { type: "string" } }, ], }, allowOperators: { oneOf: [ { type: "boolean" }, { type: "array", items: { type: "string" } }, ], }, }, type: "object", }, optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n An optional object with optional `allowObservables` and `allowOperators` properties.\n The properties can be specifed as booleans (they default to `false`) or as arrays containing\n the names of the observables or operators that are allowed."], ["\n An optional object with optional \\`allowObservables\\` and \\`allowOperators\\` properties.\n The properties can be specifed as booleans (they default to \\`false\\`) or as arrays containing\n the names of the observables or operators that are allowed."]))), requiresTypeInfo: false, ruleName: "rxjs-no-add", type: "functionality", typescriptOnly: false, }; Rule.FAILURE_STRING = "RxJS add import is forbidden"; return Rule; }(Lint.Rules.AbstractRule)); exports.Rule = Rule; var Walker = (function (_super) { tslib_1.__extends(Walker, _super); function Walker(sourceFile, options) { var _this = _super.call(this, sourceFile, options) || this; _this.allowAllObservables = false; _this.allowAllOperators = false; _this.allowedObservables = []; _this.allowedOperators = []; var _a = tslib_1.__read(_this.getOptions(), 1), ruleOptions = _a[0]; if (ruleOptions) { if (ruleOptions.hasOwnProperty("allowObservables")) { if (typeof ruleOptions.allowObservables.length === "number") { _this.allowedObservables = ruleOptions.allowObservables; } else { _this.allowAllObservables = Boolean(ruleOptions.allowObservables); } } if (ruleOptions.hasOwnProperty("allowOperators")) { if (typeof ruleOptions.allowOperators.length === "number") { _this.allowedOperators = ruleOptions.allowOperators; } else { _this.allowAllOperators = Boolean(ruleOptions.allowOperators); } } } return _this; } Walker.prototype.visitImportDeclaration = function (node) { var moduleSpecifier = node.moduleSpecifier.getText(); var match = moduleSpecifier.match(/^['"]rxjs\/add\/(\w+)\/(\w+)/); if (match) { if (match[1] === "observable" && !this.allowAllObservables) { if (this.allowedObservables.indexOf(match[2]) === -1) { this.addFailureAtNode(node.moduleSpecifier, Rule.FAILURE_STRING); } } else if (match[1] === "operator" && !this.allowAllOperators) { if (this.allowedOperators.indexOf(match[2]) === -1) { this.addFailureAtNode(node.moduleSpecifier, Rule.FAILURE_STRING); } } } _super.prototype.visitImportDeclaration.call(this, node); }; return Walker; }(Lint.RuleWalker)); var templateObject_1;