Skip to content

Commit 9eaaab1

Browse files
committed
feat(require-example): add exemptNoArguments option
1 parent 428174d commit 9eaaab1

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8213,6 +8213,15 @@ function quux () {
82138213
}
82148214
// Message: Missing JSDoc @example declaration.
82158215
8216+
/**
8217+
*
8218+
*/
8219+
function quux (someParam) {
8220+
8221+
}
8222+
// Options: [{"exemptNoArguments":true}]
8223+
// Message: Missing JSDoc @example declaration.
8224+
82168225
/**
82178226
*
82188227
*/
@@ -8442,6 +8451,14 @@ class TestClass {
84428451
set Test(value) { }
84438452
}
84448453
// Options: [{"checkSetters":true}]
8454+
8455+
/**
8456+
*
8457+
*/
8458+
function quux () {
8459+
8460+
}
8461+
// Options: [{"exemptNoArguments":true}]
84458462
````
84468463
84478464

src/iterateJsdoc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ const getUtils = (
196196
return jsdocUtils.getFunctionParameterNames(node);
197197
};
198198

199+
utils.hasParams = () => {
200+
return jsdocUtils.hasParams(node);
201+
};
202+
199203
utils.isConstructor = () => {
200204
return jsdocUtils.isConstructor(node);
201205
};

src/jsdocUtils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ const getFunctionParameterNames = (functionNode : Object) : Array<T> => {
188188
});
189189
};
190190

191+
const hasParams = (functionNode) => {
192+
// Should also check `functionNode.value.params` if supporting `MethodDefinition`
193+
return functionNode.params.length;
194+
};
195+
191196
/**
192197
* Gets all names of the target type, including those that refer to a path, e.g.
193198
* "@param foo; @param foo.bar".
@@ -736,6 +741,7 @@ export default {
736741
getTagStructureForMode,
737742
hasATag,
738743
hasDefinedTypeReturnTag,
744+
hasParams,
739745
hasReturnValue,
740746
hasTag,
741747
hasThrowValue,

src/rules/requireExample.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _ from 'lodash';
22
import iterateJsdoc from '../iterateJsdoc';
33

44
export default iterateJsdoc(({
5+
context,
56
jsdoc,
67
report,
78
utils,
@@ -10,13 +11,23 @@ export default iterateJsdoc(({
1011
return;
1112
}
1213

14+
const {
15+
exemptNoArguments = false,
16+
} = context.options[0] || {};
17+
1318
const targetTagName = 'example';
1419

1520
const functionExamples = _.filter(jsdoc.tags, {
1621
tag: targetTagName,
1722
});
1823

1924
if (!functionExamples.length) {
25+
if (exemptNoArguments && utils.isIteratingFunction() &&
26+
!utils.hasParams()
27+
) {
28+
return;
29+
}
30+
2031
utils.reportJSDoc(`Missing JSDoc @${targetTagName} declaration.`, null, () => {
2132
if (!jsdoc.tags) {
2233
jsdoc.tags = [];
@@ -78,6 +89,10 @@ export default iterateJsdoc(({
7889
},
7990
type: 'array',
8091
},
92+
exemptNoArguments: {
93+
default: false,
94+
type: 'boolean',
95+
},
8196
},
8297
type: 'object',
8398
},

test/rules/assertions/requireExample.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ export default {
2323
}
2424
`,
2525
},
26+
{
27+
code: `
28+
/**
29+
*
30+
*/
31+
function quux (someParam) {
32+
33+
}
34+
`,
35+
errors: [
36+
{
37+
message: 'Missing JSDoc @example declaration.',
38+
},
39+
],
40+
options: [
41+
{
42+
exemptNoArguments: true,
43+
},
44+
],
45+
output: `
46+
/**
47+
* @example
48+
*/
49+
function quux (someParam) {
50+
51+
}
52+
`,
53+
},
2654
{
2755
code: `/**
2856
*
@@ -499,5 +527,20 @@ function quux () {
499527
},
500528
],
501529
},
530+
{
531+
code: `
532+
/**
533+
*
534+
*/
535+
function quux () {
536+
537+
}
538+
`,
539+
options: [
540+
{
541+
exemptNoArguments: true,
542+
},
543+
],
544+
},
502545
],
503546
};

0 commit comments

Comments
 (0)