indentRule.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_USE_TABS = "tabs";
  10. var OPTION_USE_SPACES = "spaces";
  11. var Rule = (function (_super) {
  12. __extends(Rule, _super);
  13. function Rule() {
  14. _super.apply(this, arguments);
  15. }
  16. Rule.prototype.apply = function (sourceFile) {
  17. return this.applyWithWalker(new IndentWalker(sourceFile, this.getOptions()));
  18. };
  19. Rule.FAILURE_STRING_TABS = "tab indentation expected";
  20. Rule.FAILURE_STRING_SPACES = "space indentation expected";
  21. return Rule;
  22. }(Lint.Rules.AbstractRule));
  23. exports.Rule = Rule;
  24. var IndentWalker = (function (_super) {
  25. __extends(IndentWalker, _super);
  26. function IndentWalker(sourceFile, options) {
  27. _super.call(this, sourceFile, options);
  28. if (this.hasOption(OPTION_USE_TABS)) {
  29. this.regExp = new RegExp(" ");
  30. this.failureString = Rule.FAILURE_STRING_TABS;
  31. }
  32. else if (this.hasOption(OPTION_USE_SPACES)) {
  33. this.regExp = new RegExp("\t");
  34. this.failureString = Rule.FAILURE_STRING_SPACES;
  35. }
  36. }
  37. IndentWalker.prototype.visitSourceFile = function (node) {
  38. if (!this.hasOption(OPTION_USE_TABS) && !this.hasOption(OPTION_USE_SPACES)) {
  39. return;
  40. }
  41. var endOfComment = -1;
  42. var scanner = ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, node.text);
  43. for (var _i = 0, _a = node.getLineStarts(); _i < _a.length; _i++) {
  44. var lineStart = _a[_i];
  45. if (lineStart < endOfComment) {
  46. continue;
  47. }
  48. scanner.setTextPos(lineStart);
  49. var currentScannedType = scanner.scan();
  50. var fullLeadingWhitespace = "";
  51. var lastStartPos = -1;
  52. while (currentScannedType === ts.SyntaxKind.WhitespaceTrivia) {
  53. var startPos = scanner.getStartPos();
  54. if (startPos === lastStartPos) {
  55. break;
  56. }
  57. lastStartPos = startPos;
  58. fullLeadingWhitespace += scanner.getTokenText();
  59. currentScannedType = scanner.scan();
  60. }
  61. var commentRanges = ts.getTrailingCommentRanges(node.text, lineStart);
  62. if (commentRanges) {
  63. endOfComment = commentRanges[commentRanges.length - 1].end;
  64. }
  65. if (currentScannedType === ts.SyntaxKind.SingleLineCommentTrivia
  66. || currentScannedType === ts.SyntaxKind.MultiLineCommentTrivia
  67. || currentScannedType === ts.SyntaxKind.NewLineTrivia) {
  68. continue;
  69. }
  70. if (fullLeadingWhitespace.match(this.regExp)) {
  71. this.addFailure(this.createFailure(lineStart, fullLeadingWhitespace.length, this.failureString));
  72. }
  73. }
  74. };
  75. return IndentWalker;
  76. }(Lint.RuleWalker));