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");
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);
26 return _super !== null && _super.apply(this, arguments) || this;
28 /* tslint:enable:object-literal-sort-keys */
29 Rule.FAILURE_STRING_FACTORY = function (allowed) {
31 ? "Consecutive blank lines are forbidden"
32 : "Exceeds the " + allowed + " allowed consecutive blank lines";
35 * Disable the rule if the option is provided but non-numeric or less than the minimum.
37 Rule.prototype.isEnabled = function () {
38 var option = this.ruleArguments[0];
39 return _super.prototype.isEnabled.call(this) && (option === undefined || option > 0);
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);
45 Rule.DEFAULT_ALLOWED_BLANKS = 1;
46 /* tslint:disable:object-literal-sort-keys */
48 ruleName: "no-consecutive-blank-lines",
49 description: "Disallows one or more blank lines in a row.",
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),
57 optionExamples: [true, [true, 2]],
59 typescriptOnly: false,
62 }(Lint.Rules.AbstractRule));
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++) {
71 if (line.contentLength === 0 ||
72 sourceText.substr(line.pos, line.contentLength).search(/\S/) === -1) {
73 ++consecutiveBlankLines;
74 if (consecutiveBlankLines === threshold) {
75 possibleFailures.push({
80 else if (consecutiveBlankLines > threshold) {
81 possibleFailures[possibleFailures.length - 1].end = line.end;
85 consecutiveBlankLines = 0;
88 if (possibleFailures.length === 0) {
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;
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),
107 for (var _b = 0, possibleFailures_1 = possibleFailures; _b < possibleFailures_1.length; _b++) {
108 var possibleFailure = possibleFailures_1[_b];
109 _loop_1(possibleFailure);
112 function getStartOfLineBreak(sourceText, pos) {
113 return sourceText[pos - 2] === "\r" ? pos - 1 : pos - 1;
115 function getTemplateRanges(sourceFile) {
117 var cb = function (node) {
118 if (node.kind >= ts.SyntaxKind.FirstTemplateToken &&
119 node.kind <= ts.SyntaxKind.LastTemplateToken) {
122 pos: node.getStart(sourceFile),
126 return ts.forEachChild(node, cb);
129 ts.forEachChild(sourceFile, cb);
132 exports.getTemplateRanges = getTemplateRanges;
133 var templateObject_1, templateObject_2;