oneLineRule.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 OPTION_BRACE = "check-open-brace";
  10. var OPTION_CATCH = "check-catch";
  11. var OPTION_ELSE = "check-else";
  12. var OPTION_WHITESPACE = "check-whitespace";
  13. var Rule = (function (_super) {
  14. __extends(Rule, _super);
  15. function Rule() {
  16. _super.apply(this, arguments);
  17. }
  18. Rule.prototype.apply = function (sourceFile) {
  19. var oneLineWalker = new OneLineWalker(sourceFile, this.getOptions());
  20. return this.applyWithWalker(oneLineWalker);
  21. };
  22. Rule.BRACE_FAILURE_STRING = "misplaced opening brace";
  23. Rule.CATCH_FAILURE_STRING = "misplaced 'catch'";
  24. Rule.ELSE_FAILURE_STRING = "misplaced 'else'";
  25. Rule.WHITESPACE_FAILURE_STRING = "missing whitespace";
  26. return Rule;
  27. }(Lint.Rules.AbstractRule));
  28. exports.Rule = Rule;
  29. var OneLineWalker = (function (_super) {
  30. __extends(OneLineWalker, _super);
  31. function OneLineWalker() {
  32. _super.apply(this, arguments);
  33. }
  34. OneLineWalker.prototype.visitIfStatement = function (node) {
  35. var sourceFile = node.getSourceFile();
  36. var thenStatement = node.thenStatement;
  37. if (thenStatement.kind === ts.SyntaxKind.Block) {
  38. var expressionCloseParen = node.getChildAt(3);
  39. var thenOpeningBrace = thenStatement.getChildAt(0);
  40. this.handleOpeningBrace(expressionCloseParen, thenOpeningBrace);
  41. }
  42. var elseStatement = node.elseStatement;
  43. if (elseStatement != null) {
  44. var elseKeyword = getFirstChildOfKind(node, ts.SyntaxKind.ElseKeyword);
  45. if (elseStatement.kind === ts.SyntaxKind.Block) {
  46. var elseOpeningBrace = elseStatement.getChildAt(0);
  47. this.handleOpeningBrace(elseKeyword, elseOpeningBrace);
  48. }
  49. if (this.hasOption(OPTION_ELSE)) {
  50. var thenStatementEndLine = sourceFile.getLineAndCharacterOfPosition(thenStatement.getEnd()).line;
  51. var elseKeywordLine = sourceFile.getLineAndCharacterOfPosition(elseKeyword.getStart()).line;
  52. if (thenStatementEndLine !== elseKeywordLine) {
  53. var failure = this.createFailure(elseKeyword.getStart(), elseKeyword.getWidth(), Rule.ELSE_FAILURE_STRING);
  54. this.addFailure(failure);
  55. }
  56. }
  57. }
  58. _super.prototype.visitIfStatement.call(this, node);
  59. };
  60. OneLineWalker.prototype.visitCatchClause = function (node) {
  61. var catchKeyword = node.getChildAt(0);
  62. var catchOpeningBrace = node.block.getChildAt(0);
  63. this.handleOpeningBrace(catchKeyword, catchOpeningBrace);
  64. _super.prototype.visitCatchClause.call(this, node);
  65. };
  66. OneLineWalker.prototype.visitTryStatement = function (node) {
  67. var sourceFile = node.getSourceFile();
  68. var catchClause = node.catchClause;
  69. var tryKeyword = node.getChildAt(0);
  70. var tryBlock = node.tryBlock;
  71. var tryOpeningBrace = tryBlock.getChildAt(0);
  72. this.handleOpeningBrace(tryKeyword, tryOpeningBrace);
  73. if (this.hasOption(OPTION_CATCH) && catchClause != null) {
  74. var tryClosingBrace = node.tryBlock.getChildAt(node.tryBlock.getChildCount() - 1);
  75. var catchKeyword = catchClause.getChildAt(0);
  76. var tryClosingBraceLine = sourceFile.getLineAndCharacterOfPosition(tryClosingBrace.getEnd()).line;
  77. var catchKeywordLine = sourceFile.getLineAndCharacterOfPosition(catchKeyword.getStart()).line;
  78. if (tryClosingBraceLine !== catchKeywordLine) {
  79. var failure = this.createFailure(catchKeyword.getStart(), catchKeyword.getWidth(), Rule.CATCH_FAILURE_STRING);
  80. this.addFailure(failure);
  81. }
  82. }
  83. _super.prototype.visitTryStatement.call(this, node);
  84. };
  85. OneLineWalker.prototype.visitForStatement = function (node) {
  86. this.handleIterationStatement(node);
  87. _super.prototype.visitForStatement.call(this, node);
  88. };
  89. OneLineWalker.prototype.visitForInStatement = function (node) {
  90. this.handleIterationStatement(node);
  91. _super.prototype.visitForInStatement.call(this, node);
  92. };
  93. OneLineWalker.prototype.visitWhileStatement = function (node) {
  94. this.handleIterationStatement(node);
  95. _super.prototype.visitWhileStatement.call(this, node);
  96. };
  97. OneLineWalker.prototype.visitBinaryExpression = function (node) {
  98. var rightkind = node.right.kind;
  99. var opkind = node.operatorToken.kind;
  100. if (opkind === ts.SyntaxKind.EqualsToken && rightkind === ts.SyntaxKind.ObjectLiteralExpression) {
  101. var equalsToken = node.getChildAt(1);
  102. var openBraceToken = node.right.getChildAt(0);
  103. this.handleOpeningBrace(equalsToken, openBraceToken);
  104. }
  105. _super.prototype.visitBinaryExpression.call(this, node);
  106. };
  107. OneLineWalker.prototype.visitVariableDeclaration = function (node) {
  108. var initializer = node.initializer;
  109. if (initializer != null && initializer.kind === ts.SyntaxKind.ObjectLiteralExpression) {
  110. var equalsToken = node.getChildAt(1);
  111. var openBraceToken = initializer.getChildAt(0);
  112. this.handleOpeningBrace(equalsToken, openBraceToken);
  113. }
  114. _super.prototype.visitVariableDeclaration.call(this, node);
  115. };
  116. OneLineWalker.prototype.visitDoStatement = function (node) {
  117. var doKeyword = node.getChildAt(0);
  118. var statement = node.statement;
  119. if (statement.kind === ts.SyntaxKind.Block) {
  120. var openBraceToken = statement.getChildAt(0);
  121. this.handleOpeningBrace(doKeyword, openBraceToken);
  122. }
  123. _super.prototype.visitDoStatement.call(this, node);
  124. };
  125. OneLineWalker.prototype.visitModuleDeclaration = function (node) {
  126. var nameNode = node.name;
  127. var body = node.body;
  128. if (body.kind === ts.SyntaxKind.ModuleBlock) {
  129. var openBraceToken = body.getChildAt(0);
  130. this.handleOpeningBrace(nameNode, openBraceToken);
  131. }
  132. _super.prototype.visitModuleDeclaration.call(this, node);
  133. };
  134. OneLineWalker.prototype.visitEnumDeclaration = function (node) {
  135. var nameNode = node.name;
  136. var openBraceToken = getFirstChildOfKind(node, ts.SyntaxKind.OpenBraceToken);
  137. this.handleOpeningBrace(nameNode, openBraceToken);
  138. _super.prototype.visitEnumDeclaration.call(this, node);
  139. };
  140. OneLineWalker.prototype.visitSwitchStatement = function (node) {
  141. var closeParenToken = node.getChildAt(3);
  142. var openBraceToken = node.caseBlock.getChildAt(0);
  143. this.handleOpeningBrace(closeParenToken, openBraceToken);
  144. _super.prototype.visitSwitchStatement.call(this, node);
  145. };
  146. OneLineWalker.prototype.visitInterfaceDeclaration = function (node) {
  147. this.handleClassLikeDeclaration(node);
  148. _super.prototype.visitInterfaceDeclaration.call(this, node);
  149. };
  150. OneLineWalker.prototype.visitClassDeclaration = function (node) {
  151. this.handleClassLikeDeclaration(node);
  152. _super.prototype.visitClassDeclaration.call(this, node);
  153. };
  154. OneLineWalker.prototype.visitFunctionDeclaration = function (node) {
  155. this.handleFunctionLikeDeclaration(node);
  156. _super.prototype.visitFunctionDeclaration.call(this, node);
  157. };
  158. OneLineWalker.prototype.visitMethodDeclaration = function (node) {
  159. this.handleFunctionLikeDeclaration(node);
  160. _super.prototype.visitMethodDeclaration.call(this, node);
  161. };
  162. OneLineWalker.prototype.visitConstructorDeclaration = function (node) {
  163. this.handleFunctionLikeDeclaration(node);
  164. _super.prototype.visitConstructorDeclaration.call(this, node);
  165. };
  166. OneLineWalker.prototype.visitArrowFunction = function (node) {
  167. var body = node.body;
  168. if (body != null && body.kind === ts.SyntaxKind.Block) {
  169. var arrowToken = getFirstChildOfKind(node, ts.SyntaxKind.EqualsGreaterThanToken);
  170. var openBraceToken = node.body.getChildAt(0);
  171. this.handleOpeningBrace(arrowToken, openBraceToken);
  172. }
  173. _super.prototype.visitArrowFunction.call(this, node);
  174. };
  175. OneLineWalker.prototype.handleFunctionLikeDeclaration = function (node) {
  176. var body = node.body;
  177. if (body != null && body.kind === ts.SyntaxKind.Block) {
  178. var openBraceToken = node.body.getChildAt(0);
  179. if (node.type != null) {
  180. this.handleOpeningBrace(node.type, openBraceToken);
  181. }
  182. else {
  183. var closeParenToken = getFirstChildOfKind(node, ts.SyntaxKind.CloseParenToken);
  184. this.handleOpeningBrace(closeParenToken, openBraceToken);
  185. }
  186. }
  187. };
  188. OneLineWalker.prototype.handleClassLikeDeclaration = function (node) {
  189. var lastNodeOfDeclaration = node.name;
  190. var openBraceToken = getFirstChildOfKind(node, ts.SyntaxKind.OpenBraceToken);
  191. if (node.heritageClauses != null) {
  192. lastNodeOfDeclaration = node.heritageClauses[node.heritageClauses.length - 1];
  193. }
  194. else if (node.typeParameters != null) {
  195. lastNodeOfDeclaration = node.typeParameters[node.typeParameters.length - 1];
  196. }
  197. this.handleOpeningBrace(lastNodeOfDeclaration, openBraceToken);
  198. };
  199. OneLineWalker.prototype.handleIterationStatement = function (node) {
  200. var closeParenToken = node.getChildAt(node.getChildCount() - 2);
  201. var statement = node.statement;
  202. if (statement.kind === ts.SyntaxKind.Block) {
  203. var openBraceToken = statement.getChildAt(0);
  204. this.handleOpeningBrace(closeParenToken, openBraceToken);
  205. }
  206. };
  207. OneLineWalker.prototype.handleOpeningBrace = function (previousNode, openBraceToken) {
  208. if (previousNode == null || openBraceToken == null) {
  209. return;
  210. }
  211. var sourceFile = previousNode.getSourceFile();
  212. var previousNodeLine = sourceFile.getLineAndCharacterOfPosition(previousNode.getEnd()).line;
  213. var openBraceLine = sourceFile.getLineAndCharacterOfPosition(openBraceToken.getStart()).line;
  214. var failure;
  215. if (this.hasOption(OPTION_BRACE) && previousNodeLine !== openBraceLine) {
  216. failure = this.createFailure(openBraceToken.getStart(), openBraceToken.getWidth(), Rule.BRACE_FAILURE_STRING);
  217. }
  218. else if (this.hasOption(OPTION_WHITESPACE) && previousNode.getEnd() === openBraceToken.getStart()) {
  219. failure = this.createFailure(openBraceToken.getStart(), openBraceToken.getWidth(), Rule.WHITESPACE_FAILURE_STRING);
  220. }
  221. if (failure) {
  222. this.addFailure(failure);
  223. }
  224. };
  225. return OneLineWalker;
  226. }(Lint.RuleWalker));
  227. function getFirstChildOfKind(node, kind) {
  228. return node.getChildren().filter(function (child) { return child.kind === kind; })[0];
  229. }