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 Lint = require("../index");
21 var Rule = /** @class */ (function (_super) {
22 tslib_1.__extends(Rule, _super);
24 return _super !== null && _super.apply(this, arguments) || this;
26 Rule.prototype.apply = function (sourceFile) {
27 var length = sourceFile.text.length;
28 if (length === 0 || // if the file is empty, it "ends with a newline", so don't return a failure
29 sourceFile.text[length - 1] === "\n") {
33 var lines = sourceFile.getLineStarts();
34 if (lines.length > 1) {
35 fix = Lint.Replacement.appendText(length, sourceFile.text[lines[1] - 2] === "\r" ? "\r\n" : "\n");
38 new Lint.RuleFailure(sourceFile, length, length, Rule.FAILURE_STRING, this.ruleName, fix),
41 /* tslint:disable:object-literal-sort-keys */
44 description: "Ensures the file ends with a newline.",
45 descriptionDetails: "Fix for single-line files is not supported.",
46 rationale: "It is a [standard convention](https://stackoverflow.com/q/729692/3124288) to end files with a newline.",
47 optionsDescription: "Not configurable.",
49 optionExamples: [true],
52 typescriptOnly: false,
54 /* tslint:enable:object-literal-sort-keys */
55 Rule.FAILURE_STRING = "file should end with a newline";
57 }(Lint.Rules.AbstractRule));