4 * Copyright 2017 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.apply = function (sourceFile) {
29 return this.applyWithFunction(sourceFile, walk);
31 /* tslint:disable:object-literal-sort-keys */
33 ruleName: "no-unnecessary-initializer",
34 description: "Forbids a 'var'/'let' statement or destructuring initializer to be initialized to 'undefined'.",
36 optionsDescription: "Not configurable.",
38 optionExamples: [true],
39 rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Values in JavaScript default to `undefined`.\n There's no need to do so manually.\n "], ["\n Values in JavaScript default to \\`undefined\\`.\n There's no need to do so manually.\n "]))),
41 typescriptOnly: false,
43 /* tslint:enable:object-literal-sort-keys */
44 Rule.FAILURE_STRING = "Unnecessary initialization to 'undefined'.";
45 Rule.FAILURE_STRING_PARAMETER = "Use an optional parameter instead of initializing to 'undefined'. " +
46 "Also, the type declaration does not need to include '| undefined'.";
48 }(Lint.Rules.AbstractRule));
51 ts.forEachChild(ctx.sourceFile, function cb(node) {
53 case ts.SyntaxKind.BindingElement:
54 checkInitializer(node);
56 case ts.SyntaxKind.VariableDeclaration:
57 if (!tsutils_1.isBindingPattern(node.name) &&
58 !tsutils_1.isNodeFlagSet(node.parent, ts.NodeFlags.Const)) {
59 checkInitializer(node);
62 case ts.SyntaxKind.MethodDeclaration:
63 case ts.SyntaxKind.FunctionDeclaration:
64 case ts.SyntaxKind.Constructor: {
65 var parameters_1 = node.parameters;
66 parameters_1.forEach(function (parameter, i) {
67 if (isUndefined(parameter.initializer)) {
68 if (parametersAllOptionalAfter(parameters_1, i)) {
69 // No fix since they may want to remove '| undefined' from the type.
70 ctx.addFailureAtNode(parameter, Rule.FAILURE_STRING_PARAMETER);
73 failWithFix(parameter);
79 ts.forEachChild(node, cb);
81 function checkInitializer(node) {
82 if (isUndefined(node.initializer)) {
86 function failWithFix(node) {
87 var fix = Lint.Replacement.deleteFromTo(tsutils_1.getChildOfKind(node, ts.SyntaxKind.EqualsToken).pos, node.end);
88 ctx.addFailureAtNode(node, Rule.FAILURE_STRING, fix);
91 function parametersAllOptionalAfter(parameters, idx) {
92 for (var i = idx + 1; i < parameters.length; i++) {
93 if (parameters[i].questionToken !== undefined) {
96 if (parameters[i].initializer === undefined) {
102 function isUndefined(node) {
103 return (node !== undefined &&
104 node.kind === ts.SyntaxKind.Identifier &&
105 node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword);
107 var templateObject_1;