4 * Copyright 2018 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 Lint = require("../index");
21 var OPTION_LINEBREAK_STYLE_CRLF = "CRLF";
22 var OPTION_LINEBREAK_STYLE_LF = "LF";
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.prototype.apply = function (sourceFile) {
29 return this.applyWithFunction(sourceFile, walk, this.ruleArguments.indexOf(OPTION_LINEBREAK_STYLE_CRLF) !== -1);
31 /* tslint:disable:object-literal-sort-keys */
33 ruleName: "linebreak-style",
34 description: "Enforces a consistent linebreak style.",
35 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n One of the following options must be provided:\n\n * `\"", "\"` requires LF (`\\n`) linebreaks\n * `\"", "\"` requires CRLF (`\\r\\n`) linebreaks"], ["\n One of the following options must be provided:\n\n * \\`\"", "\"\\` requires LF (\\`\\\\n\\`) linebreaks\n * \\`\"", "\"\\` requires CRLF (\\`\\\\r\\\\n\\`) linebreaks"])), OPTION_LINEBREAK_STYLE_LF, OPTION_LINEBREAK_STYLE_CRLF),
38 enum: [OPTION_LINEBREAK_STYLE_LF, OPTION_LINEBREAK_STYLE_CRLF],
40 optionExamples: [[true, OPTION_LINEBREAK_STYLE_LF], [true, OPTION_LINEBREAK_STYLE_CRLF]],
42 typescriptOnly: false,
45 /* tslint:enable:object-literal-sort-keys */
46 Rule.FAILURE_CRLF = "Expected linebreak to be '" + OPTION_LINEBREAK_STYLE_CRLF + "'";
47 Rule.FAILURE_LF = "Expected linebreak to be '" + OPTION_LINEBREAK_STYLE_LF + "'";
49 }(Lint.Rules.AbstractRule));
52 var expectedCr = ctx.options;
53 var sourceText = ctx.sourceFile.text;
54 var lineStarts = ctx.sourceFile.getLineStarts();
55 for (var i = 1; i < lineStarts.length; ++i) {
56 var lineEnd = lineStarts[i] - 1;
57 if (sourceText[lineEnd - 1] === "\r") {
59 ctx.addFailure(lineStarts[i - 1], lineEnd - 1, Rule.FAILURE_LF, Lint.Replacement.deleteText(lineEnd - 1, 1));
62 else if (expectedCr) {
63 ctx.addFailure(lineStarts[i - 1], lineEnd, Rule.FAILURE_CRLF, Lint.Replacement.appendText(lineEnd, "\r"));