.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / maxLineLengthRule.js
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 _a, _b;
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);
30     function Rule() {
31         return _super !== null && _super.apply(this, arguments) || this;
32     }
33     /* tslint:enable:object-literal-sort-keys */
34     Rule.FAILURE_STRING_FACTORY = function (lineLimit) {
35         return "Exceeds maximum line length of " + lineLimit;
36     };
37     Rule.prototype.isEnabled = function () {
38         var limit = this.getRuleOptions()[OPTION_LIMIT];
39         return _super.prototype.isEnabled.call(this) && limit > 0;
40     };
41     Rule.prototype.apply = function (sourceFile) {
42         return this.applyWithFunction(sourceFile, walk, this.getRuleOptions());
43     };
44     Rule.prototype.getRuleOptions = function () {
45         var argument = this.ruleArguments[0];
46         if (typeof argument === "number") {
47             return { limit: argument };
48         }
49         else {
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];
51             return {
52                 checkRegex: Boolean(checkRegex),
53                 checkStrings: Boolean(checkStrings),
54                 ignorePattern: typeof ignorePattern === "string" ? new RegExp(ignorePattern) : undefined,
55                 limit: Number(limit),
56             };
57         }
58     };
59     /* tslint:disable:object-literal-sort-keys */
60     Rule.metadata = {
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),
65         options: {
66             type: "array",
67             items: {
68                 oneOf: [
69                     {
70                         type: "number",
71                     },
72                     {
73                         type: "object",
74                         properties: (_a = {},
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" },
79                             _a),
80                         additionalProperties: false,
81                     },
82                 ],
83             },
84             minLength: 1,
85             maxLength: 2,
86         },
87         optionExamples: [
88             [true, 120],
89             [
90                 true,
91                 (_b = {},
92                     _b[OPTION_LIMIT] = 120,
93                     _b[OPTION_IGNORE_PATTERN] = "^import |^export {(.*?)}",
94                     _b[OPTION_CHECK_STRINGS] = true,
95                     _b[OPTION_CHECK_REGEX] = true,
96                     _b),
97             ],
98         ],
99         type: "formatting",
100         typescriptOnly: false,
101     };
102     return Rule;
103 }(Lint.Rules.AbstractRule));
104 exports.Rule = Rule;
105 function walk(ctx) {
106     var limit = ctx.options.limit;
107     tsutils_1.getLineRanges(ctx.sourceFile)
108         .filter(function (line) {
109         return line.contentLength > limit && !shouldIgnoreLine(line, ctx);
110     })
111         .forEach(function (_a) {
112         var pos = _a.pos, contentLength = _a.contentLength;
113         return ctx.addFailureAt(pos, contentLength, Rule.FAILURE_STRING_FACTORY(limit));
114     });
115     return;
116 }
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) {
123         shouldOmitLine =
124             shouldOmitLine || ignorePattern.test(sourceFile.text.substr(pos, contentLength));
125     }
126     if (!checkStrings) {
127         var nodeAtLimit = tsutils_1.getTokenAtPosition(sourceFile, pos + limit);
128         if (nodeAtLimit !== undefined) {
129             shouldOmitLine =
130                 shouldOmitLine ||
131                     nodeAtLimit.kind === ts.SyntaxKind.StringLiteral ||
132                     nodeAtLimit.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral ||
133                     hasParentMatchingTypes(nodeAtLimit, sourceFile, [ts.SyntaxKind.TemplateExpression]);
134         }
135     }
136     if (!checkRegex) {
137         var nodeAtLimit = tsutils_1.getTokenAtPosition(sourceFile, pos + limit);
138         if (nodeAtLimit !== undefined) {
139             shouldOmitLine =
140                 shouldOmitLine || nodeAtLimit.kind === ts.SyntaxKind.RegularExpressionLiteral;
141         }
142     }
143     return shouldOmitLine;
144 }
145 function hasParentMatchingTypes(node, root, parentTypes) {
146     var nodeReference = node;
147     while (nodeReference !== root) {
148         if (parentTypes.indexOf(nodeReference.kind) >= 0) {
149             return true;
150         }
151         nodeReference = nodeReference.parent;
152     }
153     return false;
154 }
155 var templateObject_1, templateObject_2;