4 * Copyright 2013 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var OPTION_LIMIT = "limit";
25 var OPTION_IGNORE_PATTERN = "ignore-pattern";
26 var OPTION_CHECK_STRINGS = "check-strings";
27 var OPTION_CHECK_REGEX = "check-regex";
28 var Rule = /** @class */ (function (_super) {
29 tslib_1.__extends(Rule, _super);
31 return _super !== null && _super.apply(this, arguments) || this;
33 /* tslint:enable:object-literal-sort-keys */
34 Rule.FAILURE_STRING_FACTORY = function (lineLimit) {
35 return "Exceeds maximum line length of " + lineLimit;
37 Rule.prototype.isEnabled = function () {
38 var limit = this.getRuleOptions()[OPTION_LIMIT];
39 return _super.prototype.isEnabled.call(this) && limit > 0;
41 Rule.prototype.apply = function (sourceFile) {
42 return this.applyWithFunction(sourceFile, walk, this.getRuleOptions());
44 Rule.prototype.getRuleOptions = function () {
45 var argument = this.ruleArguments[0];
46 if (typeof argument === "number") {
47 return { limit: argument };
50 var _a = OPTION_CHECK_REGEX, checkRegex = argument[_a], _b = OPTION_CHECK_STRINGS, checkStrings = argument[_b], _c = OPTION_IGNORE_PATTERN, ignorePattern = argument[_c], _d = OPTION_LIMIT, limit = argument[_d];
52 checkRegex: Boolean(checkRegex),
53 checkStrings: Boolean(checkStrings),
54 ignorePattern: typeof ignorePattern === "string" ? new RegExp(ignorePattern) : undefined,
59 /* tslint:disable:object-literal-sort-keys */
61 ruleName: "max-line-length",
62 description: "Requires lines to be under a certain max length.",
63 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."]))),
64 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 * `", "` - number greater than 0 defining the max line length\n * `", "` - 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 * `", "` - determines if strings should be checked, `false` by default.\n * `", "` - determines if regular expressions should be checked, `false` by default.\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 * \\`", "\\` - number greater than 0 defining the max line length\n * \\`", "\\` - 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 * \\`", "\\` - determines if strings should be checked, \\`false\\` by default.\n * \\`", "\\` - determines if regular expressions should be checked, \\`false\\` by default.\n "])), OPTION_LIMIT, OPTION_IGNORE_PATTERN, OPTION_CHECK_STRINGS, OPTION_CHECK_REGEX),
75 _a[OPTION_LIMIT] = { type: "number" },
76 _a[OPTION_IGNORE_PATTERN] = { type: "string" },
77 _a[OPTION_CHECK_STRINGS] = { type: "boolean" },
78 _a[OPTION_CHECK_REGEX] = { type: "boolean" },
80 additionalProperties: false,
92 _b[OPTION_LIMIT] = 120,
93 _b[OPTION_IGNORE_PATTERN] = "^import |^export {(.*?)}",
94 _b[OPTION_CHECK_STRINGS] = true,
95 _b[OPTION_CHECK_REGEX] = true,
100 typescriptOnly: false,
103 }(Lint.Rules.AbstractRule));
106 var limit = ctx.options.limit;
107 tsutils_1.getLineRanges(ctx.sourceFile)
108 .filter(function (line) {
109 return line.contentLength > limit && !shouldIgnoreLine(line, ctx);
111 .forEach(function (_a) {
112 var pos = _a.pos, contentLength = _a.contentLength;
113 return ctx.addFailureAt(pos, contentLength, Rule.FAILURE_STRING_FACTORY(limit));
117 function shouldIgnoreLine(_a, _b) {
118 var pos = _a.pos, contentLength = _a.contentLength;
119 var options = _b.options, sourceFile = _b.sourceFile;
120 var checkRegex = options.checkRegex, checkStrings = options.checkStrings, ignorePattern = options.ignorePattern, limit = options.limit;
121 var shouldOmitLine = false;
122 if (ignorePattern !== undefined) {
124 shouldOmitLine || ignorePattern.test(sourceFile.text.substr(pos, contentLength));
127 var nodeAtLimit = tsutils_1.getTokenAtPosition(sourceFile, pos + limit);
128 if (nodeAtLimit !== undefined) {
131 nodeAtLimit.kind === ts.SyntaxKind.StringLiteral ||
132 nodeAtLimit.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral ||
133 hasParentMatchingTypes(nodeAtLimit, sourceFile, [ts.SyntaxKind.TemplateExpression]);
137 var nodeAtLimit = tsutils_1.getTokenAtPosition(sourceFile, pos + limit);
138 if (nodeAtLimit !== undefined) {
140 shouldOmitLine || nodeAtLimit.kind === ts.SyntaxKind.RegularExpressionLiteral;
143 return shouldOmitLine;
145 function hasParentMatchingTypes(node, root, parentTypes) {
146 var nodeReference = node;
147 while (nodeReference !== root) {
148 if (parentTypes.indexOf(nodeReference.kind) >= 0) {
151 nodeReference = nodeReference.parent;
155 var templateObject_1, templateObject_2;