diff options
Diffstat (limited to 'node_modules/tslint/lib/rules/maxLineLengthRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/maxLineLengthRule.js | 63 |
1 files changed, 52 insertions, 11 deletions
diff --git a/node_modules/tslint/lib/rules/maxLineLengthRule.js b/node_modules/tslint/lib/rules/maxLineLengthRule.js index b6b1ebfca..465efcbed 100644 --- a/node_modules/tslint/lib/rules/maxLineLengthRule.js +++ b/node_modules/tslint/lib/rules/maxLineLengthRule.js @@ -29,22 +29,57 @@ var Rule = /** @class */ (function (_super) { return "Exceeds maximum line length of " + lineLimit; }; Rule.prototype.isEnabled = function () { - return _super.prototype.isEnabled.call(this) && this.ruleArguments[0] > 0; + var limit = this.getRuleOptions().limit; + return _super.prototype.isEnabled.call(this) && (limit > 0); }; Rule.prototype.apply = function (sourceFile) { - return this.applyWithFunction(sourceFile, walk, this.ruleArguments[0]); + return this.applyWithFunction(sourceFile, walk, this.getRuleOptions()); + }; + Rule.prototype.getRuleOptions = function () { + var argument = this.ruleArguments[0]; + var options = { limit: 0 }; + if (typeof argument === "number") { + options.limit = argument; + } + else { + options = argument; + var ignorePattern = argument["ignore-pattern"]; + options.ignorePattern = (typeof ignorePattern === "string") ? + new RegExp((ignorePattern)) : undefined; + } + options.limit = Number(options.limit); // user can pass a string instead of number + return options; }; /* tslint:disable:object-literal-sort-keys */ Rule.metadata = { ruleName: "max-line-length", description: "Requires lines to be under a certain max length.", - rationale: (_a = ["\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."], _a.raw = ["\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."], Lint.Utils.dedent(_a)), - optionsDescription: "An integer indicating the max length of lines.", + 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."]))), + 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 "]))), options: { - type: "number", - minimum: "1", + type: "array", + items: { + oneOf: [ + { + type: "number", + }, + { + type: "object", + properties: { + "limit": { type: "number" }, + "ignore-pattern": { type: "string" }, + }, + additionalProperties: false, + }, + ], + }, + minLength: 1, + maxLength: 2, }, - optionExamples: [[true, 120]], + optionExamples: [[true, 120], [true, { + "limit": 120, + "ignore-pattern": "^import |^export \{(.*?)\}" + }]], type: "maintainability", typescriptOnly: false, }; @@ -52,12 +87,18 @@ var Rule = /** @class */ (function (_super) { }(Lint.Rules.AbstractRule)); exports.Rule = Rule; function walk(ctx) { - var limit = ctx.options; + var limit = ctx.options.limit; + var ignorePattern = ctx.options.ignorePattern; for (var _i = 0, _a = tsutils_1.getLineRanges(ctx.sourceFile); _i < _a.length; _i++) { var line = _a[_i]; - if (line.contentLength > limit) { - ctx.addFailureAt(line.pos, line.contentLength, Rule.FAILURE_STRING_FACTORY(limit)); + if (line.contentLength <= limit) { + continue; + } + var lineContent = ctx.sourceFile.text.substr(line.pos, line.contentLength); + if (ignorePattern !== undefined && ignorePattern.test(lineContent)) { + continue; } + ctx.addFailureAt(line.pos, line.contentLength, Rule.FAILURE_STRING_FACTORY(limit)); } } -var _a; +var templateObject_1, templateObject_2; |