rxjs-tslint-rules
Version:
TSLint rules for RxJS
80 lines (79 loc) • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Lint = require("tslint");
var tsutils = require("tsutils");
var ts = require("typescript");
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: "Enforces the passing of Error values to error notifications.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: true,
ruleName: "rxjs-throw-error",
type: "functionality",
typescriptOnly: true
};
Rule.FAILURE_STRING = "Passing non-Error values 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 _this = this;
var _a = tslib_1.__read(node.arguments, 1), argument = _a[0], expression = node.expression;
var typeChecker = this.getTypeChecker();
var validate = function (argument) {
var fail = true;
if (argument) {
var type = typeChecker.getTypeAtLocation(argument);
fail = !(util_1.isAny(type) || util_1.couldBeType(type, "Error"));
}
if (fail) {
_this.addFailureAtNode(argument, Rule.FAILURE_STRING);
}
};
if (tsutils.isPropertyAccessExpression(expression)) {
var name_1 = expression.name.getText();
var type = typeChecker.getTypeAtLocation(expression.expression);
if (name_1 === "throw" && util_1.couldBeType(type, "Observable")) {
validate(argument);
}
}
else if (tsutils.isIdentifier(expression)) {
var name_2 = expression.getText();
var type = typeChecker.getTypeAtLocation(expression);
var _b = tslib_1.__read(typeChecker.getSignaturesOfType(type, ts.SignatureKind.Call), 1), signature = _b[0];
if (signature) {
var returnType = typeChecker.getReturnTypeOfSignature(signature);
if ((name_2 === "_throw" || name_2 === "throwError") &&
util_1.couldBeType(returnType, "Observable")) {
validate(argument);
}
}
}
_super.prototype.visitCallExpression.call(this, node);
};
Walker.prototype.visitThrowStatement = function (node) {
var typeChecker = this.getTypeChecker();
var type = typeChecker.getTypeAtLocation(node.expression);
if (!util_1.isAny(type) && !util_1.couldBeType(type, "Error")) {
this.addFailureAtNode(node.expression, Rule.FAILURE_STRING);
}
_super.prototype.visitThrowStatement.call(this, node);
};
return Walker;
}(Lint.ProgramAwareRuleWalker));
exports.Walker = Walker;