completedDocsRule.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 documentation 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. type: "style",
  227. typescriptOnly: false,
  228. requiresTypeInfo: true,
  229. };
  230. return Rule;
  231. }(Lint.Rules.TypedRule));
  232. exports.Rule = Rule;
  233. var modifierAliases = {
  234. export: "exported",
  235. };
  236. function walk(context, typeChecker) {
  237. return ts.forEachChild(context.sourceFile, cb);
  238. function cb(node) {
  239. switch (node.kind) {
  240. case ts.SyntaxKind.ClassDeclaration:
  241. checkNode(node, exports.ARGUMENT_CLASSES);
  242. break;
  243. case ts.SyntaxKind.EnumDeclaration:
  244. checkNode(node, exports.ARGUMENT_ENUMS);
  245. for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
  246. var member = _a[_i];
  247. // Enum members don't have modifiers, so use the parent
  248. // enum declaration when checking the requirements.
  249. checkNode(member, exports.ARGUMENT_ENUM_MEMBERS, node);
  250. }
  251. break;
  252. case ts.SyntaxKind.FunctionDeclaration:
  253. checkNode(node, exports.ARGUMENT_FUNCTIONS);
  254. break;
  255. case ts.SyntaxKind.InterfaceDeclaration:
  256. checkNode(node, exports.ARGUMENT_INTERFACES);
  257. break;
  258. case ts.SyntaxKind.MethodDeclaration:
  259. if (node.parent.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
  260. checkNode(node, exports.ARGUMENT_METHODS);
  261. }
  262. break;
  263. case ts.SyntaxKind.ModuleDeclaration:
  264. checkNode(node, exports.ARGUMENT_NAMESPACES);
  265. break;
  266. case ts.SyntaxKind.PropertyDeclaration:
  267. checkNode(node, exports.ARGUMENT_PROPERTIES);
  268. break;
  269. case ts.SyntaxKind.TypeAliasDeclaration:
  270. checkNode(node, exports.ARGUMENT_TYPES);
  271. break;
  272. case ts.SyntaxKind.VariableStatement:
  273. // Only check variables at the namespace/module-level or file-level
  274. // and not variables declared inside functions and other things.
  275. switch (node.parent.kind) {
  276. case ts.SyntaxKind.SourceFile:
  277. case ts.SyntaxKind.ModuleBlock:
  278. for (var _b = 0, _c = node.declarationList.declarations; _b < _c.length; _b++) {
  279. var declaration = _c[_b];
  280. checkNode(declaration, exports.ARGUMENT_VARIABLES, node);
  281. }
  282. }
  283. break;
  284. case ts.SyntaxKind.GetAccessor:
  285. case ts.SyntaxKind.SetAccessor:
  286. if (node.parent.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
  287. checkNode(node, exports.ARGUMENT_PROPERTIES);
  288. }
  289. }
  290. return ts.forEachChild(node, cb);
  291. }
  292. function checkNode(node, nodeType, requirementNode) {
  293. if (requirementNode === void 0) { requirementNode = node; }
  294. var name = node.name;
  295. if (name === undefined) {
  296. return;
  297. }
  298. var exclusions = context.options.get(nodeType);
  299. if (exclusions === undefined) {
  300. return;
  301. }
  302. for (var _i = 0, exclusions_1 = exclusions; _i < exclusions_1.length; _i++) {
  303. var exclusion = exclusions_1[_i];
  304. if (exclusion.excludes(requirementNode)) {
  305. return;
  306. }
  307. }
  308. var symbol = typeChecker.getSymbolAtLocation(name);
  309. if (symbol === undefined) {
  310. return;
  311. }
  312. var comments = symbol.getDocumentationComment();
  313. checkComments(node, describeNode(nodeType), comments, requirementNode);
  314. }
  315. function checkComments(node, nodeDescriptor, comments, requirementNode) {
  316. if (comments.map(function (comment) { return comment.text; }).join("").trim() === "") {
  317. addDocumentationFailure(node, nodeDescriptor, requirementNode);
  318. }
  319. }
  320. function addDocumentationFailure(node, nodeType, requirementNode) {
  321. var start = node.getStart();
  322. var width = node.getText().split(/\r|\n/g)[0].length;
  323. var description = describeDocumentationFailure(requirementNode, nodeType);
  324. context.addFailureAt(start, width, description);
  325. }
  326. }
  327. function describeDocumentationFailure(node, nodeType) {
  328. var description = Rule.FAILURE_STRING_EXIST;
  329. if (node.modifiers !== undefined) {
  330. description += node.modifiers.map(function (modifier) { return describeModifier(modifier.kind); }).join(" ") + " ";
  331. }
  332. return "" + description + nodeType + ".";
  333. }
  334. function describeModifier(kind) {
  335. var description = ts.SyntaxKind[kind].toLowerCase().split("keyword")[0];
  336. var alias = modifierAliases[description];
  337. return alias !== undefined ? alias : description;
  338. }
  339. function describeNode(nodeType) {
  340. return nodeType.replace("-", " ");
  341. }
  342. var templateObject_1;
  343. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;