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");
20 var utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 function parseOptions(opts) {
27 var MULTILINE_FAILURE = "multiline comments are not allowed";
28 var SINGLE_LINE_FAILURE = "singleline comments are not allowed";
29 var DOC_FAILURE = "doc comments are not allowed";
30 var DIRECTIVE_FAILURE = "triple-slash directives are not allowed";
32 var Rule = /** @class */ (function (_super) {
33 tslib_1.__extends(Rule, _super);
35 return _super !== null && _super.apply(this, arguments) || this;
37 Rule.prototype.apply = function (sourceFile) {
38 return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
40 // tslint:disable:object-literal-sort-keys
42 ruleName: "comment-type",
43 description: "Allows a limited set of comment types",
44 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n One or more of the following mutually exclusive comment types may be provided:\n\n * `singleline`: Comments starting with `//`\n * `multiline`: Comments between `/*` and `*/` but are not doc comments\n * `doc`: Multiline comments that start with `/**`\n * 'directive': Triple-slash directives that are singleline comments starting with `///`"], ["\n One or more of the following mutually exclusive comment types may be provided:\n\n * \\`singleline\\`: Comments starting with \\`//\\`\n * \\`multiline\\`: Comments between \\`/*\\` and \\`*/\\` but are not doc comments\n * \\`doc\\`: Multiline comments that start with \\`/**\\`\n * \\'directive\\': Triple-slash directives that are singleline comments starting with \\`///\\`"]))),
49 enum: ["singleline", "multiline", "doc", "directive"],
53 optionExamples: [[true, "doc", "singleline"], [true, "singleline"], [true, "multiline"]],
56 typescriptOnly: false,
59 }(Lint.Rules.AbstractRule));
62 utils.forEachComment(ctx.sourceFile, function (fullText, _a) {
63 var kind = _a.kind, pos = _a.pos, end = _a.end;
64 if (kind === ts.SyntaxKind.SingleLineCommentTrivia) {
66 if (fullText.slice(pos, pos + 3) === "///" && !ctx.options.has("directive")) {
67 ctx.addFailure(pos, end, DIRECTIVE_FAILURE);
70 else if (fullText.slice(pos, pos + 3) !== "///" && !ctx.options.has("singleline")) {
71 ctx.addFailure(pos, end, SINGLE_LINE_FAILURE);
74 else if (kind === ts.SyntaxKind.MultiLineCommentTrivia) {
76 if (fullText.slice(pos, pos + 3) === "/**" && !ctx.options.has("doc")) {
77 ctx.addFailure(pos, end, DOC_FAILURE);
80 else if (fullText.slice(pos, pos + 3) !== "/**" && !ctx.options.has("multiline")) {
81 ctx.addFailure(pos, end, MULTILINE_FAILURE);