noUnreachableRule.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 NoUnreachableWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "unreachable code";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var NoUnreachableWalker = (function (_super) {
  22. __extends(NoUnreachableWalker, _super);
  23. function NoUnreachableWalker(sourceFile, options) {
  24. _super.call(this, sourceFile, options);
  25. this.hasReturned = false;
  26. }
  27. NoUnreachableWalker.prototype.visitNode = function (node) {
  28. var previousReturned = this.hasReturned;
  29. if (node.kind === ts.SyntaxKind.FunctionDeclaration) {
  30. this.hasReturned = false;
  31. }
  32. if (this.hasReturned) {
  33. this.hasReturned = false;
  34. this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
  35. }
  36. _super.prototype.visitNode.call(this, node);
  37. if (node.kind === ts.SyntaxKind.FunctionDeclaration) {
  38. this.hasReturned = previousReturned;
  39. }
  40. };
  41. NoUnreachableWalker.prototype.visitBlock = function (node) {
  42. _super.prototype.visitBlock.call(this, node);
  43. this.hasReturned = false;
  44. };
  45. NoUnreachableWalker.prototype.visitCaseClause = function (node) {
  46. _super.prototype.visitCaseClause.call(this, node);
  47. this.hasReturned = false;
  48. };
  49. NoUnreachableWalker.prototype.visitDefaultClause = function (node) {
  50. _super.prototype.visitDefaultClause.call(this, node);
  51. this.hasReturned = false;
  52. };
  53. NoUnreachableWalker.prototype.visitIfStatement = function (node) {
  54. this.visitNode(node.expression);
  55. this.visitNode(node.thenStatement);
  56. this.hasReturned = false;
  57. if (node.elseStatement != null) {
  58. this.visitNode(node.elseStatement);
  59. this.hasReturned = false;
  60. }
  61. };
  62. NoUnreachableWalker.prototype.visitBreakStatement = function (node) {
  63. _super.prototype.visitBreakStatement.call(this, node);
  64. this.hasReturned = true;
  65. };
  66. NoUnreachableWalker.prototype.visitContinueStatement = function (node) {
  67. _super.prototype.visitContinueStatement.call(this, node);
  68. this.hasReturned = true;
  69. };
  70. NoUnreachableWalker.prototype.visitReturnStatement = function (node) {
  71. _super.prototype.visitReturnStatement.call(this, node);
  72. this.hasReturned = true;
  73. };
  74. NoUnreachableWalker.prototype.visitThrowStatement = function (node) {
  75. _super.prototype.visitThrowStatement.call(this, node);
  76. this.hasReturned = true;
  77. };
  78. return NoUnreachableWalker;
  79. }(Lint.RuleWalker));