Skip to content

Commit b2cfa51

Browse files
committed
refactor: make forceReturnsWithAsync an option
1 parent cf9521b commit b2cfa51

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

.README/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ only (e.g., to match `Array` if the type is `Array` vs. `Array.<string>`).
296296
`@returns` documentation regardless of implicit or explicit `return`'s
297297
in the function. May be desired to flag that a project is aware of an
298298
`undefined`/`void` return.
299-
* `settings.jsdoc.forceReturnsWithAsync` - Set to `true` to always insist on
300-
`@returns` documentation regardless of implicit or explicit `return`'s
301-
in an async function. May be desired to flag that a project is aware of a
302-
`Promise<void>` return.
303299

304300
### Settings to Configure `require-example`
305301

.README/rules/require-returns.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
Requires returns are documented.
44

5+
By default `async` functions that do not explicitly return a value pass this rule. You can force all `async` functions to require return statements by setting `forceReturnsWithAsync` as true on the options object. This maybe useful as an `async` function will always return a Promise, even if the Promise returns void.
6+
7+
```js
8+
'jsdoc/require-jsdoc': ['error', {forceReturnsWithAsync: true}]
9+
```
10+
511
|||
612
|---|---|
713
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
814
|Tags|`returns`|
915
|Aliases|`return`|
10-
|Settings|`forceRequireReturn`, `forceReturnsWithAsync`|
16+
|Settings|`forceRequireReturn`|
17+
|Options|`forceReturnsWithAsync`|
1118

1219
<!-- assertions requireReturns -->

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,6 @@ only (e.g., to match `Array` if the type is `Array` vs. `Array.<string>`).
351351
`@returns` documentation regardless of implicit or explicit `return`'s
352352
in the function. May be desired to flag that a project is aware of an
353353
`undefined`/`void` return.
354-
* `settings.jsdoc.forceReturnsWithAsync` - Set to `true` to always insist on
355-
`@returns` documentation regardless of implicit or explicit `return`'s
356-
in an async function. May be desired to flag that a project is aware of a
357-
`Promise<void>` return.
358354

359355
<a name="eslint-plugin-jsdoc-settings-settings-to-configure-require-example"></a>
360356
### Settings to Configure <code>require-example</code>
@@ -4568,12 +4564,19 @@ function quux () {
45684564

45694565
Requires returns are documented.
45704566

4567+
By default `async` functions that do not explicitly return a value pass this rule. You can force all `async` functions to require return statements by setting `forceReturnsWithAsync` as true on the options object. This maybe useful as an `async` function will always return a Promise, even if the Promise returns void.
4568+
4569+
```js
4570+
'jsdoc/require-jsdoc': ['error', {forceReturnsWithAsync: true}]
4571+
```
4572+
45714573
|||
45724574
|---|---|
45734575
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
45744576
|Tags|`returns`|
45754577
|Aliases|`return`|
4576-
|Settings|`forceRequireReturn`, `forceReturnsWithAsync`|
4578+
|Settings|`forceRequireReturn`|
4579+
|Options|`forceReturnsWithAsync`|
45774580

45784581
The following patterns are considered problems:
45794582

@@ -4638,14 +4641,15 @@ const quux = async function () {}
46384641
async function quux () {
46394642
}
46404643
// Settings: {"jsdoc":{"forceRequireReturn":true}}
4644+
// Options: [{"forceReturnsWithAsync":true}]
46414645
// Message: Missing JSDoc @returns declaration.
46424646

46434647
/**
46444648
*
46454649
*/
46464650
function quux () {
46474651
}
4648-
// Settings: {"jsdoc":{"forceReturnsWithAsync":true}}
4652+
// Options: [{"forceReturnsWithAsync":true}]
46494653
// Message: Missing JSDoc @returns declaration.
46504654

46514655
const language = {
@@ -4886,7 +4890,7 @@ async function quux () {
48864890
*/
48874891
async function quux () {
48884892
}
4889-
// Settings: {"jsdoc":{"forceReturnsWithAsync":true}}
4893+
// Options: [{"forceReturnsWithAsync":true}]
48904894

48914895
/**
48924896
*

src/iterateJsdoc.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ const curryUtils = (
4646
allowAugmentsExtendsWithoutParam,
4747
checkSeesForNamepaths,
4848
forceRequireReturn,
49-
forceReturnsWithAsync,
5049
avoidExampleOnConstructors,
5150
ancestors,
5251
sourceCode,
@@ -218,10 +217,6 @@ const curryUtils = (
218217
return forceRequireReturn;
219218
};
220219

221-
utils.isForceReturnsWithAsync = () => {
222-
return forceReturnsWithAsync;
223-
};
224-
225220
utils.filterTags = (filter) => {
226221
return (jsdoc.tags || []).filter(filter);
227222
};
@@ -321,7 +316,6 @@ export default (iterator, opts = {}) => {
321316

322317
// `require-returns` only
323318
const forceRequireReturn = Boolean(_.get(context, 'settings.jsdoc.forceRequireReturn'));
324-
const forceReturnsWithAsync = Boolean(_.get(context, 'settings.jsdoc.forceReturnsWithAsync'));
325319

326320
// `require-example` only
327321
const avoidExampleOnConstructors = Boolean(_.get(context, 'settings.jsdoc.avoidExampleOnConstructors'));
@@ -398,7 +392,6 @@ export default (iterator, opts = {}) => {
398392
allowAugmentsExtendsWithoutParam,
399393
checkSeesForNamepaths,
400394
forceRequireReturn,
401-
forceReturnsWithAsync,
402395
avoidExampleOnConstructors,
403396
ancestors,
404397
sourceCode

src/rules/requireReturns.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ const canSkip = (utils) => {
3939

4040
export default iterateJsdoc(({
4141
report,
42-
utils
42+
utils,
43+
context
4344
}) => {
4445
// A preflight check. We do not need to run a deep check
4546
// in case the @returns comment is optional or undefined.
4647
if (canSkip(utils)) {
4748
return;
4849
}
4950

51+
const options = context.options[0] || {};
52+
5053
const tagName = utils.getPreferredTagName('returns');
5154
const tags = utils.getTags(tagName);
5255

@@ -58,7 +61,7 @@ export default iterateJsdoc(({
5861
const [tag] = tags;
5962
const missingReturnTag = typeof tag === 'undefined' || tag === null;
6063
if (missingReturnTag &&
61-
((utils.isAsync() && !utils.hasReturnValue(true) ? utils.isForceReturnsWithAsync() : utils.hasReturnValue()) || utils.isForceRequireReturn())
64+
((utils.isAsync() && !utils.hasReturnValue(true) ? Boolean(options.forceReturnsWithAsync) : utils.hasReturnValue()) || utils.isForceRequireReturn())
6265
) {
6366
report('Missing JSDoc @' + tagName + ' declaration.');
6467
}

test/rules/assertions/requireReturns.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ export default {
135135
/**
136136
*
137137
*/
138-
async function quux () {
139-
}
138+
const quux = async () => {}
140139
`,
141140
errors: [
142141
{
@@ -169,7 +168,7 @@ export default {
169168
],
170169
settings: {
171170
jsdoc: {
172-
forceReturnsWithAsync: true
171+
forceRequireReturn: true
173172
}
174173
}
175174
},
@@ -205,13 +204,11 @@ export default {
205204
message: 'Missing JSDoc @returns declaration.'
206205
}
207206
],
207+
options: [{
208+
forceReturnsWithAsync: true
209+
}],
208210
parserOptions: {
209211
ecmaVersion: 8
210-
},
211-
settings: {
212-
jsdoc: {
213-
forceReturnsWithAsync: true
214-
}
215212
}
216213
}
217214
],
@@ -552,13 +549,11 @@ export default {
552549
async function quux () {
553550
}
554551
`,
552+
options: [{
553+
forceReturnsWithAsync: true
554+
}],
555555
parserOptions: {
556556
ecmaVersion: 8
557-
},
558-
settings: {
559-
jsdoc: {
560-
forceReturnsWithAsync: true
561-
}
562557
}
563558
},
564559
{

0 commit comments

Comments
 (0)