UNPKG

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