Skip to content

Commit

Permalink
feat(require-example): add exemptNoArguments option (#634)
Browse files Browse the repository at this point in the history
* feat(`require-example`): add `exemptNoArguments` option

* docs(`require-example`): add `exemptNoArguments`
  • Loading branch information
brettz9 committed Sep 9, 2020
1 parent 428174d commit c750c86
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .README/rules/require-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ block avoids the need for an `@example`. Defaults to an array with
so be sure to add back `inheritdoc` if you wish its presence to cause
exemption of the rule.

##### `exemptNoArguments`

Boolean to indicate that no-argument functions should not be reported for
missing `@example` declarations.

##### `contexts`

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

<!-- assertions requireExample -->
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8162,6 +8162,12 @@ block avoids the need for an `@example`. Defaults to an array with
so be sure to add back `inheritdoc` if you wish its presence to cause
exemption of the rule.
<a name="eslint-plugin-jsdoc-rules-require-example-options-18-exemptnoarguments"></a>
##### <code>exemptNoArguments</code>
Boolean to indicate that no-argument functions should not be reported for
missing `@example` declarations.
<a name="eslint-plugin-jsdoc-rules-require-example-options-18-contexts-4"></a>
##### <code>contexts</code>
Expand Down Expand Up @@ -8199,7 +8205,7 @@ report a missing example description after this is added.
|---|---|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
|Tags|`example`|
|Options|`exemptedBy`, `avoidExampleOnConstructors`, `contexts`|
|Options|`exemptedBy`, `exemptNoArguments`, `avoidExampleOnConstructors`, `contexts`|
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
The following patterns are considered problems:
Expand All @@ -8213,6 +8219,15 @@ function quux () {
}
// Message: Missing JSDoc @example declaration.
/**
*
*/
function quux (someParam) {
}
// Options: [{"exemptNoArguments":true}]
// Message: Missing JSDoc @example declaration.
/**
*
*/
Expand Down Expand Up @@ -8442,6 +8457,14 @@ class TestClass {
set Test(value) { }
}
// Options: [{"checkSetters":true}]
/**
*
*/
function quux () {
}
// Options: [{"exemptNoArguments":true}]
````
Expand Down
4 changes: 4 additions & 0 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ const getUtils = (
return jsdocUtils.getFunctionParameterNames(node);
};

utils.hasParams = () => {
return jsdocUtils.hasParams(node);
};

utils.isConstructor = () => {
return jsdocUtils.isConstructor(node);
};
Expand Down
6 changes: 6 additions & 0 deletions src/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ const getFunctionParameterNames = (functionNode : Object) : Array<T> => {
});
};

const hasParams = (functionNode) => {
// Should also check `functionNode.value.params` if supporting `MethodDefinition`
return functionNode.params.length;
};

/**
* Gets all names of the target type, including those that refer to a path, e.g.
* "@param foo; @param foo.bar".
Expand Down Expand Up @@ -736,6 +741,7 @@ export default {
getTagStructureForMode,
hasATag,
hasDefinedTypeReturnTag,
hasParams,
hasReturnValue,
hasTag,
hasThrowValue,
Expand Down
15 changes: 15 additions & 0 deletions src/rules/requireExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash';
import iterateJsdoc from '../iterateJsdoc';

export default iterateJsdoc(({
context,
jsdoc,
report,
utils,
Expand All @@ -10,13 +11,23 @@ export default iterateJsdoc(({
return;
}

const {
exemptNoArguments = false,
} = context.options[0] || {};

const targetTagName = 'example';

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

if (!functionExamples.length) {
if (exemptNoArguments && utils.isIteratingFunction() &&
!utils.hasParams()
) {
return;
}

utils.reportJSDoc(`Missing JSDoc @${targetTagName} declaration.`, null, () => {
if (!jsdoc.tags) {
jsdoc.tags = [];
Expand Down Expand Up @@ -78,6 +89,10 @@ export default iterateJsdoc(({
},
type: 'array',
},
exemptNoArguments: {
default: false,
type: 'boolean',
},
},
type: 'object',
},
Expand Down
43 changes: 43 additions & 0 deletions test/rules/assertions/requireExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ export default {
}
`,
},
{
code: `
/**
*
*/
function quux (someParam) {
}
`,
errors: [
{
message: 'Missing JSDoc @example declaration.',
},
],
options: [
{
exemptNoArguments: true,
},
],
output: `
/**
* @example
*/
function quux (someParam) {
}
`,
},
{
code: `/**
*
Expand Down Expand Up @@ -499,5 +527,20 @@ function quux () {
},
],
},
{
code: `
/**
*
*/
function quux () {
}
`,
options: [
{
exemptNoArguments: true,
},
],
},
],
};

0 comments on commit c750c86

Please sign in to comment.