.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noUnnecessaryTypeAssertionRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
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)));
34     };
35     /* tslint:disable:object-literal-sort-keys */
36     Rule.metadata = {
37         ruleName: "no-unnecessary-type-assertion",
38         description: "Warns if a type assertion does not change the type of an expression.",
39         options: {
40             type: "list",
41             listType: {
42                 type: "array",
43                 items: { type: "string" },
44             },
45         },
46         optionsDescription: "A list of whitelisted assertion types to ignore",
47         type: "typescript",
48         hasFix: true,
49         typescriptOnly: true,
50         requiresTypeInfo: true,
51     };
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.";
54     return Rule;
55 }(Lint.Rules.TypedRule));
56 exports.Rule = Rule;
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;
63         return _this;
64     }
65     Walker.prototype.walk = function (sourceFile) {
66         var _this = this;
67         var cb = function (node) {
68             switch (node.kind) {
69                 case ts.SyntaxKind.NonNullExpression:
70                     if (_this.strictNullChecks) {
71                         _this.checkNonNullAssertion(node);
72                     }
73                     break;
74                 case ts.SyntaxKind.TypeAssertionExpression:
75                 case ts.SyntaxKind.AsExpression:
76                     _this.verifyCast(node);
77             }
78             return ts.forEachChild(node, cb);
79         };
80         return ts.forEachChild(sourceFile, cb);
81     };
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));
86         }
87     };
88     Walker.prototype.verifyCast = function (node) {
89         if (this.options.indexOf(node.type.getText(this.sourceFile)) !== -1) {
90             return;
91         }
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.
98             return;
99         }
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));
105         }
106     };
107     return Walker;
108 }(Lint.AbstractWalker));
109 /**
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
112  */
113 function couldBeTupleType(type) {
114     var properties = type.getProperties();
115     if (properties.length === 0) {
116         return false;
117     }
118     var i = 0;
119     for (; i < properties.length; ++i) {
120         var name = properties[i].name;
121         if (String(i) !== name) {
122             if (i === 0) {
123                 // if there are no integer properties, this is not a tuple
124                 return false;
125             }
126             break;
127         }
128     }
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
132         }
133     }
134     return true;
135 }