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