Skip to content

feat(match-description): ensure @desc checked by default; check more desc-like tags by default; check non-empty tags #1106

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

Merged
merged 5 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions .README/rules/match-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ by our supported Node versions):

``^\n?([A-Z`\\d_][\\s\\S]*[.?!`]\\s*)?$``

Applies to the jsdoc block description and `@description` (or `@desc`)
by default but the `tags` option (see below) may be used to match other tags.
Applies by default to the jsdoc block description and to the following tags:

- `@description`/`@desc`
- `@summary`
- `@file`/`@fileoverview`/`@overview`
- `@classdesc`

In addition, the `tags` option (see below) may be used to match other tags.

The default (and all regex options) defaults to using (only) the `u` flag, so
to add your own flags, encapsulate your expression as a string, but like a
Expand Down Expand Up @@ -54,6 +60,21 @@ You may provide a custom default message by using the following format:
This can be overridden per tag or for the main block description by setting
`message` within `tags` or `mainDescription`, respectively.

### `nonemptyTags`

If not set to `false`, will enforce that the following tags have at least
some content:

- `@copyright`
- `@example`
- `@see`
- `@todo`
- `@throws`/`@exception`
- `@yields`/`@yield`

If you supply your own tag description for any of the above tags in `tags`,
your description will take precedence.

### `tags`

If you want different regular expressions to apply to tags, you may use
Expand Down Expand Up @@ -161,7 +182,7 @@ section of our README for more on the expected format.
|Aliases|`@desc`|
|Recommended|false|
|Settings||
|Options|`contexts`, `mainDescription`, `matchDescription`, `message`, `tags`|
|Options|`contexts`, `mainDescription`, `matchDescription`, `message`, `nonemptyTags`, `tags`|

## Failing examples

Expand Down
3 changes: 2 additions & 1 deletion .README/rules/require-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

{"gitdown": "contents", "rootId": "require-description"}

Requires that all functions have a description.
Requires that all functions (or optionally other structures) with a JSDoc block
have a description.

* All functions must have an implicit description (e.g., text above tags) or
have the option `descriptionStyle` set to `tag` (requiring `@description`
Expand Down
53 changes: 42 additions & 11 deletions docs/rules/match-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [Options](#user-content-match-description-options)
* [`matchDescription`](#user-content-match-description-options-matchdescription)
* [`message`](#user-content-match-description-options-message)
* [`nonemptyTags`](#user-content-match-description-options-nonemptytags)
* [`tags`](#user-content-match-description-options-tags)
* [`mainDescription`](#user-content-match-description-options-maindescription)
* [`contexts`](#user-content-match-description-options-contexts)
Expand All @@ -21,8 +22,14 @@ by our supported Node versions):

``^\n?([A-Z`\\d_][\\s\\S]*[.?!`]\\s*)?$``

Applies to the jsdoc block description and `@description` (or `@desc`)
by default but the `tags` option (see below) may be used to match other tags.
Applies by default to the jsdoc block description and to the following tags:

- `@description`/`@desc`
- `@summary`
- `@file`/`@fileoverview`/`@overview`
- `@classdesc`

In addition, the `tags` option (see below) may be used to match other tags.

The default (and all regex options) defaults to using (only) the `u` flag, so
to add your own flags, encapsulate your expression as a string, but like a
Expand Down Expand Up @@ -71,6 +78,23 @@ You may provide a custom default message by using the following format:
This can be overridden per tag or for the main block description by setting
`message` within `tags` or `mainDescription`, respectively.

<a name="user-content-match-description-options-nonemptytags"></a>
<a name="match-description-options-nonemptytags"></a>
### <code>nonemptyTags</code>

If not set to `false`, will enforce that the following tags have at least
some content:

- `@copyright`
- `@example`
- `@see`
- `@todo`
- `@throws`/`@exception`
- `@yields`/`@yield`

If you supply your own tag description for any of the above tags in `tags`,
your description will take precedence.

<a name="user-content-match-description-options-tags"></a>
<a name="match-description-options-tags"></a>
### <code>tags</code>
Expand Down Expand Up @@ -186,7 +210,7 @@ section of our README for more on the expected format.
|Aliases|`@desc`|
|Recommended|false|
|Settings||
|Options|`contexts`, `mainDescription`, `matchDescription`, `message`, `tags`|
|Options|`contexts`, `mainDescription`, `matchDescription`, `message`, `nonemptyTags`, `tags`|

<a name="user-content-match-description-failing-examples"></a>
<a name="match-description-failing-examples"></a>
Expand Down Expand Up @@ -335,7 +359,6 @@ function quux (foo) {
function quux () {

}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"summary":true}}]
// Message: JSDoc description does not satisfy the regex pattern.

/**
Expand Down Expand Up @@ -368,7 +391,6 @@ function quux () {
function quux (foo) {

}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"description":true}}]
// Message: JSDoc description does not satisfy the regex pattern.

/**
Expand Down Expand Up @@ -602,6 +624,13 @@ function quux () {
function foo(): string;
// "jsdoc/match-description": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock[endLine=0]"}],"matchDescription":"^\\S[\\s\\S]*\\S$"}]
// Message: JSDoc description does not satisfy the regex pattern.

/**
* @copyright
*/
function quux () {
}
// Message: JSDoc description must not be empty.
````


Expand Down Expand Up @@ -722,7 +751,6 @@ function quux () {
function quux () {

}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"description":true}}]

/**
* @description Foo
Expand All @@ -732,13 +760,11 @@ function quux () {
function quux () {

}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"description":true}}]

/** @description Foo bar. */
function quux () {

}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"description":true}}]

/**
* @description Foo
Expand All @@ -747,7 +773,6 @@ function quux () {
function quux () {

}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"description":true}}]

/**
* Foo. {@see Math.sin}.
Expand Down Expand Up @@ -872,7 +897,7 @@ const q = {
// "jsdoc/match-description": ["error"|"warn", {"contexts":[]}]

/**
* @description foo.
* @deprecated foo.
*/
function quux () {

Expand All @@ -887,7 +912,6 @@ function quux () {
function quux () {

}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"summary":true}}]

/**
* Foo.
Expand Down Expand Up @@ -975,5 +999,12 @@ function foo(): string;
*/
function foo(): void;
// "jsdoc/match-description": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock[endLine!=0]:not(:has(JsdocTag))"}],"matchDescription":"^\\S[\\s\\S]*\\S$"}]

/**
* @copyright
*/
function quux () {
}
// "jsdoc/match-description": ["error"|"warn", {"nonemptyTags":false}]
````

6 changes: 6 additions & 0 deletions docs/rules/match-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ function quux () {}
*/
// "jsdoc/match-name": ["error"|"warn", {"match":[{"disallowName":"/^opt_/i","replacement":""}]}]
// Message: Only allowing names not matching `/^opt_/i` but found "opt_a".

/**
* @template
*/
// "jsdoc/match-name": ["error"|"warn", {"match":[{"disallowName":"/^$/","tags":["template"]}]}]
// Message: Only allowing names not matching `/^$/u` but found "".
````


Expand Down
6 changes: 6 additions & 0 deletions docs/rules/no-restricted-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,5 +373,11 @@ class Test {
abstract Test(): void;
}
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))","context":"TSEmptyBodyFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]","message":"methods with non-void return types must have a @returns tag"}]}]

/**
* @private
*/
function quux () {}
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(JsdocBlock:has(JsdocTag[tag=/private|protected|public/]))","context":"any","message":"Access modifier tags must be present"}]}]
````

3 changes: 2 additions & 1 deletion docs/rules/require-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* [Passing examples](#user-content-require-description-passing-examples)


Requires that all functions have a description.
Requires that all functions (or optionally other structures) with a JSDoc block
have a description.

* All functions must have an implicit description (e.g., text above tags) or
have the option `descriptionStyle` set to `tag` (requiring `@description`
Expand Down
4 changes: 4 additions & 0 deletions src/getDefaultTagStructureForMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ const getDefaultTagStructureForMode = (mode) => {
'namepathRole', isJsdoc ? 'text' : 'namepath-referencing',
],

[
'nameRequired', !isJsdoc,
],

// Though defines `namepathRole: 'namepath-defining'` in a sense, it is
// not parseable in the same way for template (e.g., allowing commas),
// so not adding
Expand Down
2 changes: 1 addition & 1 deletion src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ const getUtils = (

/** @type {GetTagsByType} */
utils.getTagsByType = (tags) => {
return jsdocUtils.getTagsByType(context, mode, tags, tagNamePreference);
return jsdocUtils.getTagsByType(context, mode, tags);
};

/** @type {HasOptionTag} */
Expand Down
6 changes: 2 additions & 4 deletions src/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1422,14 +1422,12 @@ const tagsWithNamesAndDescriptions = new Set([
* @param {import('eslint').Rule.RuleContext} context
* @param {ParserMode|undefined} mode
* @param {import('comment-parser').Spec[]} tags
* @param {TagNamePreference} tagPreference
* @returns {{
* tagsWithNames: import('comment-parser').Spec[],
* tagsWithoutNames: import('comment-parser').Spec[]
* }}
*/
const getTagsByType = (context, mode, tags, tagPreference) => {
const descName = getPreferredTagName(context, mode, 'description', tagPreference);
const getTagsByType = (context, mode, tags) => {
/**
* @type {import('comment-parser').Spec[]}
*/
Expand All @@ -1439,7 +1437,7 @@ const getTagsByType = (context, mode, tags, tagPreference) => {
tag: tagName,
} = tag;
const tagWithName = tagsWithNamesAndDescriptions.has(tagName);
if (!tagWithName && tagName !== descName) {
if (!tagWithName) {
tagsWithoutNames.push(tag);
}

Expand Down
60 changes: 51 additions & 9 deletions src/rules/matchDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export default iterateJsdoc(({
mainDescription,
matchDescription,
message,
tags,
nonemptyTags = true,
tags = {},
} = context.options[0] || {};

/**
Expand Down Expand Up @@ -81,7 +82,52 @@ export default iterateJsdoc(({
validateDescription(description);
}

if (!tags || !Object.keys(tags).length) {
/**
* @param {string} tagName
* @returns {boolean}
*/
const hasNoTag = (tagName) => {
return !tags[tagName];
};

for (const tag of [
'description',
'summary',
'file',
'classdesc',
]) {
utils.forEachPreferredTag(tag, (matchingJsdocTag, targetTagName) => {
const desc = (matchingJsdocTag.name + ' ' + utils.getTagDescription(matchingJsdocTag)).trim();
if (hasNoTag(targetTagName)) {
validateDescription(desc, matchingJsdocTag);
}
}, true);
}

if (nonemptyTags) {
for (const tag of [
'copyright',
'example',
'see',
'throws',
'todo',
'yields',
]) {
utils.forEachPreferredTag(tag, (matchingJsdocTag, targetTagName) => {
const desc = (matchingJsdocTag.name + ' ' + utils.getTagDescription(matchingJsdocTag)).trim();

if (hasNoTag(targetTagName) && !(/.+/u).test(desc)) {
report(
'JSDoc description must not be empty.',
null,
matchingJsdocTag,
);
}
});
}
}

if (!Object.keys(tags).length) {
return;
}

Expand All @@ -93,13 +139,6 @@ export default iterateJsdoc(({
return Boolean(tags[tagName]);
};

utils.forEachPreferredTag('description', (matchingJsdocTag, targetTagName) => {
const desc = (matchingJsdocTag.name + ' ' + utils.getTagDescription(matchingJsdocTag)).trim();
if (hasOptionTag(targetTagName)) {
validateDescription(desc, matchingJsdocTag);
}
}, true);

const whitelistedTags = utils.filterTags(({
tag: tagName,
}) => {
Expand Down Expand Up @@ -195,6 +234,9 @@ export default iterateJsdoc(({
message: {
type: 'string',
},
nonemptyTags: {
type: 'boolean',
},
tags: {
patternProperties: {
'.*': {
Expand Down
Loading