-
-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rule require-throws
#574
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b479a04
wip: require-throws
0xCLARITY d7b2299
more tests
0xCLARITY a671feb
Merge branch 'master' into require-throws
brettz9 2e3a6ba
- Add with `tagNamePreference` and `throws` set to `false` to restore…
brettz9 31029c6
Add require throws documentation
0xCLARITY cfe249d
- Doc cleanup / lbs
brettz9 edf939c
- Also exclude expectation of docs with `@type` tag
brettz9 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,54 @@ | ||
### `require-returns` | ||
|
||
Requires returns are documented. | ||
Requires that returns are documented. | ||
|
||
Will also report if multiple `@returns` tags are present. | ||
|
||
#### Options | ||
|
||
- `checkConstructors` - A value indicating whether `constructor`s should | ||
be checked for `@returns` tags. Defaults to `false`. | ||
be checked for `@returns` tags. Defaults to `false`. | ||
- `checkGetters` - Boolean to determine whether getter methods should | ||
be checked for `@returns` tags. Defaults to `true`. | ||
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the document | ||
block avoids the need for a `@returns`. Defaults to an array with | ||
`inheritdoc`. If you set this array, it will overwrite the default, | ||
be checked for `@returns` tags. Defaults to `true`. | ||
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the | ||
document block avoids the need for a `@returns`. Defaults to an array | ||
with `inheritdoc`. If you set this array, it will overwrite the default, | ||
so be sure to add back `inheritdoc` if you wish its presence to cause | ||
exemption of the rule. | ||
- `forceRequireReturn` - Set to `true` to always insist on | ||
`@returns` documentation regardless of implicit or explicit `return`'s | ||
in the function. May be desired to flag that a project is aware of an | ||
`undefined`/`void` return. Defaults to `false`. | ||
`@returns` documentation regardless of implicit or explicit `return`'s | ||
in the function. May be desired to flag that a project is aware of an | ||
`undefined`/`void` return. Defaults to `false`. | ||
- `forceReturnsWithAsync` - By default `async` functions that do not explicitly | ||
return a value pass this rule as an `async` function will always return a | ||
`Promise`, even if the `Promise` resolves to void. You can force all `async` | ||
functions to require return statements by setting `forceReturnsWithAsync` | ||
to `true` on the options object. This may be useful for flagging that there | ||
has been consideration of return type. Defaults to `false`. | ||
return a value pass this rule as an `async` function will always return a | ||
`Promise`, even if the `Promise` resolves to void. You can force all | ||
`async` functions to require return statements by setting | ||
`forceReturnsWithAsync` to `true` on the options object. This may be useful | ||
for flagging that there has been consideration of return type. Defaults | ||
to `false`. | ||
- `contexts` - Set this to an array of strings representing the AST context | ||
where you wish the rule to be applied. | ||
Overrides the default contexts (see below). Set to `"any"` if you want | ||
the rule to apply to any jsdoc block throughout your files (as is necessary | ||
for finding function blocks not attached to a function declaration or | ||
expression, i.e., `@callback` or `@function` (or its aliases `@func` or | ||
`@method`) (including those associated with an `@interface`). This | ||
rule will only apply on non-default contexts when there is such a tag | ||
present and the `forceRequireReturn` option is set or if the | ||
`forceReturnsWithAsync` option is set with a present `@async` tag | ||
(since we are not checking against the actual `return` values in these | ||
cases). | ||
where you wish the rule to be applied. | ||
Overrides the default contexts (see below). Set to `"any"` if you want | ||
the rule to apply to any jsdoc block throughout your files (as is necessary | ||
for finding function blocks not attached to a function declaration or | ||
expression, i.e., `@callback` or `@function` (or its aliases `@func` or | ||
`@method`) (including those associated with an `@interface`). This | ||
rule will only apply on non-default contexts when there is such a tag | ||
present and the `forceRequireReturn` option is set or if the | ||
`forceReturnsWithAsync` option is set with a present `@async` tag | ||
(since we are not checking against the actual `return` values in these | ||
cases). | ||
|
||
```js | ||
'jsdoc/require-returns': ['error', {forceReturnsWithAsync: true}] | ||
``` | ||
|
||
| | | | ||
| -------- | ------------------------------------------------------------------------------------------------------------- | | ||
| | | | ||
| -------- | ------- | | ||
| Context | `ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled | | ||
| Tags | `returns` | | ||
| Aliases | `return` | | ||
| Options | `checkConstructors`, `checkGetters`, `contexts`, `exemptedBy`, `forceRequireReturn`, `forceReturnsWithAsync` | | ||
| Settings | `overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs` | | ||
| Tags | `returns` | | ||
| Aliases | `return` | | ||
| Options | `checkConstructors`, `checkGetters`, `contexts`, `exemptedBy`, `forceRequireReturn`, `forceReturnsWithAsync` | | ||
| Settings | `overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs` | | ||
|
||
<!-- assertions requireReturns --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
### `require-throws` | ||
|
||
Requires that throw statements are documented. | ||
|
||
#### Options | ||
|
||
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the | ||
document block avoids the need for a `@throws`. Defaults to an array | ||
with `inheritdoc`. If you set this array, it will overwrite the default, | ||
so be sure to add back `inheritdoc` if you wish its presence to cause | ||
exemption of the rule. | ||
- `contexts` - Set this to an array of strings representing the AST context | ||
where you wish the rule to be applied. | ||
Overrides the default contexts (see below). Set to `"any"` if you want | ||
the rule to apply to any jsdoc block throughout your files (as is necessary | ||
for finding function blocks not attached to a function declaration or | ||
expression, i.e., `@callback` or `@function` (or its aliases `@func` or | ||
`@method`) (including those associated with an `@interface`). | ||
|
||
```js | ||
'jsdoc/require-throws': 'error', | ||
``` | ||
|
||
| | | | ||
| -------- | --- | | ||
| Context | `ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled | | ||
| Tags | `throws` | | ||
| Aliases | `exception` | | ||
| Options | `contexts`, `exemptedBy` | | ||
| Settings | `overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs` | | ||
|
||
<!-- assertions requireThrows --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import iterateJsdoc from '../iterateJsdoc'; | ||
|
||
/** | ||
* We can skip checking for a throws value, in case the documentation is inherited | ||
* or the method is either a constructor or an abstract method. | ||
* | ||
* @param {*} utils | ||
* a reference to the utils which are used to probe if a tag is present or not. | ||
* @returns {boolean} | ||
* true in case deep checking can be skipped; otherwise false. | ||
*/ | ||
const canSkip = (utils) => { | ||
return utils.hasATag([ | ||
// inheritdoc implies that all documentation is inherited | ||
// see https://jsdoc.app/tags-inheritdoc.html | ||
// | ||
// Abstract methods are by definition incomplete, | ||
// so it is not necessary to document that they throw an error. | ||
'abstract', | ||
'virtual', | ||
|
||
// The designated type can itself document `@throws` | ||
'type', | ||
]) || | ||
utils.avoidDocs(); | ||
}; | ||
|
||
export default iterateJsdoc(({ | ||
report, | ||
utils, | ||
}) => { | ||
// A preflight check. We do not need to run a deep check for abstract | ||
// functions. | ||
if (canSkip(utils)) { | ||
return; | ||
} | ||
|
||
const tagName = utils.getPreferredTagName({tagName: 'throws'}); | ||
if (!tagName) { | ||
return; | ||
} | ||
|
||
const tags = utils.getTags(tagName); | ||
const iteratingFunction = utils.isIteratingFunction(); | ||
|
||
// In case the code returns something, we expect a return value in JSDoc. | ||
const [tag] = tags; | ||
const missingThrowsTag = typeof tag === 'undefined' || tag === null; | ||
|
||
const shouldReport = () => { | ||
if (!missingThrowsTag) { | ||
return false; | ||
} | ||
|
||
return iteratingFunction && utils.hasThrowValue(); | ||
}; | ||
|
||
if (shouldReport()) { | ||
report(`Missing JSDoc @${tagName} declaration.`); | ||
} | ||
}, { | ||
contextDefaults: true, | ||
meta: { | ||
schema: [ | ||
{ | ||
additionalProperties: false, | ||
properties: { | ||
contexts: { | ||
items: { | ||
type: 'string', | ||
}, | ||
type: 'array', | ||
}, | ||
exemptedBy: { | ||
items: { | ||
type: 'string', | ||
}, | ||
type: 'array', | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to cover this case in a test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be enough to add a (passing) example which doesn't have
@throws
at all (or where it has@throws
butsettings.jsdoc.tagNamePreference
is set to mapthrows
to something else likethrow
and if@throw
wasn't present).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only line that I can't figure out how to cover. I feel like it should be covered by this test, but it seems like I'm not understanding how
utils.getPreferredTagName()
is working?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, I've added the test (it was setting
throws
tofalse
ontagNamePreference
settings, i.e., if someone had indicated they wished to block thethrows
tag yet were still using this rule. :)I think it looked good, but need a little more time to give it another look over before merging.