noDuplicateVariableRule.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 OPTION_CHECK_PARAMETERS = "check-parameters";
  24. var Rule = /** @class */ (function (_super) {
  25. tslib_1.__extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. /* tslint:enable:object-literal-sort-keys */
  30. Rule.FAILURE_STRING = function (name) {
  31. return "Duplicate variable: '" + name + "'";
  32. };
  33. Rule.prototype.apply = function (sourceFile) {
  34. return this.applyWithWalker(new NoDuplicateVariableWalker(sourceFile, this.ruleName, {
  35. parameters: this.ruleArguments.indexOf(OPTION_CHECK_PARAMETERS) !== -1,
  36. }));
  37. };
  38. /* tslint:disable:object-literal-sort-keys */
  39. Rule.metadata = {
  40. ruleName: "no-duplicate-variable",
  41. description: "Disallows duplicate variable declarations in the same block scope.",
  42. 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."]))),
  43. 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."]))),
  44. optionsDescription: "You can specify `\"" + OPTION_CHECK_PARAMETERS + "\"` to check for variables with the same name as a parameter.",
  45. options: {
  46. type: "string",
  47. enum: [OPTION_CHECK_PARAMETERS],
  48. },
  49. optionExamples: [
  50. true,
  51. [true, OPTION_CHECK_PARAMETERS],
  52. ],
  53. type: "functionality",
  54. typescriptOnly: false,
  55. };
  56. return Rule;
  57. }(Lint.Rules.AbstractRule));
  58. exports.Rule = Rule;
  59. var NoDuplicateVariableWalker = /** @class */ (function (_super) {
  60. tslib_1.__extends(NoDuplicateVariableWalker, _super);
  61. function NoDuplicateVariableWalker() {
  62. return _super !== null && _super.apply(this, arguments) || this;
  63. }
  64. NoDuplicateVariableWalker.prototype.walk = function (sourceFile) {
  65. var _this = this;
  66. this.scope = new Set();
  67. var cb = function (node) {
  68. if (utils.isFunctionScopeBoundary(node)) {
  69. var oldScope = _this.scope;
  70. _this.scope = new Set();
  71. ts.forEachChild(node, cb);
  72. _this.scope = oldScope;
  73. return;
  74. }
  75. if (_this.options.parameters && utils.isParameterDeclaration(node)) {
  76. _this.handleBindingName(node.name, false);
  77. }
  78. else if (utils.isVariableDeclarationList(node) && !utils.isBlockScopedVariableDeclarationList(node)) {
  79. for (var _i = 0, _a = node.declarations; _i < _a.length; _i++) {
  80. var variable = _a[_i];
  81. _this.handleBindingName(variable.name, true);
  82. }
  83. }
  84. return ts.forEachChild(node, cb);
  85. };
  86. return ts.forEachChild(sourceFile, cb);
  87. };
  88. NoDuplicateVariableWalker.prototype.handleBindingName = function (name, check) {
  89. if (name.kind === ts.SyntaxKind.Identifier) {
  90. if (check && this.scope.has(name.text)) {
  91. this.addFailureAtNode(name, Rule.FAILURE_STRING(name.text));
  92. }
  93. else {
  94. this.scope.add(name.text);
  95. }
  96. }
  97. else {
  98. for (var _i = 0, _a = name.elements; _i < _a.length; _i++) {
  99. var e = _a[_i];
  100. if (e.kind !== ts.SyntaxKind.OmittedExpression) {
  101. this.handleBindingName(e.name, check);
  102. }
  103. }
  104. }
  105. };
  106. return NoDuplicateVariableWalker;
  107. }(Lint.AbstractWalker));
  108. var templateObject_1, templateObject_2;