require-jsdoc.js 3.6 KB

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