.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noUnnecessaryInitializerRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 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.apply = function (sourceFile) {
29         return this.applyWithFunction(sourceFile, walk);
30     };
31     /* tslint:disable:object-literal-sort-keys */
32     Rule.metadata = {
33         ruleName: "no-unnecessary-initializer",
34         description: "Forbids a 'var'/'let' statement or destructuring initializer to be initialized to 'undefined'.",
35         hasFix: true,
36         optionsDescription: "Not configurable.",
37         options: null,
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        "]))),
40         type: "style",
41         typescriptOnly: false,
42     };
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'.";
47     return Rule;
48 }(Lint.Rules.AbstractRule));
49 exports.Rule = Rule;
50 function walk(ctx) {
51     ts.forEachChild(ctx.sourceFile, function cb(node) {
52         switch (node.kind) {
53             case ts.SyntaxKind.BindingElement:
54                 checkInitializer(node);
55                 break;
56             case ts.SyntaxKind.VariableDeclaration:
57                 if (!tsutils_1.isBindingPattern(node.name) &&
58                     !tsutils_1.isNodeFlagSet(node.parent, ts.NodeFlags.Const)) {
59                     checkInitializer(node);
60                 }
61                 break;
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);
71                         }
72                         else {
73                             failWithFix(parameter);
74                         }
75                     }
76                 });
77             }
78         }
79         ts.forEachChild(node, cb);
80     });
81     function checkInitializer(node) {
82         if (isUndefined(node.initializer)) {
83             failWithFix(node);
84         }
85     }
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);
89     }
90 }
91 function parametersAllOptionalAfter(parameters, idx) {
92     for (var i = idx + 1; i < parameters.length; i++) {
93         if (parameters[i].questionToken !== undefined) {
94             return true;
95         }
96         if (parameters[i].initializer === undefined) {
97             return false;
98         }
99     }
100     return true;
101 }
102 function isUndefined(node) {
103     return (node !== undefined &&
104         node.kind === ts.SyntaxKind.Identifier &&
105         node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword);
106 }
107 var templateObject_1;