Skip to content

Commit

Permalink
fix: add enableFixer option to require-example
Browse files Browse the repository at this point in the history
- Add test coverage for option
- Add option to rule
- Wrap fixer logic in a conditional
- Update readme with new option
- Fix `require-example` readme options list to match current implementation
  • Loading branch information
arinthros authored and brettz9 committed Apr 8, 2022
1 parent be3af9d commit 7416331
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
7 changes: 6 additions & 1 deletion .README/rules/require-example.md
Expand Up @@ -47,6 +47,11 @@ A value indicating whether getters should be checked. Defaults to `false`.

A value indicating whether setters should be checked. Defaults to `false`.

##### `enableFixer`

A boolean on whether to enable the fixer (which adds an empty `@example` block).
Defaults to `true`.

#### Fixer

The fixer for `require-example` will add an empty `@example`, but it will still
Expand All @@ -57,7 +62,7 @@ report a missing example description after this is added.
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
|Tags|`example`|
|Recommended|false|
|Options|`exemptedBy`, `exemptNoArguments`, `avoidExampleOnConstructors`, `contexts`|
|Options|`exemptedBy`, `exemptNoArguments`, `contexts`, `checkConstructors`, `checkGetters`, `checkSetters`, `enableFixer`|
|Settings|`ignoreReplacesDocs`, `overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|

<!-- assertions requireExample -->
35 changes: 30 additions & 5 deletions README.md
Expand Up @@ -11939,6 +11939,13 @@ A value indicating whether getters should be checked. Defaults to `false`.

A value indicating whether setters should be checked. Defaults to `false`.

<a name="user-content-eslint-plugin-jsdoc-rules-require-example-options-25-enablefixer-2"></a>
<a name="eslint-plugin-jsdoc-rules-require-example-options-25-enablefixer-2"></a>
##### <code>enableFixer</code>

A boolean on whether to enable the fixer (which adds an empty `@example` block).
Defaults to `true`.

<a name="user-content-eslint-plugin-jsdoc-rules-require-example-fixer"></a>
<a name="eslint-plugin-jsdoc-rules-require-example-fixer"></a>
#### Fixer
Expand All @@ -11951,7 +11958,7 @@ report a missing example description after this is added.
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
|Tags|`example`|
|Recommended|false|
|Options|`exemptedBy`, `exemptNoArguments`, `avoidExampleOnConstructors`, `contexts`|
|Options|`exemptedBy`, `exemptNoArguments`, `contexts`, `checkConstructors`, `checkGetters`, `checkSetters`, `enableFixer`|
|Settings|`ignoreReplacesDocs`, `overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|

The following patterns are considered problems:
Expand Down Expand Up @@ -12065,6 +12072,24 @@ class TestClass {
}
// "jsdoc/require-example": ["error"|"warn", {"checkSetters":true}]
// Message: Missing JSDoc @example description.

/**
*
*/
function quux (someParam) {

}
// "jsdoc/require-example": ["error"|"warn", {"enableFixer":true}]
// Message: Missing JSDoc @example declaration.

/**
*
*/
function quux (someParam) {

}
// "jsdoc/require-example": ["error"|"warn", {"enableFixer":false}]
// Message: Missing JSDoc @example declaration.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -12852,8 +12877,8 @@ setters should be checked but only when there is no getter. This may be useful
if one only wishes documentation on one of the two accessors. Defaults to
`false`.

<a name="user-content-eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-2"></a>
<a name="eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-2"></a>
<a name="user-content-eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-3"></a>
<a name="eslint-plugin-jsdoc-rules-require-jsdoc-options-28-enablefixer-3"></a>
##### <code>enableFixer</code>

A boolean on whether to enable the fixer (which adds an empty jsdoc block).
Expand Down Expand Up @@ -15030,8 +15055,8 @@ function signature, it may appear that there is an actual property named

An options object accepts the following optional properties:

<a name="user-content-eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-3"></a>
<a name="eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-3"></a>
<a name="user-content-eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-4"></a>
<a name="eslint-plugin-jsdoc-rules-require-param-options-32-enablefixer-4"></a>
##### <code>enableFixer</code>

Whether to enable the fixer. Defaults to `true`.
Expand Down
9 changes: 8 additions & 1 deletion src/rules/requireExample.js
Expand Up @@ -11,6 +11,7 @@ export default iterateJsdoc(({
}

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

Expand All @@ -30,7 +31,9 @@ export default iterateJsdoc(({
}

utils.reportJSDoc(`Missing JSDoc @${targetTagName} declaration.`, null, () => {
utils.addTag(targetTagName);
if (enableFixer) {
utils.addTag(targetTagName);
}
});

return;
Expand Down Expand Up @@ -92,6 +95,10 @@ export default iterateJsdoc(({
},
type: 'array',
},
enableFixer: {
default: true,
type: 'boolean',
},
exemptedBy: {
items: {
type: 'string',
Expand Down
59 changes: 59 additions & 0 deletions test/rules/assertions/requireExample.js
Expand Up @@ -326,6 +326,65 @@ function quux () {
},
],
},
{
code: `
/**
*
*/
function quux (someParam) {
}
`,
errors: [
{
line: 2,
message: 'Missing JSDoc @example declaration.',
},
],
options: [
{
enableFixer: true,
},
],
output: `
/**
*
* @example
*/
function quux (someParam) {
}
`,
},
{
code: `
/**
*
*/
function quux (someParam) {
}
`,
errors: [
{
line: 2,
message: 'Missing JSDoc @example declaration.',
},
],
options: [
{
enableFixer: false,
},
],
output: `
/**
*
*/
function quux (someParam) {
}
`,
},
],
valid: [
{
Expand Down

0 comments on commit 7416331

Please sign in to comment.