4 * Copyright 2018 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.prototype.applyWithProgram = function (sourceFile, program) {
29 var compilerOptions = program.getCompilerOptions();
30 var strictChecksEnabled = !!compilerOptions.strict;
31 var strictNullChecksEnabled = compilerOptions.strictNullChecks === true;
32 var strictNullChecksNotDisabled = compilerOptions.strictNullChecks !== false;
33 return this.applyWithWalker(new Walker(sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker(), strictNullChecksEnabled || (strictChecksEnabled && strictNullChecksNotDisabled)));
35 /* tslint:disable:object-literal-sort-keys */
37 ruleName: "no-unnecessary-type-assertion",
38 description: "Warns if a type assertion does not change the type of an expression.",
43 items: { type: "string" },
46 optionsDescription: "A list of whitelisted assertion types to ignore",
50 requiresTypeInfo: true,
52 /* tslint:enable:object-literal-sort-keys */
53 Rule.FAILURE_STRING = "This assertion is unnecessary since it does not change the type of the expression.";
55 }(Lint.Rules.TypedRule));
57 var Walker = /** @class */ (function (_super) {
58 tslib_1.__extends(Walker, _super);
59 function Walker(sourceFile, ruleName, options, checker, strictNullChecks) {
60 var _this = _super.call(this, sourceFile, ruleName, options) || this;
61 _this.checker = checker;
62 _this.strictNullChecks = strictNullChecks;
65 Walker.prototype.walk = function (sourceFile) {
67 var cb = function (node) {
69 case ts.SyntaxKind.NonNullExpression:
70 if (_this.strictNullChecks) {
71 _this.checkNonNullAssertion(node);
74 case ts.SyntaxKind.TypeAssertionExpression:
75 case ts.SyntaxKind.AsExpression:
76 _this.verifyCast(node);
78 return ts.forEachChild(node, cb);
80 return ts.forEachChild(sourceFile, cb);
82 Walker.prototype.checkNonNullAssertion = function (node) {
83 var type = this.checker.getTypeAtLocation(node.expression);
84 if (type === this.checker.getNonNullableType(type)) {
85 this.addFailureAtNode(node, Rule.FAILURE_STRING, Lint.Replacement.deleteFromTo(node.expression.end, node.end));
88 Walker.prototype.verifyCast = function (node) {
89 if (this.options.indexOf(node.type.getText(this.sourceFile)) !== -1) {
92 var castType = this.checker.getTypeAtLocation(node);
93 if (tsutils_1.isTypeFlagSet(castType, ts.TypeFlags.Literal) ||
94 (tsutils_1.isObjectType(castType) &&
95 (tsutils_1.isObjectFlagSet(castType, ts.ObjectFlags.Tuple) || couldBeTupleType(castType)))) {
96 // It's not always safe to remove a cast to a literal type or tuple
97 // type, as those types are sometimes widened without the cast.
100 var uncastType = this.checker.getTypeAtLocation(node.expression);
101 if (uncastType === castType) {
102 this.addFailureAtNode(node, Rule.FAILURE_STRING, node.kind === ts.SyntaxKind.TypeAssertionExpression
103 ? Lint.Replacement.deleteFromTo(node.getStart(), node.expression.getStart())
104 : Lint.Replacement.deleteFromTo(node.expression.end, node.end));
108 }(Lint.AbstractWalker));
110 * Sometimes tuple types don't have ObjectFlags.Tuple set, like when they're being matched against an inferred type.
111 * So, in addition, check if there are integer properties 0..n and no other numeric keys
113 function couldBeTupleType(type) {
114 var properties = type.getProperties();
115 if (properties.length === 0) {
119 for (; i < properties.length; ++i) {
120 var name = properties[i].name;
121 if (String(i) !== name) {
123 // if there are no integer properties, this is not a tuple
129 for (; i < properties.length; ++i) {
130 if (String(+properties[i].name) === properties[i].name) {
131 return false; // if there are any other numeric properties, this is not a tuple