semicolonRule.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 SemicolonWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING = "missing semicolon";
  18. return Rule;
  19. }(Lint.Rules.AbstractRule));
  20. exports.Rule = Rule;
  21. var SemicolonWalker = (function (_super) {
  22. __extends(SemicolonWalker, _super);
  23. function SemicolonWalker() {
  24. _super.apply(this, arguments);
  25. }
  26. SemicolonWalker.prototype.visitVariableStatement = function (node) {
  27. this.checkSemicolonAt(node);
  28. _super.prototype.visitVariableStatement.call(this, node);
  29. };
  30. SemicolonWalker.prototype.visitExpressionStatement = function (node) {
  31. this.checkSemicolonAt(node);
  32. _super.prototype.visitExpressionStatement.call(this, node);
  33. };
  34. SemicolonWalker.prototype.visitReturnStatement = function (node) {
  35. this.checkSemicolonAt(node);
  36. _super.prototype.visitReturnStatement.call(this, node);
  37. };
  38. SemicolonWalker.prototype.visitBreakStatement = function (node) {
  39. this.checkSemicolonAt(node);
  40. _super.prototype.visitBreakStatement.call(this, node);
  41. };
  42. SemicolonWalker.prototype.visitContinueStatement = function (node) {
  43. this.checkSemicolonAt(node);
  44. _super.prototype.visitContinueStatement.call(this, node);
  45. };
  46. SemicolonWalker.prototype.visitThrowStatement = function (node) {
  47. this.checkSemicolonAt(node);
  48. _super.prototype.visitThrowStatement.call(this, node);
  49. };
  50. SemicolonWalker.prototype.visitImportDeclaration = function (node) {
  51. this.checkSemicolonAt(node);
  52. _super.prototype.visitImportDeclaration.call(this, node);
  53. };
  54. SemicolonWalker.prototype.visitImportEqualsDeclaration = function (node) {
  55. this.checkSemicolonAt(node);
  56. _super.prototype.visitImportEqualsDeclaration.call(this, node);
  57. };
  58. SemicolonWalker.prototype.visitDoStatement = function (node) {
  59. this.checkSemicolonAt(node);
  60. _super.prototype.visitDoStatement.call(this, node);
  61. };
  62. SemicolonWalker.prototype.visitDebuggerStatement = function (node) {
  63. this.checkSemicolonAt(node);
  64. _super.prototype.visitDebuggerStatement.call(this, node);
  65. };
  66. SemicolonWalker.prototype.visitPropertyDeclaration = function (node) {
  67. this.checkSemicolonAt(node);
  68. _super.prototype.visitPropertyDeclaration.call(this, node);
  69. };
  70. SemicolonWalker.prototype.visitInterfaceDeclaration = function (node) {
  71. for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
  72. var member = _a[_i];
  73. this.checkSemicolonAt(member);
  74. }
  75. _super.prototype.visitInterfaceDeclaration.call(this, node);
  76. };
  77. SemicolonWalker.prototype.checkSemicolonAt = function (node) {
  78. var children = node.getChildren(this.getSourceFile());
  79. var hasSemicolon = children.some(function (child) { return child.kind === ts.SyntaxKind.SemicolonToken; });
  80. if (!hasSemicolon) {
  81. var sourceFile = this.getSourceFile();
  82. var position = node.getStart(sourceFile) + node.getWidth(sourceFile);
  83. this.addFailure(this.createFailure(Math.min(position, this.getLimit()), 0, Rule.FAILURE_STRING));
  84. }
  85. };
  86. return SemicolonWalker;
  87. }(Lint.RuleWalker));