Skip to content

Commit dc6857a

Browse files
authored
Merge pull request #205 from allthesignals/master
feat: implement a requireJSDoc compatible rule with working tests
2 parents 760fa6d + 3742994 commit dc6857a

File tree

4 files changed

+699
-0
lines changed

4 files changed

+699
-0
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import requireReturnsCheck from './rules/requireReturnsCheck';
2121
import requireReturnsDescription from './rules/requireReturnsDescription';
2222
import requireReturnsType from './rules/requireReturnsType';
2323
import validTypes from './rules/validTypes';
24+
import requireJsdoc from './rules/requireJsdoc';
2425

2526
export default {
2627
configs: {
@@ -39,6 +40,7 @@ export default {
3940
'jsdoc/require-description-complete-sentence': 'off',
4041
'jsdoc/require-example': 'off',
4142
'jsdoc/require-hyphen-before-param-description': 'off',
43+
'jsdoc/require-jsdoc': 'warn',
4244
'jsdoc/require-param': 'warn',
4345
'jsdoc/require-param-description': 'warn',
4446
'jsdoc/require-param-name': 'warn',
@@ -65,6 +67,7 @@ export default {
6567
'require-description-complete-sentence': requireDescriptionCompleteSentence,
6668
'require-example': requireExample,
6769
'require-hyphen-before-param-description': requireHyphenBeforeParamDescription,
70+
'require-jsdoc': requireJsdoc,
6871
'require-param': requireParam,
6972
'require-param-description': requireParamDescription,
7073
'require-param-name': requireParamName,

src/rules/requireJsdoc.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
const OPTIONS_SCHEMA = {
2+
additionalProperties: false,
3+
properties: {
4+
require: {
5+
additionalProperties: false,
6+
default: {},
7+
properties: {
8+
ArrowFunctionExpression: {
9+
default: false,
10+
type: 'boolean'
11+
},
12+
ClassDeclaration: {
13+
default: false,
14+
type: 'boolean'
15+
},
16+
FunctionDeclaration: {
17+
default: true,
18+
type: 'boolean'
19+
},
20+
FunctionExpression: {
21+
default: false,
22+
type: 'boolean'
23+
},
24+
MethodDefinition: {
25+
default: false,
26+
type: 'boolean'
27+
}
28+
},
29+
type: 'object'
30+
}
31+
},
32+
type: 'object'
33+
};
34+
35+
const getOption = (context, key) => {
36+
if (!context.options.length) {
37+
return OPTIONS_SCHEMA.properties.require.properties[key].default;
38+
}
39+
40+
if (!context.options[0] || !context.options[0].require) {
41+
return OPTIONS_SCHEMA.properties.require.properties[key].default;
42+
}
43+
44+
if (!context.options[0].require.hasOwnProperty(key)) {
45+
return OPTIONS_SCHEMA.properties.require.properties[key].default;
46+
}
47+
48+
return context.options[0].require[key];
49+
};
50+
51+
const getOptions = (context) => {
52+
return {
53+
ArrowFunctionExpression: getOption(context, 'ArrowFunctionExpression'),
54+
ClassDeclaration: getOption(context, 'ClassDeclaration'),
55+
FunctionDeclaration: getOption(context, 'FunctionDeclaration'),
56+
FunctionExpression: getOption(context, 'FunctionExpression'),
57+
MethodDefinition: getOption(context, 'MethodDefinition')
58+
};
59+
};
60+
61+
const checkJsDoc = (context, node) => {
62+
const jsDocNode = context.getSourceCode().getJSDocComment(node);
63+
64+
if (jsDocNode) {
65+
return;
66+
}
67+
68+
context.report({
69+
messageId: 'missingJsDoc',
70+
node
71+
});
72+
};
73+
74+
export default {
75+
/**
76+
* The entrypoint for the JSDoc rule.
77+
*
78+
* @param {*} context
79+
* a reference to the context which hold all important information
80+
* like settings and the sourcecode to check.
81+
* @returns {*}
82+
* a list with parser callback function.
83+
*/
84+
create (context) {
85+
const options = getOptions(context);
86+
87+
return {
88+
ArrowFunctionExpression: (node) => {
89+
if (!options.ArrowFunctionExpression) {
90+
return;
91+
}
92+
93+
if (node.parent.type !== 'VariableDeclarator') {
94+
return;
95+
}
96+
97+
checkJsDoc(context, node);
98+
},
99+
100+
ClassDeclaration: (node) => {
101+
if (!options.ClassDeclaration) {
102+
return;
103+
}
104+
105+
checkJsDoc(context, node);
106+
},
107+
108+
FunctionDeclaration: (node) => {
109+
if (!options.FunctionDeclaration) {
110+
return;
111+
}
112+
113+
checkJsDoc(context, node);
114+
},
115+
116+
FunctionExpression: (node) => {
117+
if (options.MethodDefinition && node.parent.type === 'MethodDefinition') {
118+
checkJsDoc(context, node);
119+
120+
return;
121+
}
122+
123+
if (!options.FunctionExpression) {
124+
return;
125+
}
126+
127+
if (node.parent.type === 'VariableDeclarator') {
128+
checkJsDoc(context, node);
129+
}
130+
131+
if (node.parent.type === 'Property' && node === node.parent.value) {
132+
checkJsDoc(context, node);
133+
}
134+
}
135+
};
136+
},
137+
138+
meta: {
139+
doc: {
140+
category: 'Stylistic Issues',
141+
description: 'Require JSDoc comments',
142+
recommended: 'true',
143+
url: 'https://github.com/gajus/eslint-plugin-jsdoc'
144+
},
145+
146+
messages: {
147+
missingJsDoc: 'Missing JSDoc comment.'
148+
},
149+
150+
schema: [
151+
OPTIONS_SCHEMA
152+
],
153+
154+
type: 'suggestion'
155+
}
156+
};

0 commit comments

Comments
 (0)