maxLineLengthRule.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var tsutils_1 = require("tsutils");
  21. var Lint = require("../index");
  22. var Rule = /** @class */ (function (_super) {
  23. tslib_1.__extends(Rule, _super);
  24. function Rule() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. /* tslint:enable:object-literal-sort-keys */
  28. Rule.FAILURE_STRING_FACTORY = function (lineLimit) {
  29. return "Exceeds maximum line length of " + lineLimit;
  30. };
  31. Rule.prototype.isEnabled = function () {
  32. var limit = this.getRuleOptions().limit;
  33. return _super.prototype.isEnabled.call(this) && (limit > 0);
  34. };
  35. Rule.prototype.apply = function (sourceFile) {
  36. return this.applyWithFunction(sourceFile, walk, this.getRuleOptions());
  37. };
  38. Rule.prototype.getRuleOptions = function () {
  39. var argument = this.ruleArguments[0];
  40. var options = { limit: 0 };
  41. if (typeof argument === "number") {
  42. options.limit = argument;
  43. }
  44. else {
  45. options = argument;
  46. var ignorePattern = argument["ignore-pattern"];
  47. options.ignorePattern = (typeof ignorePattern === "string") ?
  48. new RegExp((ignorePattern)) : undefined;
  49. }
  50. options.limit = Number(options.limit); // user can pass a string instead of number
  51. return options;
  52. };
  53. /* tslint:disable:object-literal-sort-keys */
  54. Rule.metadata = {
  55. ruleName: "max-line-length",
  56. description: "Requires lines to be under a certain max length.",
  57. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Limiting the length of a line of code improves code readability.\n It also makes comparing code side-by-side easier and improves compatibility with\n various editors, IDEs, and diff viewers."], ["\n Limiting the length of a line of code improves code readability.\n It also makes comparing code side-by-side easier and improves compatibility with\n various editors, IDEs, and diff viewers."]))),
  58. optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n It can take one argument, which can be any of the following:\n * integer indicating maximum length of lines.\n * object with keys:\n * `limit` - number < 0 defining max line length\n * `ignore-pattern` - string defining ignore pattern for this rule, being parsed by `new RegExp()`.\n For example:\n * `// ` pattern will ignore all in-line comments.\n * `^import ` pattern will ignore all import statements.\n * `^export {(.*?)}` pattern will ignore all multiple export statements.\n * `class [a-zA-Z]+ implements ` pattern will ignore all class declarations implementing interfaces.\n * `^import |^export {(.*?)}|class [a-zA-Z]+ implements |// ` pattern will ignore all the cases listed above.\n "], ["\n It can take one argument, which can be any of the following:\n * integer indicating maximum length of lines.\n * object with keys:\n * \\`limit\\` - number < 0 defining max line length\n * \\`ignore-pattern\\` - string defining ignore pattern for this rule, being parsed by \\`new RegExp()\\`.\n For example:\n * \\`\\/\\/ \\` pattern will ignore all in-line comments.\n * \\`^import \\` pattern will ignore all import statements.\n * \\`^export \\{(.*?)\\}\\` pattern will ignore all multiple export statements.\n * \\`class [a-zA-Z]+ implements \\` pattern will ignore all class declarations implementing interfaces.\n * \\`^import |^export \\{(.*?)\\}|class [a-zA-Z]+ implements |// \\` pattern will ignore all the cases listed above.\n "]))),
  59. options: {
  60. type: "array",
  61. items: {
  62. oneOf: [
  63. {
  64. type: "number",
  65. },
  66. {
  67. type: "object",
  68. properties: {
  69. "limit": { type: "number" },
  70. "ignore-pattern": { type: "string" },
  71. },
  72. additionalProperties: false,
  73. },
  74. ],
  75. },
  76. minLength: 1,
  77. maxLength: 2,
  78. },
  79. optionExamples: [[true, 120], [true, {
  80. "limit": 120,
  81. "ignore-pattern": "^import |^export \{(.*?)\}"
  82. }]],
  83. type: "maintainability",
  84. typescriptOnly: false,
  85. };
  86. return Rule;
  87. }(Lint.Rules.AbstractRule));
  88. exports.Rule = Rule;
  89. function walk(ctx) {
  90. var limit = ctx.options.limit;
  91. var ignorePattern = ctx.options.ignorePattern;
  92. for (var _i = 0, _a = tsutils_1.getLineRanges(ctx.sourceFile); _i < _a.length; _i++) {
  93. var line = _a[_i];
  94. if (line.contentLength <= limit) {
  95. continue;
  96. }
  97. var lineContent = ctx.sourceFile.text.substr(line.pos, line.contentLength);
  98. if (ignorePattern !== undefined && ignorePattern.test(lineContent)) {
  99. continue;
  100. }
  101. ctx.addFailureAt(line.pos, line.contentLength, Rule.FAILURE_STRING_FACTORY(limit));
  102. }
  103. }
  104. var templateObject_1, templateObject_2;