Skip to content

Commit 22dded1

Browse files
committed
feat(require-returns-check): with settings.jsdoc.mode (typescript or gcc), allow yieldAsReturn option as "always" or "argument" to treat yield as return (fixes #271)
1 parent 215fc28 commit 22dded1

File tree

8 files changed

+445
-35
lines changed

8 files changed

+445
-35
lines changed

Diff for: .README/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ You can then selectively add to or override the recommended rules.
8383

8484
## Settings
8585

86+
### Mode
87+
88+
While TypeScript and the Google Closure Compiler (GCC) support some jsdoc tags,
89+
there are some differences. The `settings.jsdoc.mode` lets you choose the precise
90+
mode (`"typescript"`, `"gcc"`, or `"jsdoc"` (the default)).
91+
8692
### Allow `@private` to disable rules for that comment block
8793

8894
- `settings.jsdoc.ignorePrivate` - Disables all rules for the comment block

Diff for: .README/rules/require-returns-check.md

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ Requires a return statement in function body if a `@returns` tag is specified in
44

55
Will also report if multiple `@returns` tags are present.
66

7+
#### Options
8+
9+
##### `yieldAsReturn`
10+
11+
If the setting `settings.jsdoc.mode` is set to `typescript` or `gcc`, this option
12+
will allow `yield` statements to be treated as `@returns` for the purposes of this
13+
rule. (If the mode is instead `jsdoc`, then this option will have no effect, since
14+
the jsdoc approach is to instead use the `@yields` tag.)
15+
16+
The allowable values are `"always"` which will treat any `yield` as a `return`,
17+
or `argument`, which will only treat a `yield` as a `return` when `yield` is
18+
followed by an argument.
19+
720
|||
821
|---|---|
922
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|

Diff for: README.md

+118-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ JSDoc linting rules for ESLint.
1212
* [Installation](#eslint-plugin-jsdoc-installation)
1313
* [Configuration](#eslint-plugin-jsdoc-configuration)
1414
* [Settings](#eslint-plugin-jsdoc-settings)
15+
* [Mode](#eslint-plugin-jsdoc-settings-mode)
1516
* [Allow `@private` to disable rules for that comment block](#eslint-plugin-jsdoc-settings-allow-private-to-disable-rules-for-that-comment-block)
1617
* [Alias Preference](#eslint-plugin-jsdoc-settings-alias-preference)
1718
* [`@override`/`@augments`/`@extends`/`@implements` Without Accompanying `@param`/`@description`/`@example`/`@returns`](#eslint-plugin-jsdoc-settings-override-augments-extends-implements-without-accompanying-param-description-example-returns)
@@ -122,6 +123,13 @@ You can then selectively add to or override the recommended rules.
122123
<a name="eslint-plugin-jsdoc-settings"></a>
123124
## Settings
124125

126+
<a name="eslint-plugin-jsdoc-settings-mode"></a>
127+
### Mode
128+
129+
While TypeScript and the Google Closure Compiler (GCC) support some jsdoc tags,
130+
there are some differences. The `settings.jsdoc.mode` lets you choose the precise
131+
mode (`"typescript"`, `"gcc"`, or `"jsdoc"` (the default)).
132+
125133
<a name="eslint-plugin-jsdoc-settings-allow-private-to-disable-rules-for-that-comment-block"></a>
126134
### Allow <code>@private</code> to disable rules for that comment block
127135

@@ -6324,6 +6332,21 @@ Requires a return statement in function body if a `@returns` tag is specified in
63246332

63256333
Will also report if multiple `@returns` tags are present.
63266334

6335+
<a name="eslint-plugin-jsdoc-rules-require-returns-check-options-12"></a>
6336+
#### Options
6337+
6338+
<a name="eslint-plugin-jsdoc-rules-require-returns-check-options-12-yieldasreturn"></a>
6339+
##### <code>yieldAsReturn</code>
6340+
6341+
If the setting `settings.jsdoc.mode` is set to `typescript` or `gcc`, this option
6342+
will allow `yield` statements to be treated as `@returns` for the purposes of this
6343+
rule. (If the mode is instead `jsdoc`, then this option will have no effect, since
6344+
the jsdoc approach is to instead use the `@yields` tag.)
6345+
6346+
The allowable values are `"always"` which will treat any `yield` as a `return`,
6347+
or `argument`, which will only treat a `yield` as a `return` when `yield` is
6348+
followed by an argument.
6349+
63276350
|||
63286351
|---|---|
63296352
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
@@ -6394,6 +6417,59 @@ function quux () {
63946417
}
63956418
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
63966419
// Message: Unexpected tag `@returns`
6420+
6421+
/**
6422+
*
6423+
* @returns {boolean}
6424+
*/
6425+
function * quux () {
6426+
}
6427+
// Settings: {"jsdoc":{"mode":"typescript"}}
6428+
// Options: [{"yieldAsReturn":"always"}]
6429+
// Message: JSDoc @returns declaration present but return expression not available in function.
6430+
6431+
/**
6432+
*
6433+
* @returns {boolean}
6434+
*/
6435+
function * quux () {
6436+
}
6437+
// Settings: {"jsdoc":{"mode":"typescript"}}
6438+
// Options: [{"yieldAsReturn":"argument"}]
6439+
// Message: JSDoc @returns declaration present but return expression not available in function.
6440+
6441+
/**
6442+
*
6443+
* @returns {boolean}
6444+
*/
6445+
function * quux () {
6446+
yield;
6447+
}
6448+
// Settings: {"jsdoc":{"mode":"typescript"}}
6449+
// Options: [{"yieldAsReturn":"argument"}]
6450+
// Message: JSDoc @returns declaration present but return expression not available in function.
6451+
6452+
/**
6453+
*
6454+
* @returns {boolean}
6455+
*/
6456+
function * quux () {
6457+
var a = 5, b = yield;
6458+
}
6459+
// Settings: {"jsdoc":{"mode":"typescript"}}
6460+
// Options: [{"yieldAsReturn":"argument"}]
6461+
// Message: JSDoc @returns declaration present but return expression not available in function.
6462+
6463+
/**
6464+
*
6465+
* @returns {boolean}
6466+
*/
6467+
function * quux () {
6468+
yield true;
6469+
}
6470+
// Settings: {"jsdoc":{"mode":"jsdoc"}}
6471+
// Options: [{"yieldAsReturn":"always"}]
6472+
// Message: JSDoc @returns declaration present but return expression not available in function.
63976473
````
63986474

63996475
The following patterns are not considered problems:
@@ -6650,6 +6726,46 @@ function quux () {
66506726
}
66516727
return;
66526728
}
6729+
6730+
/**
6731+
*
6732+
* @returns {boolean}
6733+
*/
6734+
function * quux () {
6735+
yield true;
6736+
}
6737+
// Settings: {"jsdoc":{"mode":"typescript"}}
6738+
// Options: [{"yieldAsReturn":"always"}]
6739+
6740+
/**
6741+
*
6742+
* @returns {boolean}
6743+
*/
6744+
function * quux () {
6745+
return true;
6746+
}
6747+
// Settings: {"jsdoc":{"mode":"typescript"}}
6748+
// Options: [{"yieldAsReturn":"always"}]
6749+
6750+
/**
6751+
*
6752+
* @returns {boolean}
6753+
*/
6754+
function * quux () {
6755+
yield true;
6756+
}
6757+
// Settings: {"jsdoc":{"mode":"typescript"}}
6758+
// Options: [{"yieldAsReturn":"argument"}]
6759+
6760+
/**
6761+
*
6762+
* @returns {boolean}
6763+
*/
6764+
function * quux () {
6765+
var a = 5, b = yield true;
6766+
}
6767+
// Settings: {"jsdoc":{"mode":"typescript"}}
6768+
// Options: [{"yieldAsReturn":"argument"}]
66536769
````
66546770

66556771

@@ -6795,7 +6911,7 @@ Requires returns are documented.
67956911

67966912
Will also report if multiple `@returns` tags are present.
67976913

6798-
<a name="eslint-plugin-jsdoc-rules-require-returns-options-12"></a>
6914+
<a name="eslint-plugin-jsdoc-rules-require-returns-options-13"></a>
67996915
#### Options
68006916

68016917
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the document
@@ -7251,7 +7367,7 @@ Also impacts behaviors on namepath (or event)-defining and pointing tags:
72517367
allow `#`, `.`, or `~` at the end (which is not allowed at the end of
72527368
normal paths).
72537369

7254-
<a name="eslint-plugin-jsdoc-rules-valid-types-options-13"></a>
7370+
<a name="eslint-plugin-jsdoc-rules-valid-types-options-14"></a>
72557371
#### Options
72567372

72577373
- `allowEmptyNamepaths` (default: true) - Set to `false` to disallow

Diff for: src/iterateJsdoc.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ const getUtils = (
201201
return jsdocUtils.hasDefinedTypeReturnTag(tag);
202202
};
203203

204-
utils.hasReturnValue = (ignoreAsync = false) => {
205-
return jsdocUtils.hasReturnValue(node, context, ignoreAsync);
204+
utils.hasReturnValue = ({ignoreAsync = false, yieldAsReturn} = {}) => {
205+
return jsdocUtils.hasReturnValue(node, context, {
206+
ignoreAsync,
207+
yieldAsReturn
208+
});
206209
};
207210

208211
utils.isAsync = () => {
@@ -319,6 +322,9 @@ const getSettings = (context) => {
319322
settings.implementsReplacesDocs = _.get(context, 'settings.jsdoc.implementsReplacesDocs');
320323
settings.augmentsExtendsReplacesDocs = _.get(context, 'settings.jsdoc.augmentsExtendsReplacesDocs');
321324

325+
// currently `require-returns-check` only, but to add to other rules
326+
settings.mode = _.get(context, 'settings.jsdoc.mode');
327+
322328
return settings;
323329
};
324330

0 commit comments

Comments
 (0)