.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / fileHeaderRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 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 _a, _b;
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);
29     function Rule() {
30         return _super !== null && _super.apply(this, arguments) || this;
31     }
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
44             }
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))
49                 : undefined;
50             return [
51                 new Lint.RuleFailure(sourceFile, offset, offset, Rule.MISSING_HEADER_FAILURE_STRING, this.ruleName, fix),
52             ];
53         }
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)
63                 : undefined;
64             return [
65                 new Lint.RuleFailure(sourceFile, offset, offset, Rule.MISSING_NEW_LINE_FAILURE_STRING, this.ruleName, fix),
66             ];
67         }
68         return [];
69     };
70     Rule.prototype.getRuleOptions = function () {
71         var _a;
72         var options = this.ruleArguments;
73         if (options.length === 1 && typeof options[0] === "object") {
74             return options[0];
75         }
76         // Legacy options
77         var args = this.ruleArguments;
78         return _a = {},
79             _a[OPTION_DEFAULT] = args[1],
80             _a[OPTION_ENFORCE_TRAILING_NEWLINE] = args[2] !== undefined
81                 ? args[2].indexOf(OPTION_ENFORCE_TRAILING_NEWLINE) !== -1
82                 : undefined,
83             _a[OPTION_MATCH] = args[0],
84             _a;
85     };
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) +
91             [
92                 "/*!"
93             ].concat(commentText.split(/\r?\n/g).map(function (line) { return (" * " + line).replace(/\s+$/, ""); }), [
94                 " */",
95             ]).join(lineEnding) +
96             lineEnding.repeat(trailingNewlines));
97     };
98     Rule.prototype.generateLineEnding = function (sourceFile) {
99         var maybeCarriageReturn = sourceFile.text[sourceFile.getLineEndOfPosition(0)] === "\r" ? "\r" : "";
100         return maybeCarriageReturn + "\n";
101     };
102     Rule.prototype.doesNewLineEndingViolationExist = function (text, offset) {
103         var entireComment = ts.forEachLeadingCommentRange(text, offset, function (pos, end) {
104             return text.substring(pos, end + 2);
105         });
106         var NEW_LINE_FOLLOWING_HEADER = /^.*((\r)?\n){2,}$/gm;
107         return (entireComment !== undefined && NEW_LINE_FOLLOWING_HEADER.test(entireComment) !== null);
108     };
109     Rule.prototype.getFileHeaderText = function (text, offset, allowSingleLineComments) {
110         var ranges = ts.getLeadingCommentRanges(text, offset);
111         if (ranges === undefined || ranges.length === 0) {
112             return undefined;
113         }
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);
119         })
120             .join("\n");
121     };
122     /* tslint:disable:object-literal-sort-keys */
123     Rule.metadata = {
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),
127         options: {
128             oneOf: [
129                 {
130                     type: "array",
131                     items: {
132                         type: "object",
133                         properties: (_a = {},
134                             _a[OPTION_MATCH] = {
135                                 type: "string",
136                             },
137                             _a[OPTION_ALLOW_SINGLE_LINE_COMMENTS] = {
138                                 type: "boolean",
139                             },
140                             _a[OPTION_DEFAULT] = {
141                                 type: "string",
142                             },
143                             _a[OPTION_ENFORCE_TRAILING_NEWLINE] = {
144                                 type: "boolean",
145                             },
146                             _a),
147                         additionalProperties: false,
148                     },
149                 },
150                 {
151                     type: "array",
152                     items: [
153                         {
154                             type: "string",
155                         },
156                         {
157                             type: "string",
158                         },
159                         {
160                             type: "string",
161                         },
162                     ],
163                     additionalItems: false,
164                     minLength: 1,
165                     maxLength: 3,
166                 },
167             ],
168         },
169         optionExamples: [
170             [
171                 true,
172                 (_b = {},
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,
177                     _b),
178             ],
179             [true, "Copyright \\d{4}", "Copyright 2018", OPTION_ENFORCE_TRAILING_NEWLINE],
180         ],
181         hasFix: true,
182         type: "style",
183         typescriptOnly: false,
184     };
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";
188     return Rule;
189 }(Lint.Rules.AbstractRule));
190 exports.Rule = Rule;
191 var templateObject_1;