eoflineRule.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 Lint = require("../index");
  21. var Rule = /** @class */ (function (_super) {
  22. tslib_1.__extends(Rule, _super);
  23. function Rule() {
  24. return _super !== null && _super.apply(this, arguments) || this;
  25. }
  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") {
  30. return [];
  31. }
  32. var fix;
  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");
  36. }
  37. return [new Lint.RuleFailure(sourceFile, length, length, Rule.FAILURE_STRING, this.ruleName, fix)];
  38. };
  39. /* tslint:disable:object-literal-sort-keys */
  40. Rule.metadata = {
  41. ruleName: "eofline",
  42. description: "Ensures the file ends with a newline.",
  43. descriptionDetails: "Fix for single-line files is not supported.",
  44. rationale: "It is a [standard convention](https://stackoverflow.com/q/729692/3124288) to end files with a newline.",
  45. optionsDescription: "Not configurable.",
  46. options: null,
  47. optionExamples: [true],
  48. hasFix: true,
  49. type: "maintainability",
  50. typescriptOnly: false,
  51. };
  52. /* tslint:enable:object-literal-sort-keys */
  53. Rule.FAILURE_STRING = "file should end with a newline";
  54. return Rule;
  55. }(Lint.Rules.AbstractRule));
  56. exports.Rule = Rule;