fileHeaderRule.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 ts = require("typescript");
  21. var Lint = require("../index");
  22. var Rule = /** @class */ (function (_super) {
  23. tslib_1.__extends(Rule, _super);
  24. function Rule() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. Rule.prototype.apply = function (sourceFile) {
  28. var text = sourceFile.text;
  29. var headerFormat = new RegExp(this.ruleArguments[0]);
  30. var textToInsert = this.ruleArguments[1];
  31. // ignore shebang if it exists
  32. var offset = text.startsWith("#!") ? text.indexOf("\n") : 0;
  33. // returns the text of the first comment or undefined
  34. var commentText = ts.forEachLeadingCommentRange(text, offset, function (pos, end, kind) { return text.substring(pos + 2, kind === ts.SyntaxKind.SingleLineCommentTrivia ? end : end - 2); });
  35. if (commentText === undefined || !headerFormat.test(commentText)) {
  36. var isErrorAtStart = offset === 0;
  37. if (!isErrorAtStart) {
  38. ++offset; // show warning in next line after shebang
  39. }
  40. var leadingNewlines = isErrorAtStart ? 0 : 1;
  41. var trailingNewlines = isErrorAtStart ? 2 : 1;
  42. var fix = textToInsert !== undefined
  43. ? Lint.Replacement.appendText(offset, this.createComment(sourceFile, textToInsert, leadingNewlines, trailingNewlines))
  44. : undefined;
  45. return [new Lint.RuleFailure(sourceFile, offset, offset, Rule.FAILURE_STRING, this.ruleName, fix)];
  46. }
  47. return [];
  48. };
  49. Rule.prototype.createComment = function (sourceFile, commentText, leadingNewlines, trailingNewlines) {
  50. if (leadingNewlines === void 0) { leadingNewlines = 1; }
  51. if (trailingNewlines === void 0) { trailingNewlines = 1; }
  52. var maybeCarriageReturn = sourceFile.text[sourceFile.getLineEndOfPosition(0)] === "\r" ? "\r" : "";
  53. var lineEnding = maybeCarriageReturn + "\n";
  54. return lineEnding.repeat(leadingNewlines) + [
  55. "/*!"
  56. ].concat(commentText.split(/\r?\n/g).map(function (line) { return (" * " + line).replace(/\s+$/, ""); }), [
  57. " */",
  58. ]).join(lineEnding) + lineEnding.repeat(trailingNewlines);
  59. };
  60. /* tslint:disable:object-literal-sort-keys */
  61. Rule.metadata = {
  62. ruleName: "file-header",
  63. description: "Enforces a certain header comment for all files, matched by a regular expression.",
  64. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n The first option, which is mandatory, is a regular expression that all headers should match.\n The second argument, which is optional, is a string that should be inserted as a header comment\n if fixing is enabled and no header that matches the first argument is found."], ["\n The first option, which is mandatory, is a regular expression that all headers should match.\n The second argument, which is optional, is a string that should be inserted as a header comment\n if fixing is enabled and no header that matches the first argument is found."]))),
  65. options: {
  66. type: "array",
  67. items: [
  68. {
  69. type: "string",
  70. },
  71. {
  72. type: "string",
  73. },
  74. ],
  75. additionalItems: false,
  76. minLength: 1,
  77. maxLength: 2,
  78. },
  79. optionExamples: [[true, "Copyright \\d{4}", "Copyright 2017"]],
  80. hasFix: true,
  81. type: "style",
  82. typescriptOnly: false,
  83. };
  84. /* tslint:enable:object-literal-sort-keys */
  85. Rule.FAILURE_STRING = "missing file header";
  86. return Rule;
  87. }(Lint.Rules.AbstractRule));
  88. exports.Rule = Rule;
  89. var templateObject_1;