linebreakStyleRule.js 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 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 OPTION_LINEBREAK_STYLE_CRLF = "CRLF";
  22. var OPTION_LINEBREAK_STYLE_LF = "LF";
  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. Rule.prototype.apply = function (sourceFile) {
  29. return this.applyWithFunction(sourceFile, walk, this.ruleArguments.indexOf(OPTION_LINEBREAK_STYLE_CRLF) !== -1);
  30. };
  31. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  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),
  36. options: {
  37. type: "string",
  38. enum: [OPTION_LINEBREAK_STYLE_LF, OPTION_LINEBREAK_STYLE_CRLF],
  39. },
  40. optionExamples: [[true, OPTION_LINEBREAK_STYLE_LF], [true, OPTION_LINEBREAK_STYLE_CRLF]],
  41. type: "maintainability",
  42. typescriptOnly: false,
  43. hasFix: true,
  44. };
  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 + "'";
  48. return Rule;
  49. }(Lint.Rules.AbstractRule));
  50. exports.Rule = Rule;
  51. function walk(ctx) {
  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") {
  58. if (!expectedCr) {
  59. ctx.addFailure(lineStarts[i - 1], lineEnd - 1, Rule.FAILURE_LF, Lint.Replacement.deleteText(lineEnd - 1, 1));
  60. }
  61. }
  62. else if (expectedCr) {
  63. ctx.addFailure(lineStarts[i - 1], lineEnd, Rule.FAILURE_CRLF, Lint.Replacement.appendText(lineEnd, "\r"));
  64. }
  65. }
  66. }
  67. var templateObject_1;