wallet-core/node_modules/tslint/lib/rules/completedDocsRule.js

358 lines
19 KiB
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
"use strict";
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
2017-08-14 05:01:11 +02:00
var tsutils_1 = require("tsutils");
2017-05-28 00:38:50 +02:00
var ts = require("typescript");
var Lint = require("../index");
2017-12-10 21:51:33 +01:00
var exclusionFactory_1 = require("./completed-docs/exclusionFactory");
2017-05-28 00:38:50 +02:00
exports.ALL = "all";
exports.ARGUMENT_CLASSES = "classes";
exports.ARGUMENT_ENUMS = "enums";
2017-08-14 05:01:11 +02:00
exports.ARGUMENT_ENUM_MEMBERS = "enum-members";
2017-05-28 00:38:50 +02:00
exports.ARGUMENT_FUNCTIONS = "functions";
exports.ARGUMENT_INTERFACES = "interfaces";
exports.ARGUMENT_METHODS = "methods";
exports.ARGUMENT_NAMESPACES = "namespaces";
exports.ARGUMENT_PROPERTIES = "properties";
exports.ARGUMENT_TYPES = "types";
exports.ARGUMENT_VARIABLES = "variables";
2017-12-10 21:51:33 +01:00
exports.DESCRIPTOR_TAGS = "tags";
2017-05-28 00:38:50 +02:00
exports.DESCRIPTOR_LOCATIONS = "locations";
exports.DESCRIPTOR_PRIVACIES = "privacies";
exports.DESCRIPTOR_VISIBILITIES = "visibilities";
exports.LOCATION_INSTANCE = "instance";
exports.LOCATION_STATIC = "static";
exports.PRIVACY_PRIVATE = "private";
exports.PRIVACY_PROTECTED = "protected";
exports.PRIVACY_PUBLIC = "public";
2017-12-10 21:51:33 +01:00
exports.TAGS_FOR_CONTENT = "content";
exports.TAGS_FOR_EXISTENCE = "exists";
2017-05-28 00:38:50 +02:00
exports.VISIBILITY_EXPORTED = "exported";
exports.VISIBILITY_INTERNAL = "internal";
2017-10-14 18:40:54 +02:00
var Rule = /** @class */ (function (_super) {
2017-05-28 00:38:50 +02:00
tslib_1.__extends(Rule, _super);
function Rule() {
2017-12-10 21:51:33 +01:00
var _this = _super !== null && _super.apply(this, arguments) || this;
/* tslint:enable:object-literal-sort-keys */
_this.exclusionFactory = new exclusionFactory_1.ExclusionFactory();
return _this;
2017-05-28 00:38:50 +02:00
}
Rule.prototype.applyWithProgram = function (sourceFile, program) {
var options = this.getOptions();
var completedDocsWalker = new CompletedDocsWalker(sourceFile, options, program);
2017-12-10 21:51:33 +01:00
completedDocsWalker.setExclusionsMap(this.getExclusionsMap(options.ruleArguments));
2017-05-28 00:38:50 +02:00
return this.applyWithWalker(completedDocsWalker);
};
2017-12-10 21:51:33 +01:00
Rule.prototype.getExclusionsMap = function (ruleArguments) {
2017-05-28 00:38:50 +02:00
if (ruleArguments.length === 0) {
2017-12-10 21:51:33 +01:00
ruleArguments = [Rule.defaultArguments];
2017-05-28 00:38:50 +02:00
}
2017-12-10 21:51:33 +01:00
return this.exclusionFactory.constructExclusionsMap(ruleArguments);
2017-05-28 00:38:50 +02:00
};
2017-08-14 05:01:11 +02:00
Rule.FAILURE_STRING_EXIST = "Documentation must exist for ";
2017-12-10 21:51:33 +01:00
Rule.defaultArguments = (_a = {},
_a[exports.ARGUMENT_CLASSES] = true,
_a[exports.ARGUMENT_FUNCTIONS] = true,
_a[exports.ARGUMENT_METHODS] = (_b = {},
_b[exports.DESCRIPTOR_TAGS] = (_c = {},
_c[exports.TAGS_FOR_CONTENT] = {
see: ".*",
},
_c[exports.TAGS_FOR_EXISTENCE] = [
"deprecated",
"inheritdoc",
],
_c),
_b),
_a[exports.ARGUMENT_PROPERTIES] = (_d = {},
_d[exports.DESCRIPTOR_TAGS] = (_e = {},
_e[exports.TAGS_FOR_CONTENT] = {
see: ".*",
},
_e[exports.TAGS_FOR_EXISTENCE] = [
"deprecated",
"inheritdoc",
],
_e),
_d),
_a);
2017-08-14 05:01:11 +02:00
Rule.ARGUMENT_DESCRIPTOR_BLOCK = {
2017-12-10 21:51:33 +01:00
properties: (_f = {},
_f[exports.DESCRIPTOR_TAGS] = {
properties: (_g = {},
_g[exports.TAGS_FOR_CONTENT] = {
items: {
type: "string",
},
type: "object",
},
_g[exports.TAGS_FOR_EXISTENCE] = {
items: {
type: "string",
},
type: "array",
},
_g),
},
_f[exports.DESCRIPTOR_VISIBILITIES] = {
2017-08-14 05:01:11 +02:00
enum: [
exports.ALL,
exports.VISIBILITY_EXPORTED,
exports.VISIBILITY_INTERNAL,
],
type: "string",
},
2017-12-10 21:51:33 +01:00
_f),
2017-08-14 05:01:11 +02:00
type: "object",
};
Rule.ARGUMENT_DESCRIPTOR_CLASS = {
2017-12-10 21:51:33 +01:00
properties: (_h = {},
_h[exports.DESCRIPTOR_TAGS] = {
properties: (_j = {},
_j[exports.TAGS_FOR_CONTENT] = {
items: {
type: "string",
},
type: "object",
},
_j[exports.TAGS_FOR_EXISTENCE] = {
items: {
type: "string",
},
type: "array",
},
_j),
},
_h[exports.DESCRIPTOR_LOCATIONS] = {
2017-08-14 05:01:11 +02:00
enum: [
exports.ALL,
exports.LOCATION_INSTANCE,
exports.LOCATION_STATIC,
],
type: "string",
},
2017-12-10 21:51:33 +01:00
_h[exports.DESCRIPTOR_PRIVACIES] = {
2017-08-14 05:01:11 +02:00
enum: [
exports.ALL,
exports.PRIVACY_PRIVATE,
exports.PRIVACY_PROTECTED,
exports.PRIVACY_PUBLIC,
],
type: "string",
},
2017-12-10 21:51:33 +01:00
_h),
2017-08-14 05:01:11 +02:00
type: "object",
};
/* tslint:disable:object-literal-sort-keys */
Rule.metadata = {
ruleName: "completed-docs",
description: "Enforces documentation for important items be filled out.",
2017-12-10 21:51:33 +01:00
optionsDescription: (_k = ["\n `true` to enable for [", "]],\n or an array with each item in one of two formats:\n\n * `string` to enable for that type\n * `object` keying types to when their documentation is required:\n * `\"", "\"` and `\"", "\"` may specify:\n * `\"", "\"`:\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`:\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * Other types may specify `\"", "\"`:\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * All types may also provide `\"", "\"`\n with members specifying tags that allow the docs to not have a body.\n * `\"", "\"`: Object mapping tags to `RegExp` bodies content allowed to count as complete docs.\n * `\"", "\"`: Array of tags that must only exist to count as complete docs.\n\n Types that may be enabled are:\n\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`\n * `\"", "\"`"], _k.raw = ["\n \\`true\\` to enable for [", "]],\n or an array with each item in one of two formats:\n\n * \\`string\\` to enable for that type\n * \\`object\\` keying types to when their documentation is required:\n * \\`\"", "\"\\` and \\`\"", "\"\\` may specify:\n * \\`\"", "\"\\`:\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`:\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * Other types may specify \\`\"", "\"\\`:\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * All types may also provide \\`\"", "\"\\`\n with members specifying tags that allow the docs to not have a body.\n * \\`\"", "\"\\`: Object mapping tags to \\`RegExp\\` bodies content allowed to count as complete docs.\n * \\`\"", "\"\\`: Array of tags that must only exist to count as complete docs.\n\n Types that may be enabled are:\n\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`\n * \\`\"", "\"\\`"], Lint.Utils.dedent(_k, Object.keys(Rule.defaultArguments).join(", "), exports.ARGUMENT_METHODS, exports.ARGUMENT_PROPERTIES, exports.DESCRIPTOR_PRIVACIES, exports.ALL, exports.PRIVACY_PRIVATE, exports.PRIVACY_PROTECTED, exports.PRIVACY_PUBLIC, exports.DESCRIPTOR_LOCATIONS, exports.ALL, exports.LOCATION_INSTANCE, exports.LOCATION_STATIC, exports.DESCRIPTOR_VISIBILITIES, exports.ALL, exports.VISIBILITY_EXPORTED, exports.VISIBILITY_INTERNAL, exports.DESCRIPTOR_TAGS, exports.TAGS_FOR_CONTENT, exports.TAGS_FOR_EXISTENCE, exports.ARGUMENT_CLASSES, exports.ARGUMENT_ENUMS, exports.ARGUMENT_ENUM_MEMBERS, exports.ARGUMENT_FUNCTIONS, exports.ARGUMENT_INTERFACES, exports.ARGUMENT_METHODS, exports.ARGUMENT_NAMESPACES, exports.ARGUMENT_PROPERTIES, exports.ARGUMENT_TYPES, exports.ARGUMENT_VARIABLES)),
2017-08-14 05:01:11 +02:00
options: {
type: "array",
items: {
anyOf: [
{
2017-12-10 21:51:33 +01:00
options: [
exports.ARGUMENT_CLASSES,
exports.ARGUMENT_ENUMS,
exports.ARGUMENT_FUNCTIONS,
exports.ARGUMENT_INTERFACES,
exports.ARGUMENT_METHODS,
exports.ARGUMENT_NAMESPACES,
exports.ARGUMENT_PROPERTIES,
exports.ARGUMENT_TYPES,
exports.ARGUMENT_VARIABLES,
],
2017-08-14 05:01:11 +02:00
type: "string",
},
{
type: "object",
2017-12-10 21:51:33 +01:00
properties: (_l = {},
_l[exports.ARGUMENT_CLASSES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l[exports.ARGUMENT_ENUMS] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l[exports.ARGUMENT_ENUM_MEMBERS] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l[exports.ARGUMENT_FUNCTIONS] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l[exports.ARGUMENT_INTERFACES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l[exports.ARGUMENT_METHODS] = Rule.ARGUMENT_DESCRIPTOR_CLASS,
_l[exports.ARGUMENT_NAMESPACES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l[exports.ARGUMENT_PROPERTIES] = Rule.ARGUMENT_DESCRIPTOR_CLASS,
_l[exports.ARGUMENT_TYPES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l[exports.ARGUMENT_VARIABLES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
_l),
2017-08-14 05:01:11 +02:00
},
],
},
2017-05-28 00:38:50 +02:00
},
2017-08-14 05:01:11 +02:00
optionExamples: [
2017-05-28 00:38:50 +02:00
true,
2017-08-14 05:01:11 +02:00
[true, exports.ARGUMENT_ENUMS, exports.ARGUMENT_FUNCTIONS, exports.ARGUMENT_METHODS],
[
true,
2017-12-10 21:51:33 +01:00
(_m = {},
_m[exports.ARGUMENT_ENUMS] = true,
_m[exports.ARGUMENT_FUNCTIONS] = (_o = {},
_o[exports.DESCRIPTOR_VISIBILITIES] = [exports.VISIBILITY_EXPORTED],
_o),
_m[exports.ARGUMENT_METHODS] = (_p = {},
_p[exports.DESCRIPTOR_LOCATIONS] = exports.LOCATION_INSTANCE,
_p[exports.DESCRIPTOR_PRIVACIES] = [exports.PRIVACY_PUBLIC, exports.PRIVACY_PROTECTED],
_p),
_m[exports.DESCRIPTOR_TAGS] = (_q = {},
_q[exports.TAGS_FOR_CONTENT] = {
see: ["#.*"],
},
_q[exports.TAGS_FOR_EXISTENCE] = ["inheritdoc"],
_q),
_m),
2017-08-14 05:01:11 +02:00
],
2017-05-28 00:38:50 +02:00
],
2017-08-14 05:01:11 +02:00
type: "style",
typescriptOnly: false,
2017-12-10 21:51:33 +01:00
requiresTypeInfo: true,
2017-08-14 05:01:11 +02:00
};
return Rule;
}(Lint.Rules.TypedRule));
2017-05-28 00:38:50 +02:00
exports.Rule = Rule;
2017-10-14 18:40:54 +02:00
var CompletedDocsWalker = /** @class */ (function (_super) {
2017-05-28 00:38:50 +02:00
tslib_1.__extends(CompletedDocsWalker, _super);
function CompletedDocsWalker() {
return _super !== null && _super.apply(this, arguments) || this;
}
2017-12-10 21:51:33 +01:00
CompletedDocsWalker.prototype.setExclusionsMap = function (exclusionsMap) {
this.exclusionsMap = exclusionsMap;
2017-05-28 00:38:50 +02:00
};
CompletedDocsWalker.prototype.visitClassDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_CLASSES);
_super.prototype.visitClassDeclaration.call(this, node);
};
CompletedDocsWalker.prototype.visitEnumDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_ENUMS);
_super.prototype.visitEnumDeclaration.call(this, node);
};
2017-08-14 05:01:11 +02:00
CompletedDocsWalker.prototype.visitEnumMember = function (node) {
// Enum members don't have modifiers, so use the parent
// enum declaration when checking the requirements.
this.checkNode(node, exports.ARGUMENT_ENUM_MEMBERS, node.parent);
_super.prototype.visitEnumMember.call(this, node);
};
2017-05-28 00:38:50 +02:00
CompletedDocsWalker.prototype.visitFunctionDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_FUNCTIONS);
_super.prototype.visitFunctionDeclaration.call(this, node);
};
CompletedDocsWalker.prototype.visitInterfaceDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_INTERFACES);
_super.prototype.visitInterfaceDeclaration.call(this, node);
};
CompletedDocsWalker.prototype.visitMethodDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_METHODS);
_super.prototype.visitMethodDeclaration.call(this, node);
};
CompletedDocsWalker.prototype.visitModuleDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_NAMESPACES);
_super.prototype.visitModuleDeclaration.call(this, node);
};
CompletedDocsWalker.prototype.visitPropertyDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_PROPERTIES);
_super.prototype.visitPropertyDeclaration.call(this, node);
};
CompletedDocsWalker.prototype.visitTypeAliasDeclaration = function (node) {
this.checkNode(node, exports.ARGUMENT_TYPES);
_super.prototype.visitTypeAliasDeclaration.call(this, node);
};
CompletedDocsWalker.prototype.visitVariableDeclaration = function (node) {
2017-08-14 05:01:11 +02:00
this.checkVariable(node);
2017-05-28 00:38:50 +02:00
_super.prototype.visitVariableDeclaration.call(this, node);
};
2017-12-10 21:51:33 +01:00
CompletedDocsWalker.prototype.checkNode = function (node, nodeType, requirementNode) {
if (requirementNode === void 0) { requirementNode = node; }
var name = node.name;
if (name === undefined) {
return;
}
var exclusions = this.exclusionsMap.get(nodeType);
if (exclusions === undefined) {
return;
}
for (var _i = 0, exclusions_1 = exclusions; _i < exclusions_1.length; _i++) {
var exclusion = exclusions_1[_i];
if (exclusion.excludes(requirementNode)) {
return;
}
}
var symbol = this.getTypeChecker().getSymbolAtLocation(name);
if (symbol === undefined) {
return;
}
var comments = symbol.getDocumentationComment();
this.checkComments(node, this.describeNode(nodeType), comments, requirementNode);
};
2017-08-14 05:01:11 +02:00
CompletedDocsWalker.prototype.checkVariable = function (node) {
// Only check variables in variable declaration lists
// and not variables in catch clauses and for loops.
var list = node.parent;
if (!tsutils_1.isVariableDeclarationList(list)) {
return;
}
var statement = list.parent;
if (!tsutils_1.isVariableStatement(statement)) {
return;
}
// Only check variables at the namespace/module-level or file-level
// and not variables declared inside functions and other things.
switch (statement.parent.kind) {
case ts.SyntaxKind.SourceFile:
case ts.SyntaxKind.ModuleBlock:
this.checkNode(node, exports.ARGUMENT_VARIABLES, statement);
}
};
CompletedDocsWalker.prototype.checkComments = function (node, nodeDescriptor, comments, requirementNode) {
2017-05-28 00:38:50 +02:00
if (comments.map(function (comment) { return comment.text; }).join("").trim() === "") {
2017-08-14 05:01:11 +02:00
this.addDocumentationFailure(node, nodeDescriptor, requirementNode);
2017-05-28 00:38:50 +02:00
}
};
2017-08-14 05:01:11 +02:00
CompletedDocsWalker.prototype.addDocumentationFailure = function (node, nodeType, requirementNode) {
2017-05-28 00:38:50 +02:00
var start = node.getStart();
var width = node.getText().split(/\r|\n/g)[0].length;
2017-08-14 05:01:11 +02:00
var description = this.describeDocumentationFailure(requirementNode, nodeType);
2017-05-28 00:38:50 +02:00
this.addFailureAt(start, width, description);
};
CompletedDocsWalker.prototype.describeDocumentationFailure = function (node, nodeType) {
var _this = this;
var description = Rule.FAILURE_STRING_EXIST;
if (node.modifiers !== undefined) {
description += node.modifiers.map(function (modifier) { return _this.describeModifier(modifier.kind); }).join(",") + " ";
}
return "" + description + nodeType + ".";
};
CompletedDocsWalker.prototype.describeModifier = function (kind) {
var description = ts.SyntaxKind[kind].toLowerCase().split("keyword")[0];
var alias = CompletedDocsWalker.modifierAliases[description];
return alias !== undefined ? alias : description;
};
2017-12-10 21:51:33 +01:00
CompletedDocsWalker.prototype.describeNode = function (nodeType) {
return nodeType.replace("-", " ");
};
2017-08-14 05:01:11 +02:00
CompletedDocsWalker.modifierAliases = {
export: "exported",
};
2017-05-28 00:38:50 +02:00
return CompletedDocsWalker;
}(Lint.ProgramAwareRuleWalker));
2017-12-10 21:51:33 +01:00
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;