maxLineLengthRule.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.isEnabled = function () {
  14. if (_super.prototype.isEnabled.call(this)) {
  15. var option = this.getOptions().ruleArguments[0];
  16. if (typeof option === "number" && option > 0) {
  17. return true;
  18. }
  19. }
  20. return false;
  21. };
  22. Rule.prototype.apply = function (sourceFile) {
  23. var ruleFailures = [];
  24. var lineLimit = this.getOptions().ruleArguments[0];
  25. var lineStarts = sourceFile.getLineStarts();
  26. var errorString = Rule.FAILURE_STRING + lineLimit;
  27. var disabledIntervals = this.getOptions().disabledIntervals;
  28. var source = sourceFile.getFullText();
  29. for (var i = 0; i < lineStarts.length - 1; ++i) {
  30. var from = lineStarts[i], to = lineStarts[i + 1];
  31. if ((to - from - 1) > lineLimit && !((to - from - 2) === lineLimit && source[to - 2] === "\r")) {
  32. var ruleFailure = new Lint.RuleFailure(sourceFile, from, to - 1, errorString, this.getOptions().ruleName);
  33. if (!Lint.doesIntersect(ruleFailure, disabledIntervals)) {
  34. ruleFailures.push(ruleFailure);
  35. }
  36. }
  37. }
  38. return ruleFailures;
  39. };
  40. Rule.FAILURE_STRING = "exceeds maximum line length of ";
  41. return Rule;
  42. }(Lint.Rules.AbstractRule));
  43. exports.Rule = Rule;