.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noConsecutiveBlankLinesRule.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 utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24     tslib_1.__extends(Rule, _super);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
28     /* tslint:enable:object-literal-sort-keys */
29     Rule.FAILURE_STRING_FACTORY = function (allowed) {
30         return allowed === 1
31             ? "Consecutive blank lines are forbidden"
32             : "Exceeds the " + allowed + " allowed consecutive blank lines";
33     };
34     /**
35      * Disable the rule if the option is provided but non-numeric or less than the minimum.
36      */
37     Rule.prototype.isEnabled = function () {
38         var option = this.ruleArguments[0];
39         return _super.prototype.isEnabled.call(this) && (option === undefined || option > 0);
40     };
41     Rule.prototype.apply = function (sourceFile) {
42         var limit = this.ruleArguments[0];
43         return this.applyWithFunction(sourceFile, walk, limit !== undefined ? limit : Rule.DEFAULT_ALLOWED_BLANKS);
44     };
45     Rule.DEFAULT_ALLOWED_BLANKS = 1;
46     /* tslint:disable:object-literal-sort-keys */
47     Rule.metadata = {
48         ruleName: "no-consecutive-blank-lines",
49         description: "Disallows one or more blank lines in a row.",
50         hasFix: true,
51         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Helps maintain a readable style in your codebase.\n\n            Extra blank lines take up extra space and add little to a semantic understanding of the code.\n            It can be harder to read through files when fewer components can fit into the screen.\n            If you find a file is so large you feel a need to split them up with extra blank lines or comments,\n            consider splitting your file into smaller files.\n        "], ["\n            Helps maintain a readable style in your codebase.\n\n            Extra blank lines take up extra space and add little to a semantic understanding of the code.\n            It can be harder to read through files when fewer components can fit into the screen.\n            If you find a file is so large you feel a need to split them up with extra blank lines or comments,\n            consider splitting your file into smaller files.\n        "]))),
52         optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            An optional number of maximum allowed sequential blanks can be specified. If no value\n            is provided, a default of ", " will be used."], ["\n            An optional number of maximum allowed sequential blanks can be specified. If no value\n            is provided, a default of ", " will be used."])), Rule.DEFAULT_ALLOWED_BLANKS),
53         options: {
54             type: "number",
55             minimum: "1",
56         },
57         optionExamples: [true, [true, 2]],
58         type: "formatting",
59         typescriptOnly: false,
60     };
61     return Rule;
62 }(Lint.Rules.AbstractRule));
63 exports.Rule = Rule;
64 function walk(ctx) {
65     var sourceText = ctx.sourceFile.text;
66     var threshold = ctx.options + 1;
67     var possibleFailures = [];
68     var consecutiveBlankLines = 0;
69     for (var _i = 0, _a = utils.getLineRanges(ctx.sourceFile); _i < _a.length; _i++) {
70         var line = _a[_i];
71         if (line.contentLength === 0 ||
72             sourceText.substr(line.pos, line.contentLength).search(/\S/) === -1) {
73             ++consecutiveBlankLines;
74             if (consecutiveBlankLines === threshold) {
75                 possibleFailures.push({
76                     end: line.end,
77                     pos: line.pos,
78                 });
79             }
80             else if (consecutiveBlankLines > threshold) {
81                 possibleFailures[possibleFailures.length - 1].end = line.end;
82             }
83         }
84         else {
85             consecutiveBlankLines = 0;
86         }
87     }
88     if (possibleFailures.length === 0) {
89         return;
90     }
91     var failureString = Rule.FAILURE_STRING_FACTORY(ctx.options);
92     var templateRanges = getTemplateRanges(ctx.sourceFile);
93     var _loop_1 = function (possibleFailure) {
94         if (!templateRanges.some(function (template) {
95             return template.pos < possibleFailure.pos && possibleFailure.pos < template.end;
96         })) {
97             ctx.addFailureAt(possibleFailure.pos, 1, failureString, [
98                 Lint.Replacement.deleteFromTo(
99                 // special handling for fixing blank lines at the end of the file
100                 // to fix this we need to cut off the line break of the last allowed blank line, too
101                 possibleFailure.end === sourceText.length
102                     ? getStartOfLineBreak(sourceText, possibleFailure.pos)
103                     : possibleFailure.pos, possibleFailure.end),
104             ]);
105         }
106     };
107     for (var _b = 0, possibleFailures_1 = possibleFailures; _b < possibleFailures_1.length; _b++) {
108         var possibleFailure = possibleFailures_1[_b];
109         _loop_1(possibleFailure);
110     }
111 }
112 function getStartOfLineBreak(sourceText, pos) {
113     return sourceText[pos - 2] === "\r" ? pos - 1 : pos - 1;
114 }
115 function getTemplateRanges(sourceFile) {
116     var intervals = [];
117     var cb = function (node) {
118         if (node.kind >= ts.SyntaxKind.FirstTemplateToken &&
119             node.kind <= ts.SyntaxKind.LastTemplateToken) {
120             intervals.push({
121                 end: node.end,
122                 pos: node.getStart(sourceFile),
123             });
124         }
125         else {
126             return ts.forEachChild(node, cb);
127         }
128     };
129     ts.forEachChild(sourceFile, cb);
130     return intervals;
131 }
132 exports.getTemplateRanges = getTemplateRanges;
133 var templateObject_1, templateObject_2;