4 * Copyright 2015 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 OPTION_IGNORE_PARMS = "ignore-params";
24 var OPTION_IGNORE_PROPERTIES = "ignore-properties";
25 var Rule = /** @class */ (function (_super) {
26 tslib_1.__extends(Rule, _super);
28 return _super !== null && _super.apply(this, arguments) || this;
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";
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,
40 /* tslint:disable:object-literal-sort-keys */
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),
50 enum: [OPTION_IGNORE_PARMS, OPTION_IGNORE_PROPERTIES],
58 [true, OPTION_IGNORE_PARMS],
59 [true, OPTION_IGNORE_PARMS, OPTION_IGNORE_PROPERTIES],
65 }(Lint.Rules.AbstractRule));
67 var NoInferrableTypesWalker = /** @class */ (function (_super) {
68 tslib_1.__extends(NoInferrableTypesWalker, _super);
69 function NoInferrableTypesWalker() {
70 return _super !== null && _super.apply(this, arguments) || this;
72 NoInferrableTypesWalker.prototype.walk = function (sourceFile) {
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);
84 return ts.forEachChild(node, cb);
86 return ts.forEachChild(sourceFile, cb);
88 return NoInferrableTypesWalker;
89 }(Lint.AbstractWalker));
90 function shouldCheck(node, _a) {
91 var ignoreParameters = _a.ignoreParameters, ignoreProperties = _a.ignoreProperties;
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:
106 function typeIsInferrable(type, initializer) {
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:
126 var templateObject_1;