completedDocsRule.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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 ts = require("typescript");
  21. var Lint = require("../index");
  22. var exclusionFactory_1 = require("./completed-docs/exclusionFactory");
  23. exports.ALL = "all";
  24. exports.ARGUMENT_CLASSES = "classes";
  25. exports.ARGUMENT_ENUMS = "enums";
  26. exports.ARGUMENT_ENUM_MEMBERS = "enum-members";
  27. exports.ARGUMENT_FUNCTIONS = "functions";
  28. exports.ARGUMENT_INTERFACES = "interfaces";
  29. exports.ARGUMENT_METHODS = "methods";
  30. exports.ARGUMENT_NAMESPACES = "namespaces";
  31. exports.ARGUMENT_PROPERTIES = "properties";
  32. exports.ARGUMENT_TYPES = "types";
  33. exports.ARGUMENT_VARIABLES = "variables";
  34. exports.DESCRIPTOR_TAGS = "tags";
  35. exports.DESCRIPTOR_LOCATIONS = "locations";
  36. exports.DESCRIPTOR_PRIVACIES = "privacies";
  37. exports.DESCRIPTOR_VISIBILITIES = "visibilities";
  38. exports.LOCATION_INSTANCE = "instance";
  39. exports.LOCATION_STATIC = "static";
  40. exports.PRIVACY_PRIVATE = "private";
  41. exports.PRIVACY_PROTECTED = "protected";
  42. exports.PRIVACY_PUBLIC = "public";
  43. exports.TAGS_FOR_CONTENT = "content";
  44. exports.TAGS_FOR_EXISTENCE = "existence";
  45. exports.VISIBILITY_EXPORTED = "exported";
  46. exports.VISIBILITY_INTERNAL = "internal";
  47. var Rule = /** @class */ (function (_super) {
  48. tslib_1.__extends(Rule, _super);
  49. function Rule() {
  50. var _this = _super !== null && _super.apply(this, arguments) || this;
  51. /* tslint:enable:object-literal-sort-keys */
  52. _this.exclusionFactory = new exclusionFactory_1.ExclusionFactory();
  53. return _this;
  54. }
  55. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  56. var options = this.getOptions();
  57. var exclusionsMap = this.getExclusionsMap(options.ruleArguments);
  58. return this.applyWithFunction(sourceFile, walk, exclusionsMap, program.getTypeChecker());
  59. };
  60. Rule.prototype.getExclusionsMap = function (ruleArguments) {
  61. if (ruleArguments.length === 0) {
  62. ruleArguments = [Rule.defaultArguments];
  63. }
  64. return this.exclusionFactory.constructExclusionsMap(ruleArguments);
  65. };
  66. Rule.FAILURE_STRING_EXIST = "Documentation must exist for ";
  67. Rule.defaultArguments = (_a = {},
  68. _a[exports.ARGUMENT_CLASSES] = true,
  69. _a[exports.ARGUMENT_FUNCTIONS] = true,
  70. _a[exports.ARGUMENT_METHODS] = (_b = {},
  71. _b[exports.DESCRIPTOR_TAGS] = (_c = {},
  72. _c[exports.TAGS_FOR_CONTENT] = {
  73. see: ".*",
  74. },
  75. _c[exports.TAGS_FOR_EXISTENCE] = [
  76. "deprecated",
  77. "inheritdoc",
  78. ],
  79. _c),
  80. _b),
  81. _a[exports.ARGUMENT_PROPERTIES] = (_d = {},
  82. _d[exports.DESCRIPTOR_TAGS] = (_e = {},
  83. _e[exports.TAGS_FOR_CONTENT] = {
  84. see: ".*",
  85. },
  86. _e[exports.TAGS_FOR_EXISTENCE] = [
  87. "deprecated",
  88. "inheritdoc",
  89. ],
  90. _e),
  91. _d),
  92. _a);
  93. Rule.ARGUMENT_DESCRIPTOR_BLOCK = {
  94. properties: (_f = {},
  95. _f[exports.DESCRIPTOR_TAGS] = {
  96. properties: (_g = {},
  97. _g[exports.TAGS_FOR_CONTENT] = {
  98. items: {
  99. type: "string",
  100. },
  101. type: "object",
  102. },
  103. _g[exports.TAGS_FOR_EXISTENCE] = {
  104. items: {
  105. type: "string",
  106. },
  107. type: "array",
  108. },
  109. _g),
  110. },
  111. _f[exports.DESCRIPTOR_VISIBILITIES] = {
  112. enum: [
  113. exports.ALL,
  114. exports.VISIBILITY_EXPORTED,
  115. exports.VISIBILITY_INTERNAL,
  116. ],
  117. type: "string",
  118. },
  119. _f),
  120. type: "object",
  121. };
  122. Rule.ARGUMENT_DESCRIPTOR_CLASS = {
  123. properties: (_h = {},
  124. _h[exports.DESCRIPTOR_TAGS] = {
  125. properties: (_j = {},
  126. _j[exports.TAGS_FOR_CONTENT] = {
  127. items: {
  128. type: "string",
  129. },
  130. type: "object",
  131. },
  132. _j[exports.TAGS_FOR_EXISTENCE] = {
  133. items: {
  134. type: "string",
  135. },
  136. type: "array",
  137. },
  138. _j),
  139. },
  140. _h[exports.DESCRIPTOR_LOCATIONS] = {
  141. enum: [
  142. exports.ALL,
  143. exports.LOCATION_INSTANCE,
  144. exports.LOCATION_STATIC,
  145. ],
  146. type: "string",
  147. },
  148. _h[exports.DESCRIPTOR_PRIVACIES] = {
  149. enum: [
  150. exports.ALL,
  151. exports.PRIVACY_PRIVATE,
  152. exports.PRIVACY_PROTECTED,
  153. exports.PRIVACY_PUBLIC,
  154. ],
  155. type: "string",
  156. },
  157. _h),
  158. type: "object",
  159. };
  160. /* tslint:disable:object-literal-sort-keys */
  161. Rule.metadata = {
  162. ruleName: "completed-docs",
  163. description: "Enforces JSDoc comments for important items be filled out.",
  164. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\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 * `\"", "\"`"], ["\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 * \\`\"", "\"\\`"])), 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),
  165. options: {
  166. type: "array",
  167. items: {
  168. anyOf: [
  169. {
  170. options: [
  171. exports.ARGUMENT_CLASSES,
  172. exports.ARGUMENT_ENUMS,
  173. exports.ARGUMENT_FUNCTIONS,
  174. exports.ARGUMENT_INTERFACES,
  175. exports.ARGUMENT_METHODS,
  176. exports.ARGUMENT_NAMESPACES,
  177. exports.ARGUMENT_PROPERTIES,
  178. exports.ARGUMENT_TYPES,
  179. exports.ARGUMENT_VARIABLES,
  180. ],
  181. type: "string",
  182. },
  183. {
  184. type: "object",
  185. properties: (_k = {},
  186. _k[exports.ARGUMENT_CLASSES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  187. _k[exports.ARGUMENT_ENUMS] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  188. _k[exports.ARGUMENT_ENUM_MEMBERS] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  189. _k[exports.ARGUMENT_FUNCTIONS] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  190. _k[exports.ARGUMENT_INTERFACES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  191. _k[exports.ARGUMENT_METHODS] = Rule.ARGUMENT_DESCRIPTOR_CLASS,
  192. _k[exports.ARGUMENT_NAMESPACES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  193. _k[exports.ARGUMENT_PROPERTIES] = Rule.ARGUMENT_DESCRIPTOR_CLASS,
  194. _k[exports.ARGUMENT_TYPES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  195. _k[exports.ARGUMENT_VARIABLES] = Rule.ARGUMENT_DESCRIPTOR_BLOCK,
  196. _k),
  197. },
  198. ],
  199. },
  200. },
  201. optionExamples: [
  202. true,
  203. [true, exports.ARGUMENT_ENUMS, exports.ARGUMENT_FUNCTIONS, exports.ARGUMENT_METHODS],
  204. [
  205. true,
  206. (_l = {},
  207. _l[exports.ARGUMENT_ENUMS] = true,
  208. _l[exports.ARGUMENT_FUNCTIONS] = (_m = {},
  209. _m[exports.DESCRIPTOR_VISIBILITIES] = [exports.VISIBILITY_EXPORTED],
  210. _m),
  211. _l[exports.ARGUMENT_METHODS] = (_o = {},
  212. _o[exports.DESCRIPTOR_LOCATIONS] = exports.LOCATION_INSTANCE,
  213. _o[exports.DESCRIPTOR_PRIVACIES] = [exports.PRIVACY_PUBLIC, exports.PRIVACY_PROTECTED],
  214. _o),
  215. _l[exports.ARGUMENT_PROPERTIES] = (_p = {},
  216. _p[exports.DESCRIPTOR_TAGS] = (_q = {},
  217. _q[exports.TAGS_FOR_CONTENT] = {
  218. see: ["#.*"],
  219. },
  220. _q[exports.TAGS_FOR_EXISTENCE] = ["inheritdoc"],
  221. _q),
  222. _p),
  223. _l),
  224. ],
  225. ],
  226. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Helps ensure important components are documented.\n\n Note: use this rule sparingly. It's better to have self-documenting names on components with single, consice responsibilities.\n Comments that only restate the names of variables add nothing to code, and can easily become outdated.\n "], ["\n Helps ensure important components are documented.\n\n Note: use this rule sparingly. It's better to have self-documenting names on components with single, consice responsibilities.\n Comments that only restate the names of variables add nothing to code, and can easily become outdated.\n "]))),
  227. type: "style",
  228. typescriptOnly: false,
  229. requiresTypeInfo: true,
  230. };
  231. return Rule;
  232. }(Lint.Rules.TypedRule));
  233. exports.Rule = Rule;
  234. var modifierAliases = {
  235. export: "exported",
  236. };
  237. function walk(context, typeChecker) {
  238. return ts.forEachChild(context.sourceFile, cb);
  239. function cb(node) {
  240. switch (node.kind) {
  241. case ts.SyntaxKind.ClassDeclaration:
  242. checkNode(node, exports.ARGUMENT_CLASSES);
  243. break;
  244. case ts.SyntaxKind.EnumDeclaration:
  245. checkNode(node, exports.ARGUMENT_ENUMS);
  246. for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
  247. var member = _a[_i];
  248. // Enum members don't have modifiers, so use the parent
  249. // enum declaration when checking the requirements.
  250. checkNode(member, exports.ARGUMENT_ENUM_MEMBERS, node);
  251. }
  252. break;
  253. case ts.SyntaxKind.FunctionDeclaration:
  254. checkNode(node, exports.ARGUMENT_FUNCTIONS);
  255. break;
  256. case ts.SyntaxKind.InterfaceDeclaration:
  257. checkNode(node, exports.ARGUMENT_INTERFACES);
  258. break;
  259. case ts.SyntaxKind.MethodDeclaration:
  260. if (node.parent.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
  261. checkNode(node, exports.ARGUMENT_METHODS);
  262. }
  263. break;
  264. case ts.SyntaxKind.ModuleDeclaration:
  265. checkNode(node, exports.ARGUMENT_NAMESPACES);
  266. break;
  267. case ts.SyntaxKind.PropertyDeclaration:
  268. checkNode(node, exports.ARGUMENT_PROPERTIES);
  269. break;
  270. case ts.SyntaxKind.TypeAliasDeclaration:
  271. checkNode(node, exports.ARGUMENT_TYPES);
  272. break;
  273. case ts.SyntaxKind.VariableStatement:
  274. // Only check variables at the namespace/module-level or file-level
  275. // and not variables declared inside functions and other things.
  276. switch (node.parent.kind) {
  277. case ts.SyntaxKind.SourceFile:
  278. case ts.SyntaxKind.ModuleBlock:
  279. for (var _b = 0, _c = node.declarationList.declarations; _b < _c.length; _b++) {
  280. var declaration = _c[_b];
  281. checkNode(declaration, exports.ARGUMENT_VARIABLES, node);
  282. }
  283. }
  284. break;
  285. case ts.SyntaxKind.GetAccessor:
  286. case ts.SyntaxKind.SetAccessor:
  287. if (node.parent.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
  288. checkNode(node, exports.ARGUMENT_PROPERTIES);
  289. }
  290. }
  291. return ts.forEachChild(node, cb);
  292. }
  293. function checkNode(node, nodeType, requirementNode) {
  294. if (requirementNode === void 0) { requirementNode = node; }
  295. var name = node.name;
  296. if (name === undefined) {
  297. return;
  298. }
  299. var exclusions = context.options.get(nodeType);
  300. if (exclusions === undefined) {
  301. return;
  302. }
  303. for (var _i = 0, exclusions_1 = exclusions; _i < exclusions_1.length; _i++) {
  304. var exclusion = exclusions_1[_i];
  305. if (exclusion.excludes(requirementNode)) {
  306. return;
  307. }
  308. }
  309. var symbol = typeChecker.getSymbolAtLocation(name);
  310. if (symbol === undefined) {
  311. return;
  312. }
  313. var comments = symbol.getDocumentationComment(typeChecker);
  314. checkComments(node, describeNode(nodeType), comments, requirementNode);
  315. }
  316. function checkComments(node, nodeDescriptor, comments, requirementNode) {
  317. if (comments.map(function (comment) { return comment.text; }).join("").trim() === "") {
  318. addDocumentationFailure(node, nodeDescriptor, requirementNode);
  319. }
  320. }
  321. function addDocumentationFailure(node, nodeType, requirementNode) {
  322. var start = node.getStart();
  323. var width = node.getText().split(/\r|\n/g)[0].length;
  324. var description = describeDocumentationFailure(requirementNode, nodeType);
  325. context.addFailureAt(start, width, description);
  326. }
  327. }
  328. function describeDocumentationFailure(node, nodeType) {
  329. var description = Rule.FAILURE_STRING_EXIST;
  330. if (node.modifiers !== undefined) {
  331. description += node.modifiers.map(function (modifier) { return describeModifier(modifier.kind); }).join(" ") + " ";
  332. }
  333. return "" + description + nodeType + ".";
  334. }
  335. function describeModifier(kind) {
  336. var description = ts.SyntaxKind[kind].toLowerCase().split("keyword")[0];
  337. var alias = modifierAliases[description];
  338. return alias !== undefined ? alias : description;
  339. }
  340. function describeNode(nodeType) {
  341. return nodeType.replace("-", " ");
  342. }
  343. var templateObject_1, templateObject_2;
  344. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;