require-jsdoc.js 3.8 KB

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