Skip to content

Commit cea72bf

Browse files
authored
Merge pull request #232 from brettz9/require-jsdoc-exempt-empty-functions
require-jsdoc exempt empty functions option (fix for #77)
2 parents ecbbf5d + bddb1aa commit cea72bf

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

src/rules/requireJsdoc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import _ from 'lodash';
12
import iterateJsdoc from '../iterateJsdoc';
3+
import jsdocUtils from '../jsdocUtils';
24

35
const OPTIONS_SCHEMA = {
46
additionalProperties: false,
@@ -87,6 +89,16 @@ export default iterateJsdoc(null, {
8789
return;
8890
}
8991

92+
const exemptEmptyFunctions = Boolean(_.get(context, 'settings.jsdoc.exemptEmptyFunctions'));
93+
if (exemptEmptyFunctions) {
94+
const functionParameterNames = jsdocUtils.getFunctionParameterNames(node);
95+
if (!functionParameterNames.length) {
96+
if (!jsdocUtils.hasReturnValue(node, context)) {
97+
return;
98+
}
99+
}
100+
}
101+
90102
context.report({
91103
messageId: 'missingJsDoc',
92104
node

test/rules/assertions/requireJsdoc.js

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,45 @@ export default {
216216
FunctionExpression: true
217217
}
218218
}]
219+
},
220+
{
221+
code: `
222+
function foo (abc) {}
223+
`,
224+
errors: [{
225+
message: 'Missing JSDoc comment.',
226+
type: 'FunctionDeclaration'
227+
}],
228+
settings: {
229+
jsdoc: {
230+
exemptEmptyFunctions: false
231+
}
232+
}
233+
},
234+
{
235+
code: `
236+
function foo () {
237+
return true;
238+
}
239+
`,
240+
errors: [{
241+
message: 'Missing JSDoc comment.',
242+
type: 'FunctionDeclaration'
243+
}],
244+
settings: {
245+
jsdoc: {
246+
exemptEmptyFunctions: false
247+
}
248+
}
219249
}
220250
],
221251
valid: [{
222252
code: `
223253
var array = [1,2,3];
224254
array.forEach(function() {});
225-
255+
226256
/**
227-
* @class MyClass
257+
* @class MyClass
228258
**/
229259
function MyClass() {}
230260
@@ -240,14 +270,14 @@ export default {
240270
Function doing something
241271
*/
242272
Object.myFunction = function () {};
243-
var obj = {
273+
var obj = {
244274
/**
245275
* Function doing something
246276
**/
247277
myFunction: function () {} };
248-
278+
249279
/**
250-
@func myFunction
280+
@func myFunction
251281
*/
252282
function myFunction() {}
253283
/**
@@ -258,9 +288,9 @@ export default {
258288
@function myFunction
259289
*/
260290
function myFunction() {}
261-
291+
262292
/**
263-
@func myFunction
293+
@func myFunction
264294
*/
265295
var myFunction = function () {}
266296
/**
@@ -271,9 +301,9 @@ export default {
271301
@function myFunction
272302
*/
273303
var myFunction = function () {}
274-
304+
275305
/**
276-
@func myFunction
306+
@func myFunction
277307
*/
278308
Object.myFunction = function() {}
279309
/**
@@ -285,23 +315,23 @@ export default {
285315
*/
286316
Object.myFunction = function() {}
287317
(function(){})();
288-
318+
289319
var object = {
290320
/**
291-
* @func myFunction - Some function
321+
* @func myFunction - Some function
292322
*/
293323
myFunction: function() {} }
294324
var object = {
295325
/**
296-
* @method myFunction - Some function
326+
* @method myFunction - Some function
297327
*/
298328
myFunction: function() {} }
299329
var object = {
300330
/**
301-
* @function myFunction - Some function
331+
* @function myFunction - Some function
302332
*/
303333
myFunction: function() {} }
304-
334+
305335
var array = [1,2,3];
306336
array.filter(function() {});
307337
Object.keys(this.options.rules || {}).forEach(function(name) {}.bind(this));
@@ -534,6 +564,28 @@ export default {
534564
parserOptions: {
535565
ecmaVersion: 6
536566
}
567+
},
568+
{
569+
code: `
570+
function foo () {}
571+
`,
572+
settings: {
573+
jsdoc: {
574+
exemptEmptyFunctions: true
575+
}
576+
}
577+
},
578+
{
579+
code: `
580+
function foo () {
581+
return;
582+
}
583+
`,
584+
settings: {
585+
jsdoc: {
586+
exemptEmptyFunctions: true
587+
}
588+
}
537589
}
538590
]
539591
};

0 commit comments

Comments
 (0)