rxjs-tslint-rules
Version:
TSLint rules for RxJS
72 lines (71 loc) • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rule = void 0;
var tslib_1 = require("tslib");
var tsquery_1 = require("@phenomnomnominal/tsquery");
var Lint = tslib_1.__importStar(require("tslint"));
var ts = tslib_1.__importStar(require("typescript"));
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.applyWithProgram = function (sourceFile, program) {
var _this = this;
var _a = tslib_1.__read(this.getOptions().ruleArguments, 1), options = _a[0];
var allowExplicitAny = options && options.hasOwnProperty("allowExplicitAny")
? options.allowExplicitAny
: false;
var failures = [];
var callExpressions = tsquery_1.tsquery(sourceFile, "CallExpression[expression.text=\"catchError\"]");
callExpressions.forEach(function (callExpression) {
var _a = tslib_1.__read(callExpression.arguments, 1), arg = _a[0];
if (!arg) {
return;
}
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
var _b = tslib_1.__read(arg.parameters, 1), parameter = _b[0];
if (!parameter) {
return;
}
if (parameter.type) {
if (parameter.type.kind === ts.SyntaxKind.AnyKeyword) {
if (allowExplicitAny) {
return;
}
failures.push(new Lint.RuleFailure(sourceFile, parameter.getStart(), parameter.getStart() + parameter.getWidth(), Rule.EXPLICIT_ANY, _this.ruleName, Lint.Replacement.replaceNode(parameter.type, "unknown")));
}
else if (parameter.type.kind !== ts.SyntaxKind.UnknownKeyword) {
failures.push(new Lint.RuleFailure(sourceFile, parameter.getStart(), parameter.getStart() + parameter.getWidth(), Rule.NARROWED, _this.ruleName));
}
}
else {
failures.push(new Lint.RuleFailure(sourceFile, parameter.getStart(), parameter.getStart() + parameter.getWidth(), Rule.IMPLICIT_ANY, _this.ruleName, Lint.Replacement.appendText(parameter.getStart() + parameter.getWidth(), ": unknown")));
}
}
});
return failures;
};
Rule.metadata = {
deprecationMessage: peer.v5 ? peer.v5NotSupportedMessage : undefined,
description: "Disallows implicit `any` error parameters in `catchError` operators.",
options: {
properties: {
allowExplicitAny: { type: "boolean" },
},
type: "object",
},
optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n An optional object with an optional `allowExplicitAny` property."], ["\n An optional object with an optional \\`allowExplicitAny\\` property."]))),
requiresTypeInfo: true,
ruleName: "rxjs-no-implicit-any-catch",
type: "functionality",
typescriptOnly: true,
};
Rule.EXPLICIT_ANY = "Explicit any in catchError";
Rule.IMPLICIT_ANY = "Implicit any in catchError";
Rule.NARROWED = "Error type must be unknown or any";
return Rule;
}(Lint.Rules.TypedRule));
exports.Rule = Rule;
var templateObject_1;