alignRule.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. var alignWalker = new AlignWalker(sourceFile, this.getOptions());
  15. return this.applyWithWalker(alignWalker);
  16. };
  17. Rule.PARAMETERS_OPTION = "parameters";
  18. Rule.ARGUMENTS_OPTION = "arguments";
  19. Rule.STATEMENTS_OPTION = "statements";
  20. Rule.FAILURE_STRING_SUFFIX = " are not aligned";
  21. return Rule;
  22. }(Lint.Rules.AbstractRule));
  23. exports.Rule = Rule;
  24. var AlignWalker = (function (_super) {
  25. __extends(AlignWalker, _super);
  26. function AlignWalker() {
  27. _super.apply(this, arguments);
  28. }
  29. AlignWalker.prototype.visitConstructorDeclaration = function (node) {
  30. this.checkAlignment(Rule.PARAMETERS_OPTION, node.parameters);
  31. _super.prototype.visitConstructorDeclaration.call(this, node);
  32. };
  33. AlignWalker.prototype.visitFunctionDeclaration = function (node) {
  34. this.checkAlignment(Rule.PARAMETERS_OPTION, node.parameters);
  35. _super.prototype.visitFunctionDeclaration.call(this, node);
  36. };
  37. AlignWalker.prototype.visitFunctionExpression = function (node) {
  38. this.checkAlignment(Rule.PARAMETERS_OPTION, node.parameters);
  39. _super.prototype.visitFunctionExpression.call(this, node);
  40. };
  41. AlignWalker.prototype.visitMethodDeclaration = function (node) {
  42. this.checkAlignment(Rule.PARAMETERS_OPTION, node.parameters);
  43. _super.prototype.visitMethodDeclaration.call(this, node);
  44. };
  45. AlignWalker.prototype.visitCallExpression = function (node) {
  46. this.checkAlignment(Rule.ARGUMENTS_OPTION, node.arguments);
  47. _super.prototype.visitCallExpression.call(this, node);
  48. };
  49. AlignWalker.prototype.visitNewExpression = function (node) {
  50. this.checkAlignment(Rule.ARGUMENTS_OPTION, node.arguments);
  51. _super.prototype.visitNewExpression.call(this, node);
  52. };
  53. AlignWalker.prototype.visitBlock = function (node) {
  54. this.checkAlignment(Rule.STATEMENTS_OPTION, node.statements);
  55. _super.prototype.visitBlock.call(this, node);
  56. };
  57. AlignWalker.prototype.checkAlignment = function (kind, nodes) {
  58. if (nodes == null || nodes.length === 0 || !this.hasOption(kind)) {
  59. return;
  60. }
  61. var prevPos = getPosition(nodes[0]);
  62. var alignToColumn = prevPos.character;
  63. for (var _i = 0, _a = nodes.slice(1); _i < _a.length; _i++) {
  64. var node = _a[_i];
  65. var curPos = getPosition(node);
  66. if (curPos.line !== prevPos.line && curPos.character !== alignToColumn) {
  67. this.addFailure(this.createFailure(node.getStart(), node.getWidth(), kind + Rule.FAILURE_STRING_SUFFIX));
  68. break;
  69. }
  70. prevPos = curPos;
  71. }
  72. };
  73. return AlignWalker;
  74. }(Lint.RuleWalker));
  75. function getPosition(node) {
  76. return node.getSourceFile().getLineAndCharacterOfPosition(node.getStart());
  77. }