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