Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

valid-jsdoc.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * @fileoverview Validates JSDoc comments are syntactically correct
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const doctrine = require("doctrine");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "suggestion",
  16. docs: {
  17. description: "enforce valid JSDoc comments",
  18. category: "Possible Errors",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/valid-jsdoc"
  21. },
  22. schema: [
  23. {
  24. type: "object",
  25. properties: {
  26. prefer: {
  27. type: "object",
  28. additionalProperties: {
  29. type: "string"
  30. }
  31. },
  32. preferType: {
  33. type: "object",
  34. additionalProperties: {
  35. type: "string"
  36. }
  37. },
  38. requireReturn: {
  39. type: "boolean"
  40. },
  41. requireParamDescription: {
  42. type: "boolean"
  43. },
  44. requireReturnDescription: {
  45. type: "boolean"
  46. },
  47. matchDescription: {
  48. type: "string"
  49. },
  50. requireReturnType: {
  51. type: "boolean"
  52. },
  53. requireParamType: {
  54. type: "boolean"
  55. }
  56. },
  57. additionalProperties: false
  58. }
  59. ],
  60. fixable: "code"
  61. },
  62. create(context) {
  63. const options = context.options[0] || {},
  64. prefer = options.prefer || {},
  65. sourceCode = context.getSourceCode(),
  66. // these both default to true, so you have to explicitly make them false
  67. requireReturn = options.requireReturn !== false,
  68. requireParamDescription = options.requireParamDescription !== false,
  69. requireReturnDescription = options.requireReturnDescription !== false,
  70. requireReturnType = options.requireReturnType !== false,
  71. requireParamType = options.requireParamType !== false,
  72. preferType = options.preferType || {},
  73. checkPreferType = Object.keys(preferType).length !== 0;
  74. //--------------------------------------------------------------------------
  75. // Helpers
  76. //--------------------------------------------------------------------------
  77. // Using a stack to store if a function returns or not (handling nested functions)
  78. const fns = [];
  79. /**
  80. * Check if node type is a Class
  81. * @param {ASTNode} node node to check.
  82. * @returns {boolean} True is its a class
  83. * @private
  84. */
  85. function isTypeClass(node) {
  86. return node.type === "ClassExpression" || node.type === "ClassDeclaration";
  87. }
  88. /**
  89. * When parsing a new function, store it in our function stack.
  90. * @param {ASTNode} node A function node to check.
  91. * @returns {void}
  92. * @private
  93. */
  94. function startFunction(node) {
  95. fns.push({
  96. returnPresent: (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") ||
  97. isTypeClass(node) || node.async
  98. });
  99. }
  100. /**
  101. * Indicate that return has been found in the current function.
  102. * @param {ASTNode} node The return node.
  103. * @returns {void}
  104. * @private
  105. */
  106. function addReturn(node) {
  107. const functionState = fns[fns.length - 1];
  108. if (functionState && node.argument !== null) {
  109. functionState.returnPresent = true;
  110. }
  111. }
  112. /**
  113. * Check if return tag type is void or undefined
  114. * @param {Object} tag JSDoc tag
  115. * @returns {boolean} True if its of type void or undefined
  116. * @private
  117. */
  118. function isValidReturnType(tag) {
  119. return tag.type === null || tag.type.name === "void" || tag.type.type === "UndefinedLiteral";
  120. }
  121. /**
  122. * Check if type should be validated based on some exceptions
  123. * @param {Object} type JSDoc tag
  124. * @returns {boolean} True if it can be validated
  125. * @private
  126. */
  127. function canTypeBeValidated(type) {
  128. return type !== "UndefinedLiteral" && // {undefined} as there is no name property available.
  129. type !== "NullLiteral" && // {null}
  130. type !== "NullableLiteral" && // {?}
  131. type !== "FunctionType" && // {function(a)}
  132. type !== "AllLiteral"; // {*}
  133. }
  134. /**
  135. * Extract the current and expected type based on the input type object
  136. * @param {Object} type JSDoc tag
  137. * @returns {{currentType: Doctrine.Type, expectedTypeName: string}} The current type annotation and
  138. * the expected name of the annotation
  139. * @private
  140. */
  141. function getCurrentExpectedTypes(type) {
  142. let currentType;
  143. if (type.name) {
  144. currentType = type;
  145. } else if (type.expression) {
  146. currentType = type.expression;
  147. }
  148. return {
  149. currentType,
  150. expectedTypeName: currentType && preferType[currentType.name]
  151. };
  152. }
  153. /**
  154. * Gets the location of a JSDoc node in a file
  155. * @param {Token} jsdocComment The comment that this node is parsed from
  156. * @param {{range: number[]}} parsedJsdocNode A tag or other node which was parsed from this comment
  157. * @returns {{start: SourceLocation, end: SourceLocation}} The 0-based source location for the tag
  158. */
  159. function getAbsoluteRange(jsdocComment, parsedJsdocNode) {
  160. return {
  161. start: sourceCode.getLocFromIndex(jsdocComment.range[0] + 2 + parsedJsdocNode.range[0]),
  162. end: sourceCode.getLocFromIndex(jsdocComment.range[0] + 2 + parsedJsdocNode.range[1])
  163. };
  164. }
  165. /**
  166. * Validate type for a given JSDoc node
  167. * @param {Object} jsdocNode JSDoc node
  168. * @param {Object} type JSDoc tag
  169. * @returns {void}
  170. * @private
  171. */
  172. function validateType(jsdocNode, type) {
  173. if (!type || !canTypeBeValidated(type.type)) {
  174. return;
  175. }
  176. const typesToCheck = [];
  177. let elements = [];
  178. switch (type.type) {
  179. case "TypeApplication": // {Array.<String>}
  180. elements = type.applications[0].type === "UnionType" ? type.applications[0].elements : type.applications;
  181. typesToCheck.push(getCurrentExpectedTypes(type));
  182. break;
  183. case "RecordType": // {{20:String}}
  184. elements = type.fields;
  185. break;
  186. case "UnionType": // {String|number|Test}
  187. case "ArrayType": // {[String, number, Test]}
  188. elements = type.elements;
  189. break;
  190. case "FieldType": // Array.<{count: number, votes: number}>
  191. if (type.value) {
  192. typesToCheck.push(getCurrentExpectedTypes(type.value));
  193. }
  194. break;
  195. default:
  196. typesToCheck.push(getCurrentExpectedTypes(type));
  197. }
  198. elements.forEach(validateType.bind(null, jsdocNode));
  199. typesToCheck.forEach(typeToCheck => {
  200. if (typeToCheck.expectedTypeName &&
  201. typeToCheck.expectedTypeName !== typeToCheck.currentType.name) {
  202. context.report({
  203. node: jsdocNode,
  204. message: "Use '{{expectedTypeName}}' instead of '{{currentTypeName}}'.",
  205. loc: getAbsoluteRange(jsdocNode, typeToCheck.currentType),
  206. data: {
  207. currentTypeName: typeToCheck.currentType.name,
  208. expectedTypeName: typeToCheck.expectedTypeName
  209. },
  210. fix(fixer) {
  211. return fixer.replaceTextRange(
  212. typeToCheck.currentType.range.map(indexInComment => jsdocNode.range[0] + 2 + indexInComment),
  213. typeToCheck.expectedTypeName
  214. );
  215. }
  216. });
  217. }
  218. });
  219. }
  220. /**
  221. * Validate the JSDoc node and output warnings if anything is wrong.
  222. * @param {ASTNode} node The AST node to check.
  223. * @returns {void}
  224. * @private
  225. */
  226. function checkJSDoc(node) {
  227. const jsdocNode = sourceCode.getJSDocComment(node),
  228. functionData = fns.pop(),
  229. paramTagsByName = Object.create(null),
  230. paramTags = [];
  231. let hasReturns = false,
  232. returnsTag,
  233. hasConstructor = false,
  234. isInterface = false,
  235. isOverride = false,
  236. isAbstract = false;
  237. // make sure only to validate JSDoc comments
  238. if (jsdocNode) {
  239. let jsdoc;
  240. try {
  241. jsdoc = doctrine.parse(jsdocNode.value, {
  242. strict: true,
  243. unwrap: true,
  244. sloppy: true,
  245. range: true
  246. });
  247. } catch (ex) {
  248. if (/braces/i.test(ex.message)) {
  249. context.report({ node: jsdocNode, message: "JSDoc type missing brace." });
  250. } else {
  251. context.report({ node: jsdocNode, message: "JSDoc syntax error." });
  252. }
  253. return;
  254. }
  255. jsdoc.tags.forEach(tag => {
  256. switch (tag.title.toLowerCase()) {
  257. case "param":
  258. case "arg":
  259. case "argument":
  260. paramTags.push(tag);
  261. break;
  262. case "return":
  263. case "returns":
  264. hasReturns = true;
  265. returnsTag = tag;
  266. break;
  267. case "constructor":
  268. case "class":
  269. hasConstructor = true;
  270. break;
  271. case "override":
  272. case "inheritdoc":
  273. isOverride = true;
  274. break;
  275. case "abstract":
  276. case "virtual":
  277. isAbstract = true;
  278. break;
  279. case "interface":
  280. isInterface = true;
  281. break;
  282. // no default
  283. }
  284. // check tag preferences
  285. if (Object.prototype.hasOwnProperty.call(prefer, tag.title) && tag.title !== prefer[tag.title]) {
  286. const entireTagRange = getAbsoluteRange(jsdocNode, tag);
  287. context.report({
  288. node: jsdocNode,
  289. message: "Use @{{name}} instead.",
  290. loc: {
  291. start: entireTagRange.start,
  292. end: {
  293. line: entireTagRange.start.line,
  294. column: entireTagRange.start.column + `@${tag.title}`.length
  295. }
  296. },
  297. data: { name: prefer[tag.title] },
  298. fix(fixer) {
  299. return fixer.replaceTextRange(
  300. [
  301. jsdocNode.range[0] + tag.range[0] + 3,
  302. jsdocNode.range[0] + tag.range[0] + tag.title.length + 3
  303. ],
  304. prefer[tag.title]
  305. );
  306. }
  307. });
  308. }
  309. // validate the types
  310. if (checkPreferType && tag.type) {
  311. validateType(jsdocNode, tag.type);
  312. }
  313. });
  314. paramTags.forEach(param => {
  315. if (requireParamType && !param.type) {
  316. context.report({
  317. node: jsdocNode,
  318. message: "Missing JSDoc parameter type for '{{name}}'.",
  319. loc: getAbsoluteRange(jsdocNode, param),
  320. data: { name: param.name }
  321. });
  322. }
  323. if (!param.description && requireParamDescription) {
  324. context.report({
  325. node: jsdocNode,
  326. message: "Missing JSDoc parameter description for '{{name}}'.",
  327. loc: getAbsoluteRange(jsdocNode, param),
  328. data: { name: param.name }
  329. });
  330. }
  331. if (paramTagsByName[param.name]) {
  332. context.report({
  333. node: jsdocNode,
  334. message: "Duplicate JSDoc parameter '{{name}}'.",
  335. loc: getAbsoluteRange(jsdocNode, param),
  336. data: { name: param.name }
  337. });
  338. } else if (param.name.indexOf(".") === -1) {
  339. paramTagsByName[param.name] = param;
  340. }
  341. });
  342. if (hasReturns) {
  343. if (!requireReturn && !functionData.returnPresent && (returnsTag.type === null || !isValidReturnType(returnsTag)) && !isAbstract) {
  344. context.report({
  345. node: jsdocNode,
  346. message: "Unexpected @{{title}} tag; function has no return statement.",
  347. loc: getAbsoluteRange(jsdocNode, returnsTag),
  348. data: {
  349. title: returnsTag.title
  350. }
  351. });
  352. } else {
  353. if (requireReturnType && !returnsTag.type) {
  354. context.report({ node: jsdocNode, message: "Missing JSDoc return type." });
  355. }
  356. if (!isValidReturnType(returnsTag) && !returnsTag.description && requireReturnDescription) {
  357. context.report({ node: jsdocNode, message: "Missing JSDoc return description." });
  358. }
  359. }
  360. }
  361. // check for functions missing @returns
  362. if (!isOverride && !hasReturns && !hasConstructor && !isInterface &&
  363. node.parent.kind !== "get" && node.parent.kind !== "constructor" &&
  364. node.parent.kind !== "set" && !isTypeClass(node)) {
  365. if (requireReturn || (functionData.returnPresent && !node.async)) {
  366. context.report({
  367. node: jsdocNode,
  368. message: "Missing JSDoc @{{returns}} for function.",
  369. data: {
  370. returns: prefer.returns || "returns"
  371. }
  372. });
  373. }
  374. }
  375. // check the parameters
  376. const jsdocParamNames = Object.keys(paramTagsByName);
  377. if (node.params) {
  378. node.params.forEach((param, paramsIndex) => {
  379. const bindingParam = param.type === "AssignmentPattern"
  380. ? param.left
  381. : param;
  382. // TODO(nzakas): Figure out logical things to do with destructured, default, rest params
  383. if (bindingParam.type === "Identifier") {
  384. const name = bindingParam.name;
  385. if (jsdocParamNames[paramsIndex] && (name !== jsdocParamNames[paramsIndex])) {
  386. context.report({
  387. node: jsdocNode,
  388. message: "Expected JSDoc for '{{name}}' but found '{{jsdocName}}'.",
  389. loc: getAbsoluteRange(jsdocNode, paramTagsByName[jsdocParamNames[paramsIndex]]),
  390. data: {
  391. name,
  392. jsdocName: jsdocParamNames[paramsIndex]
  393. }
  394. });
  395. } else if (!paramTagsByName[name] && !isOverride) {
  396. context.report({
  397. node: jsdocNode,
  398. message: "Missing JSDoc for parameter '{{name}}'.",
  399. data: {
  400. name
  401. }
  402. });
  403. }
  404. }
  405. });
  406. }
  407. if (options.matchDescription) {
  408. const regex = new RegExp(options.matchDescription);
  409. if (!regex.test(jsdoc.description)) {
  410. context.report({ node: jsdocNode, message: "JSDoc description does not satisfy the regex pattern." });
  411. }
  412. }
  413. }
  414. }
  415. //--------------------------------------------------------------------------
  416. // Public
  417. //--------------------------------------------------------------------------
  418. return {
  419. ArrowFunctionExpression: startFunction,
  420. FunctionExpression: startFunction,
  421. FunctionDeclaration: startFunction,
  422. ClassExpression: startFunction,
  423. ClassDeclaration: startFunction,
  424. "ArrowFunctionExpression:exit": checkJSDoc,
  425. "FunctionExpression:exit": checkJSDoc,
  426. "FunctionDeclaration:exit": checkJSDoc,
  427. "ClassExpression:exit": checkJSDoc,
  428. "ClassDeclaration:exit": checkJSDoc,
  429. ReturnStatement: addReturn
  430. };
  431. }
  432. };