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");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var OPTION_MATCH = "match";
24 var OPTION_ALLOW_SINGLE_LINE_COMMENTS = "allow-single-line-comments";
25 var OPTION_DEFAULT = "default";
26 var OPTION_ENFORCE_TRAILING_NEWLINE = "enforce-trailing-newline";
27 var Rule = /** @class */ (function (_super) {
28 tslib_1.__extends(Rule, _super);
30 return _super !== null && _super.apply(this, arguments) || this;
32 Rule.prototype.apply = function (sourceFile) {
33 var options = this.getRuleOptions();
34 var text = sourceFile.text;
35 var headerFormat = new RegExp(options[OPTION_MATCH]);
36 var textToInsert = options[OPTION_DEFAULT];
37 // ignore shebang if it exists
38 var offset = text.startsWith("#!") ? text.indexOf("\n") : 0;
39 var commentText = this.getFileHeaderText(text, offset, !!options[OPTION_ALLOW_SINGLE_LINE_COMMENTS]);
40 if (commentText === undefined || !headerFormat.test(commentText)) {
41 var isErrorAtStart = offset === 0;
42 if (!isErrorAtStart) {
43 ++offset; // show warning in next line after shebang
45 var leadingNewlines = isErrorAtStart ? 0 : 1;
46 var trailingNewlines = isErrorAtStart ? 2 : 1;
47 var fix = textToInsert !== undefined
48 ? Lint.Replacement.appendText(offset, this.createComment(sourceFile, textToInsert, leadingNewlines, trailingNewlines))
51 new Lint.RuleFailure(sourceFile, offset, offset, Rule.MISSING_HEADER_FAILURE_STRING, this.ruleName, fix),
54 var trailingNewLineViolation = options[OPTION_ENFORCE_TRAILING_NEWLINE] &&
55 headerFormat.test(commentText) &&
56 this.doesNewLineEndingViolationExist(text, offset);
57 if (trailingNewLineViolation) {
58 var trailingCommentRanges = ts.getTrailingCommentRanges(text, offset);
59 var endOfComment = trailingCommentRanges[0].end;
60 var lineEnding = this.generateLineEnding(sourceFile);
61 var fix = textToInsert !== undefined
62 ? Lint.Replacement.appendText(endOfComment, lineEnding)
65 new Lint.RuleFailure(sourceFile, offset, offset, Rule.MISSING_NEW_LINE_FAILURE_STRING, this.ruleName, fix),
70 Rule.prototype.getRuleOptions = function () {
72 var options = this.ruleArguments;
73 if (options.length === 1 && typeof options[0] === "object") {
77 var args = this.ruleArguments;
79 _a[OPTION_DEFAULT] = args[1],
80 _a[OPTION_ENFORCE_TRAILING_NEWLINE] = args[2] !== undefined
81 ? args[2].indexOf(OPTION_ENFORCE_TRAILING_NEWLINE) !== -1
83 _a[OPTION_MATCH] = args[0],
86 Rule.prototype.createComment = function (sourceFile, commentText, leadingNewlines, trailingNewlines) {
87 if (leadingNewlines === void 0) { leadingNewlines = 1; }
88 if (trailingNewlines === void 0) { trailingNewlines = 1; }
89 var lineEnding = this.generateLineEnding(sourceFile);
90 return (lineEnding.repeat(leadingNewlines) +
93 ].concat(commentText.split(/\r?\n/g).map(function (line) { return (" * " + line).replace(/\s+$/, ""); }), [
96 lineEnding.repeat(trailingNewlines));
98 Rule.prototype.generateLineEnding = function (sourceFile) {
99 var maybeCarriageReturn = sourceFile.text[sourceFile.getLineEndOfPosition(0)] === "\r" ? "\r" : "";
100 return maybeCarriageReturn + "\n";
102 Rule.prototype.doesNewLineEndingViolationExist = function (text, offset) {
103 var entireComment = ts.forEachLeadingCommentRange(text, offset, function (pos, end) {
104 return text.substring(pos, end + 2);
106 var NEW_LINE_FOLLOWING_HEADER = /^.*((\r)?\n){2,}$/gm;
107 return (entireComment !== undefined && NEW_LINE_FOLLOWING_HEADER.test(entireComment) !== null);
109 Rule.prototype.getFileHeaderText = function (text, offset, allowSingleLineComments) {
110 var ranges = ts.getLeadingCommentRanges(text, offset);
111 if (ranges === undefined || ranges.length === 0) {
114 var fileHeaderRanges = !allowSingleLineComments ? ranges.slice(0, 1) : ranges;
115 return fileHeaderRanges
116 .map(function (range) {
117 var pos = range.pos, kind = range.kind, end = range.end;
118 return text.substring(pos + 2, kind === ts.SyntaxKind.SingleLineCommentTrivia ? end : end - 2);
122 /* tslint:disable:object-literal-sort-keys */
124 ruleName: "file-header",
125 description: "Enforces a certain header comment for all files, matched by a regular expression.",
126 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n A single object may be passed in for configuration that must contain:\n\n * `", "`: a regular expression that all headers should match\n\n Any of the following optional fields may also be provided:\n\n * `", "`: a boolean for whether `//` should be considered file headers in addition to `/*` comments\n * `", "`: text to add for file headers when running in `--fix` mode\n * `", "`: a boolean for whether a newline must follow the header\n\n The rule will also accept array of strings as a legacy form of options, though the object form is recommended.\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 third argument, which is optional, is a string that denotes whether or not a newline should\n exist on the header."], ["\n A single object may be passed in for configuration that must contain:\n\n * \\`", "\\`: a regular expression that all headers should match\n\n Any of the following optional fields may also be provided:\n\n * \\`", "\\`: a boolean for whether \\`//\\` should be considered file headers in addition to \\`/*\\` comments\n * \\`", "\\`: text to add for file headers when running in \\`--fix\\` mode\n * \\`", "\\`: a boolean for whether a newline must follow the header\n\n The rule will also accept array of strings as a legacy form of options, though the object form is recommended.\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 third argument, which is optional, is a string that denotes whether or not a newline should\n exist on the header."])), OPTION_MATCH, OPTION_ALLOW_SINGLE_LINE_COMMENTS, OPTION_DEFAULT, OPTION_ENFORCE_TRAILING_NEWLINE),
133 properties: (_a = {},
137 _a[OPTION_ALLOW_SINGLE_LINE_COMMENTS] = {
140 _a[OPTION_DEFAULT] = {
143 _a[OPTION_ENFORCE_TRAILING_NEWLINE] = {
147 additionalProperties: false,
163 additionalItems: false,
173 _b[OPTION_MATCH] = "Copyright \\d{4}",
174 _b[OPTION_ALLOW_SINGLE_LINE_COMMENTS] = true,
175 _b[OPTION_DEFAULT] = "Copyright 2018",
176 _b[OPTION_ENFORCE_TRAILING_NEWLINE] = true,
179 [true, "Copyright \\d{4}", "Copyright 2018", OPTION_ENFORCE_TRAILING_NEWLINE],
183 typescriptOnly: false,
185 /* tslint:enable:object-literal-sort-keys */
186 Rule.MISSING_HEADER_FAILURE_STRING = "missing file header";
187 Rule.MISSING_NEW_LINE_FAILURE_STRING = "missing new line following the file header";
189 }(Lint.Rules.AbstractRule));
191 var templateObject_1;