aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/noUnnecessaryTypeAssertionRule.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/tslint/lib/rules/noUnnecessaryTypeAssertionRule.js')
-rw-r--r--node_modules/tslint/lib/rules/noUnnecessaryTypeAssertionRule.js26
1 files changed, 20 insertions, 6 deletions
diff --git a/node_modules/tslint/lib/rules/noUnnecessaryTypeAssertionRule.js b/node_modules/tslint/lib/rules/noUnnecessaryTypeAssertionRule.js
index 97a06adfc..6af6a0d60 100644
--- a/node_modules/tslint/lib/rules/noUnnecessaryTypeAssertionRule.js
+++ b/node_modules/tslint/lib/rules/noUnnecessaryTypeAssertionRule.js
@@ -26,14 +26,20 @@ var Rule = /** @class */ (function (_super) {
return _super !== null && _super.apply(this, arguments) || this;
}
Rule.prototype.applyWithProgram = function (sourceFile, program) {
- return this.applyWithWalker(new Walker(sourceFile, this.ruleName, program.getTypeChecker()));
+ return this.applyWithWalker(new Walker(sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker()));
};
/* tslint:disable:object-literal-sort-keys */
Rule.metadata = {
ruleName: "no-unnecessary-type-assertion",
description: "Warns if a type assertion does not change the type of an expression.",
- options: null,
- optionsDescription: "Not configurable",
+ options: {
+ type: "list",
+ listType: {
+ type: "array",
+ items: { type: "string" },
+ },
+ },
+ optionsDescription: "A list of whitelisted assertion types to ignore",
type: "typescript",
hasFix: true,
typescriptOnly: true,
@@ -46,8 +52,8 @@ var Rule = /** @class */ (function (_super) {
exports.Rule = Rule;
var Walker = /** @class */ (function (_super) {
tslib_1.__extends(Walker, _super);
- function Walker(sourceFile, ruleName, checker) {
- var _this = _super.call(this, sourceFile, ruleName, undefined) || this;
+ function Walker(sourceFile, ruleName, options, checker) {
+ var _this = _super.call(this, sourceFile, ruleName, options) || this;
_this.checker = checker;
return _this;
}
@@ -65,6 +71,9 @@ var Walker = /** @class */ (function (_super) {
return ts.forEachChild(sourceFile, cb);
};
Walker.prototype.verifyCast = function (node) {
+ if (tsutils_1.isAssertionExpression(node) && this.options.indexOf(node.type.getText(this.sourceFile)) !== -1) {
+ return;
+ }
var castType = this.checker.getTypeAtLocation(node);
if (castType === undefined) {
return;
@@ -72,7 +81,12 @@ var Walker = /** @class */ (function (_super) {
if (node.kind !== ts.SyntaxKind.NonNullExpression &&
(tsutils_1.isTypeFlagSet(castType, ts.TypeFlags.Literal) ||
tsutils_1.isObjectType(castType) &&
- tsutils_1.isObjectFlagSet(castType, ts.ObjectFlags.Tuple))) {
+ tsutils_1.isObjectFlagSet(castType, ts.ObjectFlags.Tuple)) ||
+ // Sometimes tuple types don't have ObjectFlags.Tuple set, like when
+ // they're being matched against an inferred type. So, in addition,
+ // check if any properties are numbers, which implies that this is
+ // likely a tuple type.
+ (castType.getProperties().some(function (symbol) { return !isNaN(Number(symbol.name)); }))) {
// It's not always safe to remove a cast to a literal type or tuple
// type, as those types are sometimes widened without the cast.
return;