UNPKG

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