noShadowedVariableRule.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var ts = require("typescript");
  8. var Lint = require("../lint");
  9. var Rule = (function (_super) {
  10. __extends(Rule, _super);
  11. function Rule() {
  12. _super.apply(this, arguments);
  13. }
  14. Rule.prototype.apply = function (sourceFile) {
  15. return this.applyWithWalker(new NoShadowedVariableWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "shadowed variable: '";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var NoShadowedVariableWalker = (function (_super) {
  22. __extends(NoShadowedVariableWalker, _super);
  23. function NoShadowedVariableWalker() {
  24. _super.apply(this, arguments);
  25. }
  26. NoShadowedVariableWalker.prototype.createScope = function () {
  27. return new ScopeInfo();
  28. };
  29. NoShadowedVariableWalker.prototype.createBlockScope = function () {
  30. return new ScopeInfo();
  31. };
  32. NoShadowedVariableWalker.prototype.visitBindingElement = function (node) {
  33. var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
  34. var variableDeclaration = Lint.getBindingElementVariableDeclaration(node);
  35. if (isSingleVariable) {
  36. if (variableDeclaration) {
  37. var isBlockScopedVariable = Lint.isBlockScopedVariable(variableDeclaration);
  38. this.handleSingleVariableIdentifier(node.name, isBlockScopedVariable);
  39. }
  40. else {
  41. this.handleSingleParameterIdentifier(node.name);
  42. }
  43. }
  44. _super.prototype.visitBindingElement.call(this, node);
  45. };
  46. NoShadowedVariableWalker.prototype.visitCatchClause = function (node) {
  47. this.visitBlock(node.block);
  48. };
  49. NoShadowedVariableWalker.prototype.visitCallSignature = function (node) {
  50. };
  51. NoShadowedVariableWalker.prototype.visitFunctionType = function (node) {
  52. };
  53. NoShadowedVariableWalker.prototype.visitMethodSignature = function (node) {
  54. };
  55. NoShadowedVariableWalker.prototype.visitParameterDeclaration = function (node) {
  56. var isSingleParameter = node.name.kind === ts.SyntaxKind.Identifier;
  57. if (isSingleParameter) {
  58. this.handleSingleParameterIdentifier(node.name);
  59. }
  60. _super.prototype.visitParameterDeclaration.call(this, node);
  61. };
  62. NoShadowedVariableWalker.prototype.visitTypeLiteral = function (node) {
  63. };
  64. NoShadowedVariableWalker.prototype.visitVariableDeclaration = function (node) {
  65. var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
  66. if (isSingleVariable) {
  67. this.handleSingleVariableIdentifier(node.name, Lint.isBlockScopedVariable(node));
  68. }
  69. _super.prototype.visitVariableDeclaration.call(this, node);
  70. };
  71. NoShadowedVariableWalker.prototype.handleSingleVariableIdentifier = function (variableIdentifier, isBlockScoped) {
  72. var variableName = variableIdentifier.text;
  73. var currentScope = this.getCurrentScope();
  74. var currentBlockScope = this.getCurrentBlockScope();
  75. if (this.isVarInAnyScope(variableName) && currentBlockScope.varNames.indexOf(variableName) < 0) {
  76. this.addFailureOnIdentifier(variableIdentifier);
  77. }
  78. if (!isBlockScoped
  79. || this.getCurrentBlockDepth() === 1
  80. || this.getCurrentBlockDepth() === this.getCurrentDepth()) {
  81. currentScope.varNames.push(variableName);
  82. }
  83. currentBlockScope.varNames.push(variableName);
  84. };
  85. NoShadowedVariableWalker.prototype.handleSingleParameterIdentifier = function (variableIdentifier) {
  86. var variableName = variableIdentifier.text;
  87. var currentScope = this.getCurrentScope();
  88. if (this.isVarInAnyScope(variableName)) {
  89. this.addFailureOnIdentifier(variableIdentifier);
  90. }
  91. currentScope.varNames.push(variableName);
  92. };
  93. NoShadowedVariableWalker.prototype.isVarInAnyScope = function (varName) {
  94. return this.getAllScopes().some(function (scopeInfo) { return scopeInfo.varNames.indexOf(varName) >= 0; });
  95. };
  96. NoShadowedVariableWalker.prototype.addFailureOnIdentifier = function (ident) {
  97. var failureString = Rule.FAILURE_STRING + ident.text + "'";
  98. this.addFailure(this.createFailure(ident.getStart(), ident.getWidth(), failureString));
  99. };
  100. return NoShadowedVariableWalker;
  101. }(Lint.BlockScopeAwareRuleWalker));
  102. var ScopeInfo = (function () {
  103. function ScopeInfo() {
  104. this.varNames = [];
  105. }
  106. return ScopeInfo;
  107. }());