rxjs-tslint-rules
Version:
TSLint rules for RxJS
60 lines (59 loc) • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Walker = exports.Rule = void 0;
var tslib_1 = require("tslib");
var Lint = tslib_1.__importStar(require("tslint"));
var tsutils = tslib_1.__importStar(require("tsutils"));
var util_1 = require("../support/util");
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 Walker(sourceFile, this.getOptions(), program));
};
Rule.metadata = {
description: "Disallows calling the unsubscribe method of a subject instance.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: true,
ruleName: "rxjs-no-subject-unsubscribe",
type: "functionality",
typescriptOnly: true,
};
Rule.FAILURE_STRING = "Calling unsubscribe on a subject is forbidden";
return Rule;
}(Lint.Rules.TypedRule));
exports.Rule = Rule;
var Walker = (function (_super) {
tslib_1.__extends(Walker, _super);
function Walker() {
return _super !== null && _super.apply(this, arguments) || this;
}
Walker.prototype.visitCallExpression = function (node) {
var expression = node.expression;
if (tsutils.isPropertyAccessExpression(expression)) {
var name_1 = expression.name.getText();
var typeChecker = this.getTypeChecker();
var type = typeChecker.getTypeAtLocation(expression.expression);
if (name_1 === "unsubscribe" &&
util_1.isReferenceType(type) &&
util_1.couldBeType(type.target, "Subject")) {
this.addFailureAtNode(expression.name, Rule.FAILURE_STRING);
}
else if (name_1 === "add" &&
util_1.isReferenceType(type) &&
util_1.couldBeType(type.target, "Subscription")) {
var _a = tslib_1.__read(node.arguments, 1), argument = _a[0];
var argumentType = typeChecker.getTypeAtLocation(argument);
if (util_1.couldBeType(argumentType, "Subject")) {
this.addFailureAtNode(argument, Rule.FAILURE_STRING);
}
}
}
_super.prototype.visitCallExpression.call(this, node);
};
return Walker;
}(Lint.ProgramAwareRuleWalker));
exports.Walker = Walker;