Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 20KB

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