4 * Copyright 2017 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 tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.FAILURE_STRING_REDUNDANT_TAG = function (tagName) {
29 return "JSDoc tag '@" + tagName + "' is redundant in TypeScript code.";
31 Rule.FAILURE_STRING_NO_COMMENT = function (tagName) {
32 return "'@" + tagName + "' is redundant in TypeScript code if it has no description.";
34 Rule.prototype.apply = function (sourceFile) {
35 return this.applyWithFunction(sourceFile, walk);
37 /* tslint:disable:object-literal-sort-keys */
39 ruleName: "no-redundant-jsdoc",
40 description: "Forbids JSDoc which duplicates TypeScript functionality.",
41 optionsDescription: "Not configurable.",
43 optionExamples: [true],
47 /* tslint:enable:object-literal-sort-keys */
48 Rule.FAILURE_STRING_REDUNDANT_TYPE = "Type annotation in JSDoc is redundant in TypeScript code.";
50 }(Lint.Rules.AbstractRule));
53 var sourceFile = ctx.sourceFile;
54 // Intentionally exclude EndOfFileToken: it can have JSDoc, but it is only relevant in JavaScript files
55 return sourceFile.statements.forEach(function cb(node) {
56 if (tsutils_1.canHaveJsDoc(node)) {
57 for (var _i = 0, _a = tsutils_1.getJsDoc(node, sourceFile); _i < _a.length; _i++) {
58 var tags = _a[_i].tags;
59 if (tags !== undefined) {
60 for (var _b = 0, tags_1 = tags; _b < tags_1.length; _b++) {
67 return ts.forEachChild(node, cb);
69 function checkTag(tag) {
71 case ts.SyntaxKind.JSDocTag:
72 if (redundantTags.has(tag.tagName.text)) {
73 ctx.addFailureAtNode(tag.tagName, Rule.FAILURE_STRING_REDUNDANT_TAG(tag.tagName.text));
76 case ts.SyntaxKind.JSDocAugmentsTag:
79 case ts.SyntaxKind.JSDocClassTag:
80 case ts.SyntaxKind.JSDocThisTag:
81 case ts.SyntaxKind.JSDocTypeTag:
82 case ts.SyntaxKind.JSDocTypedefTag:
83 case ts.SyntaxKind.JSDocPropertyTag:
85 ctx.addFailureAtNode(tag.tagName, Rule.FAILURE_STRING_REDUNDANT_TAG(tag.tagName.text));
87 case ts.SyntaxKind.JSDocTemplateTag:
88 if (tag.comment === undefined ||
90 ctx.addFailureAtNode(tag.tagName, Rule.FAILURE_STRING_NO_COMMENT(tag.tagName.text));
93 case ts.SyntaxKind.JSDocReturnTag:
94 case ts.SyntaxKind.JSDocParameterTag: {
95 var _a = tag, typeExpression = _a.typeExpression, comment = _a.comment;
96 if (typeExpression !== undefined) {
97 ctx.addFailureAtNode(typeExpression, Rule.FAILURE_STRING_REDUNDANT_TYPE);
99 if (comment === undefined || comment === "") {
100 // Redundant if no documentation
101 ctx.addFailureAtNode(tag.tagName, Rule.FAILURE_STRING_NO_COMMENT(tag.tagName.text));
106 throw new Error("Unexpected tag kind: " + ts.SyntaxKind[tag.kind]);
110 var redundantTags = new Set([