UNPKG

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