noUseBeforeDeclareRule.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. var languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
  16. return this.applyWithWalker(new NoUseBeforeDeclareWalker(sourceFile, this.getOptions(), languageService));
  17. };
  18. Rule.FAILURE_STRING_PREFIX = "variable '";
  19. Rule.FAILURE_STRING_POSTFIX = "' used before declaration";
  20. return Rule;
  21. }(Lint.Rules.AbstractRule));
  22. exports.Rule = Rule;
  23. var NoUseBeforeDeclareWalker = (function (_super) {
  24. __extends(NoUseBeforeDeclareWalker, _super);
  25. function NoUseBeforeDeclareWalker(sourceFile, options, languageService) {
  26. _super.call(this, sourceFile, options);
  27. this.languageService = languageService;
  28. }
  29. NoUseBeforeDeclareWalker.prototype.createScope = function () {
  30. return {};
  31. };
  32. NoUseBeforeDeclareWalker.prototype.visitBindingElement = function (node) {
  33. var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
  34. var isBlockScoped = Lint.isBlockScopedBindingElement(node);
  35. if (isSingleVariable && !isBlockScoped) {
  36. var variableName = node.name.text;
  37. this.validateUsageForVariable(variableName, node.name.getStart());
  38. }
  39. _super.prototype.visitBindingElement.call(this, node);
  40. };
  41. NoUseBeforeDeclareWalker.prototype.visitImportDeclaration = function (node) {
  42. var importClause = node.importClause;
  43. if (importClause != null && importClause.name != null) {
  44. var variableIdentifier = importClause.name;
  45. this.validateUsageForVariable(variableIdentifier.text, variableIdentifier.getStart());
  46. }
  47. _super.prototype.visitImportDeclaration.call(this, node);
  48. };
  49. NoUseBeforeDeclareWalker.prototype.visitImportEqualsDeclaration = function (node) {
  50. var name = node.name;
  51. this.validateUsageForVariable(name.text, name.getStart());
  52. _super.prototype.visitImportEqualsDeclaration.call(this, node);
  53. };
  54. NoUseBeforeDeclareWalker.prototype.visitNamedImports = function (node) {
  55. for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
  56. var namedImport = _a[_i];
  57. this.validateUsageForVariable(namedImport.name.text, namedImport.name.getStart());
  58. }
  59. _super.prototype.visitNamedImports.call(this, node);
  60. };
  61. NoUseBeforeDeclareWalker.prototype.visitNamespaceImport = function (node) {
  62. this.validateUsageForVariable(node.name.text, node.name.getStart());
  63. _super.prototype.visitNamespaceImport.call(this, node);
  64. };
  65. NoUseBeforeDeclareWalker.prototype.visitVariableDeclaration = function (node) {
  66. var isSingleVariable = node.name.kind === ts.SyntaxKind.Identifier;
  67. var variableName = node.name.text;
  68. var currentScope = this.getCurrentScope();
  69. if (isSingleVariable && currentScope[variableName] == null) {
  70. this.validateUsageForVariable(variableName, node.getStart());
  71. }
  72. currentScope[variableName] = true;
  73. _super.prototype.visitVariableDeclaration.call(this, node);
  74. };
  75. NoUseBeforeDeclareWalker.prototype.validateUsageForVariable = function (name, position) {
  76. var fileName = this.getSourceFile().fileName;
  77. var highlights = this.languageService.getDocumentHighlights(fileName, position, [fileName]);
  78. if (highlights != null) {
  79. for (var _i = 0, highlights_1 = highlights; _i < highlights_1.length; _i++) {
  80. var highlight = highlights_1[_i];
  81. for (var _a = 0, _b = highlight.highlightSpans; _a < _b.length; _a++) {
  82. var highlightSpan = _b[_a];
  83. var referencePosition = highlightSpan.textSpan.start;
  84. if (referencePosition < position) {
  85. var failureString = Rule.FAILURE_STRING_PREFIX + name + Rule.FAILURE_STRING_POSTFIX;
  86. this.addFailure(this.createFailure(referencePosition, name.length, failureString));
  87. }
  88. }
  89. }
  90. }
  91. };
  92. return NoUseBeforeDeclareWalker;
  93. }(Lint.ScopeAwareRuleWalker));