Skip to content

Commit c750c86

Browse files
authored
feat(require-example): add exemptNoArguments option (#634)
* feat(`require-example`): add `exemptNoArguments` option * docs(`require-example`): add `exemptNoArguments`
1 parent 428174d commit c750c86

File tree

6 files changed

+98
-2
lines changed

6 files changed

+98
-2
lines changed

.README/rules/require-example.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ block avoids the need for an `@example`. Defaults to an array with
1818
so be sure to add back `inheritdoc` if you wish its presence to cause
1919
exemption of the rule.
2020

21+
##### `exemptNoArguments`
22+
23+
Boolean to indicate that no-argument functions should not be reported for
24+
missing `@example` declarations.
25+
2126
##### `contexts`
2227

2328
Set this to an array of strings representing the AST context
@@ -50,7 +55,7 @@ report a missing example description after this is added.
5055
|---|---|
5156
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
5257
|Tags|`example`|
53-
|Options|`exemptedBy`, `avoidExampleOnConstructors`, `contexts`|
58+
|Options|`exemptedBy`, `exemptNoArguments`, `avoidExampleOnConstructors`, `contexts`|
5459
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
5560

5661
<!-- assertions requireExample -->

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8162,6 +8162,12 @@ block avoids the need for an `@example`. Defaults to an array with
81628162
so be sure to add back `inheritdoc` if you wish its presence to cause
81638163
exemption of the rule.
81648164
8165+
<a name="eslint-plugin-jsdoc-rules-require-example-options-18-exemptnoarguments"></a>
8166+
##### <code>exemptNoArguments</code>
8167+
8168+
Boolean to indicate that no-argument functions should not be reported for
8169+
missing `@example` declarations.
8170+
81658171
<a name="eslint-plugin-jsdoc-rules-require-example-options-18-contexts-4"></a>
81668172
##### <code>contexts</code>
81678173
@@ -8199,7 +8205,7 @@ report a missing example description after this is added.
81998205
|---|---|
82008206
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
82018207
|Tags|`example`|
8202-
|Options|`exemptedBy`, `avoidExampleOnConstructors`, `contexts`|
8208+
|Options|`exemptedBy`, `exemptNoArguments`, `avoidExampleOnConstructors`, `contexts`|
82038209
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
82048210
82058211
The following patterns are considered problems:
@@ -8213,6 +8219,15 @@ function quux () {
82138219
}
82148220
// Message: Missing JSDoc @example declaration.
82158221
8222+
/**
8223+
*
8224+
*/
8225+
function quux (someParam) {
8226+
8227+
}
8228+
// Options: [{"exemptNoArguments":true}]
8229+
// Message: Missing JSDoc @example declaration.
8230+
82168231
/**
82178232
*
82188233
*/
@@ -8442,6 +8457,14 @@ class TestClass {
84428457
set Test(value) { }
84438458
}
84448459
// Options: [{"checkSetters":true}]
8460+
8461+
/**
8462+
*
8463+
*/
8464+
function quux () {
8465+
8466+
}
8467+
// Options: [{"exemptNoArguments":true}]
84458468
````
84468469
84478470

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)