UNPKG

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