UNPKG

rxjs-tslint-rules

Version:
92 lines (91 loc) 4.38 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 tsutils = tslib_1.__importStar(require("tsutils")); var ts = tslib_1.__importStar(require("typescript")); var peer = tslib_1.__importStar(require("../support/peer")); var util_1 = require("../support/util"); var defaultAllowedTypesRegExp = /^EventEmitter$/; var Rule = (function (_super) { tslib_1.__extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.prototype.applyWithProgram = function (sourceFile, program) { return this.applyWithWalker(new RxjsNoExposedSubjects(sourceFile, this.getOptions(), program)); }; Rule.metadata = { deprecationMessage: peer.v5 ? peer.v5NotSupportedMessage : undefined, description: "Disallows exposed subjects.", options: { properties: { allowProtected: { type: "boolean" }, }, type: "object", }, optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n An optional object with optional `allowProtected` property - which defaults to `false`."], ["\n An optional object with optional \\`allowProtected\\` property - which defaults to \\`false\\`."]))), requiresTypeInfo: true, ruleName: "rxjs-no-exposed-subjects", type: "functionality", typescriptOnly: true, }; return Rule; }(Lint.Rules.TypedRule)); exports.Rule = Rule; var RxjsNoExposedSubjects = (function (_super) { tslib_1.__extends(RxjsNoExposedSubjects, _super); function RxjsNoExposedSubjects(sourceFile, rawOptions, program) { var _this = _super.call(this, sourceFile, rawOptions, program) || this; _this.allowProtected = false; var _a = tslib_1.__read(_this.getOptions(), 1), options = _a[0]; if (options) { _this.allowProtected = options.allowProtected; } return _this; } RxjsNoExposedSubjects.prototype.visitConstructorDeclaration = function (node) { var _this = this; node.parameters.forEach(function (param) { return _this.validateNode(param); }); _super.prototype.visitConstructorDeclaration.call(this, node); }; RxjsNoExposedSubjects.prototype.visitPropertyDeclaration = function (node) { this.validateNode(node); _super.prototype.visitPropertyDeclaration.call(this, node); }; RxjsNoExposedSubjects.prototype.visitMethodDeclaration = function (node) { this.validateNode(node, node.type); _super.prototype.visitMethodDeclaration.call(this, node); }; RxjsNoExposedSubjects.prototype.visitMethodSignature = function (node) { this.validateNode(node, node.type); _super.prototype.visitMethodSignature.call(this, node); }; RxjsNoExposedSubjects.prototype.visitGetAccessor = function (node) { this.validateNode(node); _super.prototype.visitGetAccessor.call(this, node); }; RxjsNoExposedSubjects.prototype.visitSetAccessor = function (node) { this.validateNode(node); _super.prototype.visitSetAccessor.call(this, node); }; RxjsNoExposedSubjects.prototype.validateNode = function (node, typeNode) { var name = node.name; if (name) { var text = name.getText(); var allowProtected = this.allowProtected; var protectedModifier = allowProtected && tsutils.getModifier(node, ts.SyntaxKind.ProtectedKeyword); var privateModifier = tsutils.getModifier(node, ts.SyntaxKind.PrivateKeyword); var type = this.getTypeChecker().getTypeAtLocation(typeNode || node); if (!(protectedModifier || privateModifier) && util_1.couldBeType(type, "Subject") && !util_1.couldBeType(type, defaultAllowedTypesRegExp)) { this.addFailureAtNode(name, "Subject '" + text + "' must be private" + (allowProtected ? " or protected" : "") + "."); } } }; return RxjsNoExposedSubjects; }(Lint.ProgramAwareRuleWalker)); var templateObject_1;