.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noInferrableTypesRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2015 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 OPTION_IGNORE_PARMS = "ignore-params";
24 var OPTION_IGNORE_PROPERTIES = "ignore-properties";
25 var Rule = /** @class */ (function (_super) {
26     tslib_1.__extends(Rule, _super);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
30     /* tslint:enable:object-literal-sort-keys */
31     Rule.FAILURE_STRING_FACTORY = function (type) {
32         return "Type " + type + " trivially inferred from a " + type + " literal, remove type annotation";
33     };
34     Rule.prototype.apply = function (sourceFile) {
35         return this.applyWithWalker(new NoInferrableTypesWalker(sourceFile, this.ruleName, {
36             ignoreParameters: this.ruleArguments.indexOf(OPTION_IGNORE_PARMS) !== -1,
37             ignoreProperties: this.ruleArguments.indexOf(OPTION_IGNORE_PROPERTIES) !== -1,
38         }));
39     };
40     /* tslint:disable:object-literal-sort-keys */
41     Rule.metadata = {
42         ruleName: "no-inferrable-types",
43         description: "Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean.",
44         rationale: "Explicit types where they can be easily inferred by the compiler make code more verbose.",
45         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Two arguments may be optionally provided:\n\n            * `", "` allows specifying an inferrable type annotation for function params.\n            This can be useful when combining with the `typedef` rule.\n            * `", "` allows specifying an inferrable type annotation for class properties."], ["\n            Two arguments may be optionally provided:\n\n            * \\`", "\\` allows specifying an inferrable type annotation for function params.\n            This can be useful when combining with the \\`typedef\\` rule.\n            * \\`", "\\` allows specifying an inferrable type annotation for class properties."])), OPTION_IGNORE_PARMS, OPTION_IGNORE_PROPERTIES),
46         options: {
47             type: "array",
48             items: {
49                 type: "string",
50                 enum: [OPTION_IGNORE_PARMS, OPTION_IGNORE_PROPERTIES],
51             },
52             minLength: 0,
53             maxLength: 2,
54         },
55         hasFix: true,
56         optionExamples: [
57             true,
58             [true, OPTION_IGNORE_PARMS],
59             [true, OPTION_IGNORE_PARMS, OPTION_IGNORE_PROPERTIES],
60         ],
61         type: "typescript",
62         typescriptOnly: true,
63     };
64     return Rule;
65 }(Lint.Rules.AbstractRule));
66 exports.Rule = Rule;
67 var NoInferrableTypesWalker = /** @class */ (function (_super) {
68     tslib_1.__extends(NoInferrableTypesWalker, _super);
69     function NoInferrableTypesWalker() {
70         return _super !== null && _super.apply(this, arguments) || this;
71     }
72     NoInferrableTypesWalker.prototype.walk = function (sourceFile) {
73         var _this = this;
74         var cb = function (node) {
75             if (shouldCheck(node, _this.options)) {
76                 var name = node.name, type = node.type, initializer = node.initializer;
77                 if (type !== undefined &&
78                     initializer !== undefined &&
79                     typeIsInferrable(type.kind, initializer)) {
80                     var fix = Lint.Replacement.deleteFromTo(name.end, type.end);
81                     _this.addFailureAtNode(type, Rule.FAILURE_STRING_FACTORY(ts.tokenToString(type.kind)), fix);
82                 }
83             }
84             return ts.forEachChild(node, cb);
85         };
86         return ts.forEachChild(sourceFile, cb);
87     };
88     return NoInferrableTypesWalker;
89 }(Lint.AbstractWalker));
90 function shouldCheck(node, _a) {
91     var ignoreParameters = _a.ignoreParameters, ignoreProperties = _a.ignoreProperties;
92     switch (node.kind) {
93         case ts.SyntaxKind.Parameter:
94             return (!ignoreParameters &&
95                 !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ReadonlyKeyword) &&
96                 // "ignore-properties" also works for parameter properties
97                 !(ignoreProperties && node.modifiers !== undefined));
98         case ts.SyntaxKind.PropertyDeclaration:
99             return !ignoreProperties && !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ReadonlyKeyword);
100         case ts.SyntaxKind.VariableDeclaration:
101             return true;
102         default:
103             return false;
104     }
105 }
106 function typeIsInferrable(type, initializer) {
107     switch (type) {
108         case ts.SyntaxKind.BooleanKeyword:
109             return (initializer.kind === ts.SyntaxKind.TrueKeyword ||
110                 initializer.kind === ts.SyntaxKind.FalseKeyword);
111         case ts.SyntaxKind.NumberKeyword:
112             return Lint.isNumeric(initializer);
113         case ts.SyntaxKind.StringKeyword:
114             switch (initializer.kind) {
115                 case ts.SyntaxKind.StringLiteral:
116                 case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
117                 case ts.SyntaxKind.TemplateExpression:
118                     return true;
119                 default:
120                     return false;
121             }
122         default:
123             return false;
124     }
125 }
126 var templateObject_1;