blob: 416a22ce6c4375a1f2ebd0c8a722430db31cdc3c [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
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
58 create(context) {
59 const source = context.getSourceCode();
60 const DEFAULT_OPTIONS = {
61 FunctionDeclaration: true,
62 MethodDefinition: false,
63 ClassDeclaration: false,
64 ArrowFunctionExpression: false,
65 FunctionExpression: false
66 };
67 const options = Object.assign(DEFAULT_OPTIONS, context.options[0] && context.options[0].require);
68
69 /**
70 * Report the error message
71 * @param {ASTNode} node node to report
72 * @returns {void}
73 */
74 function report(node) {
75 context.report({ node, message: "Missing JSDoc comment." });
76 }
77
78 /**
79 * Check if the jsdoc comment is present or not.
80 * @param {ASTNode} node node to examine
81 * @returns {void}
82 */
83 function checkJsDoc(node) {
84 const jsdocComment = source.getJSDocComment(node);
85
86 if (!jsdocComment) {
87 report(node);
88 }
89 }
90
91 return {
92 FunctionDeclaration(node) {
93 if (options.FunctionDeclaration) {
94 checkJsDoc(node);
95 }
96 },
97 FunctionExpression(node) {
98 if (
99 (options.MethodDefinition && node.parent.type === "MethodDefinition") ||
100 (options.FunctionExpression && (node.parent.type === "VariableDeclarator" || (node.parent.type === "Property" && node === node.parent.value)))
101 ) {
102 checkJsDoc(node);
103 }
104 },
105 ClassDeclaration(node) {
106 if (options.ClassDeclaration) {
107 checkJsDoc(node);
108 }
109 },
110 ArrowFunctionExpression(node) {
111 if (options.ArrowFunctionExpression && node.parent.type === "VariableDeclarator") {
112 checkJsDoc(node);
113 }
114 }
115 };
116 }
117};