Skip to content

Commit

Permalink
feat(require-returns): add optional fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Sep 13, 2023
1 parent 6f1b50b commit dfd2a8a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .README/rules/require-returns.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Will also report if multiple `@returns` tags are present.
`forceReturnsWithAsync` option is set with a present `@async` tag
(since we are not checking against the actual `return` values in these
cases).
- `enableFixer` - Whether to enable the fixer to add a blank `@returns`.
Defaults to `false`.

## Context and settings

Expand All @@ -51,7 +53,7 @@ Will also report if multiple `@returns` tags are present.
| Tags | `returns` |
| Aliases | `return` |
|Recommended|true|
| Options |`checkConstructors`, `checkGetters`, `contexts`, `exemptedBy`, `forceRequireReturn`, `forceReturnsWithAsync`|
| Options |`checkConstructors`, `checkGetters`, `contexts`, `enableFixer`, `exemptedBy`, `forceRequireReturn`, `forceReturnsWithAsync`|
| Settings | `ignoreReplacesDocs`, `overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs` |

## Failing examples
Expand Down
14 changes: 13 additions & 1 deletion docs/rules/require-returns.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Will also report if multiple `@returns` tags are present.
`forceReturnsWithAsync` option is set with a present `@async` tag
(since we are not checking against the actual `return` values in these
cases).
- `enableFixer` - Whether to enable the fixer to add a blank `@returns`.
Defaults to `false`.

<a name="user-content-require-returns-context-and-settings"></a>
<a name="require-returns-context-and-settings"></a>
Expand All @@ -61,7 +63,7 @@ Will also report if multiple `@returns` tags are present.
| Tags | `returns` |
| Aliases | `return` |
|Recommended|true|
| Options |`checkConstructors`, `checkGetters`, `contexts`, `exemptedBy`, `forceRequireReturn`, `forceReturnsWithAsync`|
| Options |`checkConstructors`, `checkGetters`, `contexts`, `enableFixer`, `exemptedBy`, `forceRequireReturn`, `forceReturnsWithAsync`|
| Settings | `ignoreReplacesDocs`, `overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs` |

<a name="user-content-require-returns-failing-examples"></a>
Expand All @@ -80,6 +82,16 @@ function quux (foo) {
}
// Message: Missing JSDoc @returns declaration.

/**
*
*/
function quux (foo) {

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

/**
*
*/
Expand Down
9 changes: 8 additions & 1 deletion src/rules/requireReturns.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default iterateJsdoc(({
}) => {
const {
contexts,
enableFixer = false,
forceRequireReturn = false,
forceReturnsWithAsync = false,
} = context.options[0] || {};
Expand Down Expand Up @@ -110,7 +111,9 @@ export default iterateJsdoc(({
};

if (shouldReport()) {
report(`Missing JSDoc @${tagName} declaration.`);
utils.reportJSDoc(`Missing JSDoc @${tagName} declaration.`, null, enableFixer ? () => {
utils.addTag(tagName);
} : null);
}
}, {
contextDefaults: true,
Expand All @@ -119,6 +122,7 @@ export default iterateJsdoc(({
description: 'Requires that returns are documented.',
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-returns.md#repos-sticky-header',
},
fixable: 'code',
schema: [
{
additionalProperties: false,
Expand Down Expand Up @@ -156,6 +160,9 @@ export default iterateJsdoc(({
},
type: 'array',
},
enableFixer: {
type: 'boolean',
},
exemptedBy: {
items: {
type: 'string',
Expand Down
32 changes: 32 additions & 0 deletions test/rules/assertions/requireReturns.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@ export default {
},
],
},
{
code: `
/**
*
*/
function quux (foo) {
return foo;
}
`,
errors: [
{
line: 2,
message: 'Missing JSDoc @returns declaration.',
},
],
options: [
{
enableFixer: true,
},
],
output: `
/**
*
* @returns
*/
function quux (foo) {
return foo;
}
`,
},
{
code: `
/**
Expand Down

0 comments on commit dfd2a8a

Please sign in to comment.