Skip to content

Commit 373252c

Browse files
committed
- Allow tentative API for options in iterateJsdoc to support returning objects defining scopes beyond just the default functions; utilize in requireJsdoc
1 parent 50abf16 commit 373252c

File tree

3 files changed

+67
-60
lines changed

3 files changed

+67
-60
lines changed

Diff for: src/iterateJsdoc.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const parseComment = (commentNode, indent) => {
2222
};
2323

2424
const curryUtils = (
25-
functionNode,
25+
node,
2626
jsdoc,
2727
tagNamePreference,
2828
exampleCodeRegex,
@@ -47,15 +47,15 @@ const curryUtils = (
4747
const utils = {};
4848

4949
utils.getFunctionParameterNames = () => {
50-
return jsdocUtils.getFunctionParameterNames(functionNode);
50+
return jsdocUtils.getFunctionParameterNames(node);
5151
};
5252

5353
utils.getFunctionSourceCode = () => {
54-
return sourceCode.getText(functionNode);
54+
return sourceCode.getText(node);
5555
};
5656

5757
utils.isConstructor = () => {
58-
return functionNode.parent && functionNode.parent.kind === 'constructor';
58+
return node.parent && node.parent.kind === 'constructor';
5959
};
6060

6161
utils.getJsdocParameterNamesDeep = () => {
@@ -178,8 +178,19 @@ export {
178178
parseComment
179179
};
180180

181-
export default (iterator, meta) => {
181+
export default (iterator, options) => {
182+
const opts = options || {};
183+
182184
return {
185+
/**
186+
* The entrypoint for the JSDoc rule.
187+
*
188+
* @param {*} context
189+
* a reference to the context which hold all important information
190+
* like settings and the sourcecode to check.
191+
* @returns {Object}
192+
* a list with parser callback function.
193+
*/
183194
create (context) {
184195
const sourceCode = context.getSourceCode();
185196
const tagNamePreference = _.get(context, 'settings.jsdoc.tagNamePreference') || {};
@@ -200,8 +211,8 @@ export default (iterator, meta) => {
200211
const allowAugmentsExtendsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowAugmentsExtendsWithoutParam'));
201212
const checkSeesForNamepaths = Boolean(_.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
202213

203-
const checkJsdoc = (functionNode) => {
204-
const jsdocNode = sourceCode.getJSDocComment(functionNode);
214+
const checkJsdoc = (node) => {
215+
const jsdocNode = sourceCode.getJSDocComment(node);
205216

206217
if (!jsdocNode) {
207218
return;
@@ -247,7 +258,7 @@ export default (iterator, meta) => {
247258
};
248259

249260
const utils = curryUtils(
250-
functionNode,
261+
node,
251262
jsdoc,
252263
tagNamePreference,
253264
exampleCodeRegex,
@@ -272,22 +283,26 @@ export default (iterator, meta) => {
272283

273284
iterator({
274285
context,
275-
functionNode,
276286
indent,
277287
jsdoc,
278288
jsdocNode,
289+
node,
279290
report,
280291
sourceCode,
281292
utils
282293
});
283294
};
284295

296+
if (opts.returns) {
297+
return opts.returns(context, sourceCode, checkJsdoc);
298+
}
299+
285300
return {
286301
ArrowFunctionExpression: checkJsdoc,
287302
FunctionDeclaration: checkJsdoc,
288303
FunctionExpression: checkJsdoc
289304
};
290305
},
291-
meta
306+
meta: opts.meta
292307
};
293308
};

Diff for: src/rules/requireJsdoc.js

+39-47
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import iterateJsdoc from '../iterateJsdoc';
2+
13
const OPTIONS_SCHEMA = {
24
additionalProperties: false,
35
properties: {
@@ -58,30 +60,39 @@ const getOptions = (context) => {
5860
};
5961
};
6062

61-
const checkJsDoc = (context, node) => {
62-
const jsDocNode = context.getSourceCode().getJSDocComment(node);
63+
export default iterateJsdoc(null, {
64+
meta: {
65+
doc: {
66+
category: 'Stylistic Issues',
67+
description: 'Require JSDoc comments',
68+
recommended: 'true',
69+
url: 'https://github.com/gajus/eslint-plugin-jsdoc'
70+
},
6371

64-
if (jsDocNode) {
65-
return;
66-
}
72+
messages: {
73+
missingJsDoc: 'Missing JSDoc comment.'
74+
},
6775

68-
context.report({
69-
messageId: 'missingJsDoc',
70-
node
71-
});
72-
};
76+
schema: [
77+
OPTIONS_SCHEMA
78+
],
79+
80+
type: 'suggestion'
81+
},
82+
returns (context, sourceCode) {
83+
const checkJsDoc = (node) => {
84+
const jsDocNode = sourceCode.getJSDocComment(node);
85+
86+
if (jsDocNode) {
87+
return;
88+
}
89+
90+
context.report({
91+
messageId: 'missingJsDoc',
92+
node
93+
});
94+
};
7395

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) {
8596
const options = getOptions(context);
8697

8798
return {
@@ -94,28 +105,28 @@ export default {
94105
return;
95106
}
96107

97-
checkJsDoc(context, node);
108+
checkJsDoc(node);
98109
},
99110

100111
ClassDeclaration: (node) => {
101112
if (!options.ClassDeclaration) {
102113
return;
103114
}
104115

105-
checkJsDoc(context, node);
116+
checkJsDoc(node);
106117
},
107118

108119
FunctionDeclaration: (node) => {
109120
if (!options.FunctionDeclaration) {
110121
return;
111122
}
112123

113-
checkJsDoc(context, node);
124+
checkJsDoc(node);
114125
},
115126

116127
FunctionExpression: (node) => {
117128
if (options.MethodDefinition && node.parent.type === 'MethodDefinition') {
118-
checkJsDoc(context, node);
129+
checkJsDoc(node);
119130

120131
return;
121132
}
@@ -125,32 +136,13 @@ export default {
125136
}
126137

127138
if (node.parent.type === 'VariableDeclarator') {
128-
checkJsDoc(context, node);
139+
checkJsDoc(node);
129140
}
130141

131142
if (node.parent.type === 'Property' && node === node.parent.value) {
132-
checkJsDoc(context, node);
143+
checkJsDoc(node);
133144
}
134145
}
135146
};
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'
155147
}
156-
};
148+
});

Diff for: src/rules/requireReturnsCheck.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import iterateJsdoc from '../iterateJsdoc';
33
export default iterateJsdoc(({
44
jsdoc,
55
report,
6-
functionNode,
6+
node,
77
utils
88
}) => {
99
// Implicit return like `() => foo` is ok
10-
if (functionNode.type === 'ArrowFunctionExpression' && functionNode.expression) {
10+
if (node.type === 'ArrowFunctionExpression' && node.expression) {
1111
return;
1212
}
1313

1414
// Async function always returns a promise
15-
if (functionNode.async) {
15+
if (node.async) {
1616
return;
1717
}
1818

0 commit comments

Comments
 (0)