labelUndefinedRule.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 Lint = require("../lint");
  8. var Rule = (function (_super) {
  9. __extends(Rule, _super);
  10. function Rule() {
  11. _super.apply(this, arguments);
  12. }
  13. Rule.prototype.apply = function (sourceFile) {
  14. return this.applyWithWalker(new LabelUndefinedWalker(sourceFile, this.getOptions()));
  15. };
  16. Rule.FAILURE_STRING = "undefined label: '";
  17. return Rule;
  18. }(Lint.Rules.AbstractRule));
  19. exports.Rule = Rule;
  20. var LabelUndefinedWalker = (function (_super) {
  21. __extends(LabelUndefinedWalker, _super);
  22. function LabelUndefinedWalker() {
  23. _super.apply(this, arguments);
  24. }
  25. LabelUndefinedWalker.prototype.createScope = function () {
  26. return {};
  27. };
  28. LabelUndefinedWalker.prototype.visitLabeledStatement = function (node) {
  29. var label = node.label.text;
  30. var currentScope = this.getCurrentScope();
  31. currentScope[label] = true;
  32. _super.prototype.visitLabeledStatement.call(this, node);
  33. };
  34. LabelUndefinedWalker.prototype.visitBreakStatement = function (node) {
  35. this.validateLabelAt(node.label, node.getStart(), node.getChildAt(0).getWidth());
  36. _super.prototype.visitBreakStatement.call(this, node);
  37. };
  38. LabelUndefinedWalker.prototype.visitContinueStatement = function (node) {
  39. this.validateLabelAt(node.label, node.getStart(), node.getChildAt(0).getWidth());
  40. _super.prototype.visitContinueStatement.call(this, node);
  41. };
  42. LabelUndefinedWalker.prototype.validateLabelAt = function (label, position, width) {
  43. var currentScope = this.getCurrentScope();
  44. if (label != null && !currentScope[label.text]) {
  45. var failureString = Rule.FAILURE_STRING + label.text + "'";
  46. var failure = this.createFailure(position, width, failureString);
  47. this.addFailure(failure);
  48. }
  49. };
  50. return LabelUndefinedWalker;
  51. }(Lint.ScopeAwareRuleWalker));