require-jsdoc.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @fileoverview Rule to check for jsdoc presence.
  3. * @author Gyandeep Singh
  4. */
  5. "use strict";
  6. module.exports = {
  7. meta: {
  8. docs: {
  9. description: "require JSDoc comments",
  10. category: "Stylistic Issues",
  11. recommended: false,
  12. url: "https://eslint.org/docs/rules/require-jsdoc"
  13. },
  14. schema: [
  15. {
  16. type: "object",
  17. properties: {
  18. require: {
  19. type: "object",
  20. properties: {
  21. ClassDeclaration: {
  22. type: "boolean"
  23. },
  24. MethodDefinition: {
  25. type: "boolean"
  26. },
  27. FunctionDeclaration: {
  28. type: "boolean"
  29. },
  30. ArrowFunctionExpression: {
  31. type: "boolean"
  32. },
  33. FunctionExpression: {
  34. type: "boolean"
  35. }
  36. },
  37. additionalProperties: false
  38. }
  39. },
  40. additionalProperties: false
  41. }
  42. ]
  43. },
  44. create(context) {
  45. const source = context.getSourceCode();
  46. const DEFAULT_OPTIONS = {
  47. FunctionDeclaration: true,
  48. MethodDefinition: false,
  49. ClassDeclaration: false,
  50. ArrowFunctionExpression: false,
  51. FunctionExpression: false
  52. };
  53. const options = Object.assign(DEFAULT_OPTIONS, context.options[0] && context.options[0].require || {});
  54. /**
  55. * Report the error message
  56. * @param {ASTNode} node node to report
  57. * @returns {void}
  58. */
  59. function report(node) {
  60. context.report({ node, message: "Missing JSDoc comment." });
  61. }
  62. /**
  63. * Check if the jsdoc comment is present or not.
  64. * @param {ASTNode} node node to examine
  65. * @returns {void}
  66. */
  67. function checkJsDoc(node) {
  68. const jsdocComment = source.getJSDocComment(node);
  69. if (!jsdocComment) {
  70. report(node);
  71. }
  72. }
  73. return {
  74. FunctionDeclaration(node) {
  75. if (options.FunctionDeclaration) {
  76. checkJsDoc(node);
  77. }
  78. },
  79. FunctionExpression(node) {
  80. if (
  81. (options.MethodDefinition && node.parent.type === "MethodDefinition") ||
  82. (options.FunctionExpression && (node.parent.type === "VariableDeclarator" || (node.parent.type === "Property" && node === node.parent.value)))
  83. ) {
  84. checkJsDoc(node);
  85. }
  86. },
  87. ClassDeclaration(node) {
  88. if (options.ClassDeclaration) {
  89. checkJsDoc(node);
  90. }
  91. },
  92. ArrowFunctionExpression(node) {
  93. if (options.ArrowFunctionExpression && node.parent.type === "VariableDeclarator") {
  94. checkJsDoc(node);
  95. }
  96. }
  97. };
  98. }
  99. };