a zip code crypto-currency system good for red ONLY

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