curlyRule.js 3.5 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 CurlyWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.DO_FAILURE_STRING = "do statements must be braced";
  18. Rule.ELSE_FAILURE_STRING = "else statements must be braced";
  19. Rule.FOR_FAILURE_STRING = "for statements must be braced";
  20. Rule.IF_FAILURE_STRING = "if statements must be braced";
  21. Rule.WHILE_FAILURE_STRING = "while statements must be braced";
  22. return Rule;
  23. }(Lint.Rules.AbstractRule));
  24. exports.Rule = Rule;
  25. var CurlyWalker = (function (_super) {
  26. __extends(CurlyWalker, _super);
  27. function CurlyWalker() {
  28. _super.apply(this, arguments);
  29. }
  30. CurlyWalker.prototype.visitForInStatement = function (node) {
  31. if (!isStatementBraced(node.statement)) {
  32. this.addFailureForNode(node, Rule.FOR_FAILURE_STRING);
  33. }
  34. _super.prototype.visitForInStatement.call(this, node);
  35. };
  36. CurlyWalker.prototype.visitForOfStatement = function (node) {
  37. if (!isStatementBraced(node.statement)) {
  38. this.addFailureForNode(node, Rule.FOR_FAILURE_STRING);
  39. }
  40. _super.prototype.visitForInStatement.call(this, node);
  41. };
  42. CurlyWalker.prototype.visitForStatement = function (node) {
  43. if (!isStatementBraced(node.statement)) {
  44. this.addFailureForNode(node, Rule.FOR_FAILURE_STRING);
  45. }
  46. _super.prototype.visitForStatement.call(this, node);
  47. };
  48. CurlyWalker.prototype.visitIfStatement = function (node) {
  49. if (!isStatementBraced(node.thenStatement)) {
  50. this.addFailure(this.createFailure(node.getStart(), node.thenStatement.getEnd() - node.getStart(), Rule.IF_FAILURE_STRING));
  51. }
  52. if (node.elseStatement != null
  53. && node.elseStatement.kind !== ts.SyntaxKind.IfStatement
  54. && !isStatementBraced(node.elseStatement)) {
  55. var elseKeywordNode = node.getChildren().filter(function (child) { return child.kind === ts.SyntaxKind.ElseKeyword; })[0];
  56. this.addFailure(this.createFailure(elseKeywordNode.getStart(), node.elseStatement.getEnd() - elseKeywordNode.getStart(), Rule.ELSE_FAILURE_STRING));
  57. }
  58. _super.prototype.visitIfStatement.call(this, node);
  59. };
  60. CurlyWalker.prototype.visitDoStatement = function (node) {
  61. if (!isStatementBraced(node.statement)) {
  62. this.addFailureForNode(node, Rule.DO_FAILURE_STRING);
  63. }
  64. _super.prototype.visitDoStatement.call(this, node);
  65. };
  66. CurlyWalker.prototype.visitWhileStatement = function (node) {
  67. if (!isStatementBraced(node.statement)) {
  68. this.addFailureForNode(node, Rule.WHILE_FAILURE_STRING);
  69. }
  70. _super.prototype.visitWhileStatement.call(this, node);
  71. };
  72. CurlyWalker.prototype.addFailureForNode = function (node, failure) {
  73. this.addFailure(this.createFailure(node.getStart(), node.getWidth(), failure));
  74. };
  75. return CurlyWalker;
  76. }(Lint.RuleWalker));
  77. function isStatementBraced(node) {
  78. return node.kind === ts.SyntaxKind.Block;
  79. }