.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noDuplicateVariableRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 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 utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var utils_1 = require("../utils");
24 var OPTION_CHECK_PARAMETERS = "check-parameters";
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 = function (name) {
32         return "Duplicate variable: '" + name + "'";
33     };
34     Rule.prototype.apply = function (sourceFile) {
35         return this.applyWithWalker(new NoDuplicateVariableWalker(sourceFile, this.ruleName, {
36             parameters: this.ruleArguments.indexOf(OPTION_CHECK_PARAMETERS) !== -1,
37         }));
38     };
39     /* tslint:disable:object-literal-sort-keys */
40     Rule.metadata = {
41         ruleName: "no-duplicate-variable",
42         description: "Disallows duplicate variable declarations in the same block scope.",
43         descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            This rule is only useful when using the `var` keyword -\n            the compiler will detect redeclarations of `let` and `const` variables."], ["\n            This rule is only useful when using the \\`var\\` keyword -\n            the compiler will detect redeclarations of \\`let\\` and \\`const\\` variables."]))),
44         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            A variable can be reassigned if necessary -\n            there's no good reason to have a duplicate variable declaration."], ["\n            A variable can be reassigned if necessary -\n            there's no good reason to have a duplicate variable declaration."]))),
45         optionsDescription: "You can specify `\"" + OPTION_CHECK_PARAMETERS + "\"` to check for variables with the same name as a parameter.",
46         options: {
47             type: "string",
48             enum: [OPTION_CHECK_PARAMETERS],
49         },
50         optionExamples: [true, [true, OPTION_CHECK_PARAMETERS]],
51         type: "functionality",
52         typescriptOnly: false,
53     };
54     return Rule;
55 }(Lint.Rules.AbstractRule));
56 exports.Rule = Rule;
57 var NoDuplicateVariableWalker = /** @class */ (function (_super) {
58     tslib_1.__extends(NoDuplicateVariableWalker, _super);
59     function NoDuplicateVariableWalker() {
60         var _this = _super !== null && _super.apply(this, arguments) || this;
61         _this.scope = new Set();
62         return _this;
63     }
64     NoDuplicateVariableWalker.prototype.walk = function (sourceFile) {
65         var _this = this;
66         this.scope = new Set();
67         var cb = function (node) {
68             // tslint:disable:deprecation This is needed for https://github.com/palantir/tslint/pull/4274 and will be fixed once TSLint
69             // requires tsutils > 3.0.
70             if (utils_1.isFunctionScopeBoundary(node)) {
71                 // tslint:enable:deprecation
72                 var oldScope = _this.scope;
73                 _this.scope = new Set();
74                 ts.forEachChild(node, cb);
75                 _this.scope = oldScope;
76                 return;
77             }
78             if (_this.options.parameters && utils.isParameterDeclaration(node)) {
79                 _this.handleBindingName(node.name, false);
80             }
81             else if (utils.isVariableDeclarationList(node) &&
82                 !utils.isBlockScopedVariableDeclarationList(node)) {
83                 for (var _i = 0, _a = node.declarations; _i < _a.length; _i++) {
84                     var variable = _a[_i];
85                     _this.handleBindingName(variable.name, true);
86                 }
87             }
88             return ts.forEachChild(node, cb);
89         };
90         return ts.forEachChild(sourceFile, cb);
91     };
92     NoDuplicateVariableWalker.prototype.handleBindingName = function (name, check) {
93         if (name.kind === ts.SyntaxKind.Identifier) {
94             if (check && this.scope.has(name.text)) {
95                 this.addFailureAtNode(name, Rule.FAILURE_STRING(name.text));
96             }
97             else {
98                 this.scope.add(name.text);
99             }
100         }
101         else {
102             for (var _i = 0, _a = name.elements; _i < _a.length; _i++) {
103                 var e = _a[_i];
104                 if (e.kind !== ts.SyntaxKind.OmittedExpression) {
105                     this.handleBindingName(e.name, check);
106                 }
107             }
108         }
109     };
110     return NoDuplicateVariableWalker;
111 }(Lint.AbstractWalker));
112 var templateObject_1, templateObject_2;