Skip to content
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

feat: Require description only for public functions #890

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .README/rules/require-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ An options object may have any of the following properties:
Defaults to `true`.
- `checkSetters` - A value indicating whether setters should be checked.
Defaults to `true`.
- `publicOnly`: - A value indicating whether only exported constructs should be checked

| | |
| -------- | ------------------------------------------------------------------------------------------------------------- |
Expand Down
8 changes: 8 additions & 0 deletions .README/rules/require-param-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Requires that each `@param` tag has a `description` value.

#### Options

Accepts one optional options object with the following optional keys.

##### `publicOnly`

This option will insist that missing jsdoc blocks are only reported for
function bodies / class declarations that are exported from the module.
For more information see the documentation of `require-jsdoc`.

##### `contexts`

Set this to an array of strings representing the AST context (or an object with
Expand Down
8 changes: 8 additions & 0 deletions .README/rules/require-returns-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ or if it is `Promise<void>` or `Promise<undefined>`.

#### Options

Accepts one optional options object with the following optional keys.

##### `publicOnly`

This option will insist that missing jsdoc blocks are only reported for
function bodies / class declarations that are exported from the module.
For more information see the documentation of `require-jsdoc`.

##### `contexts`

Set this to an array of strings representing the AST context (or an object with
Expand Down
237 changes: 237 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11411,6 +11411,7 @@ An options object may have any of the following properties:
Defaults to `true`.
- `checkSetters` - A value indicating whether setters should be checked.
Defaults to `true`.
- `publicOnly`: - A value indicating whether only exported constructs should be checked

| | |
| -------- | ------------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -11696,6 +11697,44 @@ class quux {
// Settings: {"jsdoc":{"implementsReplacesDocs":false}}
// "jsdoc/require-description": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock[postDelimiter=\"\"]:has(JsdocTag[tag=\"implements\"])","context":"any"}],"descriptionStyle":"tag"}]
// Message: Missing JSDoc @description declaration.

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

}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":true}]
// Message: Missing JSDoc block description.

/**
*
*/
module.exports = function quux (foo) {

}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":{"cjs":true,"esm":false}}]
// Message: Missing JSDoc block description.

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

}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":false}]
// Message: Missing JSDoc block description.

export default class Class {
/**
*
*/
quux(foo) {

}
}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":true}]
// Message: Missing JSDoc block description.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -11926,6 +11965,40 @@ class quux {
* description already.
*/
// "jsdoc/require-description": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock[postDelimiter=\"\"]:has(JsdocTag[rawType=\"{Bar}\"])","context":"any"}],"descriptionStyle":"tag"}]

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

}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":true}]

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

}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":true}]

/**
*
*/
module.exports = function quux (foo) {

}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":{"cjs":false}}]

class Class {
/**
*
*/
quux(foo) {

}
}
// "jsdoc/require-description": ["error"|"warn", {"publicOnly":true}]
````


Expand Down Expand Up @@ -14625,6 +14698,16 @@ Requires that each `@param` tag has a `description` value.
<a name="eslint-plugin-jsdoc-rules-require-param-description-options-29"></a>
#### Options

Accepts one optional options object with the following optional keys.

<a name="user-content-eslint-plugin-jsdoc-rules-require-param-description-options-29-publiconly-1"></a>
<a name="eslint-plugin-jsdoc-rules-require-param-description-options-29-publiconly-1"></a>
##### <code>publicOnly</code>

This option will insist that missing jsdoc blocks are only reported for
function bodies / class declarations that are exported from the module.
For more information see the documentation of `require-jsdoc`.

<a name="user-content-eslint-plugin-jsdoc-rules-require-param-description-options-29-contexts-8"></a>
<a name="eslint-plugin-jsdoc-rules-require-param-description-options-29-contexts-8"></a>
##### <code>contexts</code>
Expand Down Expand Up @@ -14699,6 +14782,44 @@ function quux (foo) {
}
// Settings: {"jsdoc":{"tagNamePreference":{"param":false}}}
// Message: Unexpected tag `@param`

/**
* @param foo
*/
export function quux (foo) {

}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":true}]
// Message: Missing JSDoc @param "foo" description.

/**
* @param foo
*/
module.exports = function quux (foo) {

}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":{"cjs":true,"esm":false}}]
// Message: Missing JSDoc @param "foo" description.

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

}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":false}]
// Message: Missing JSDoc @param "foo" description.

export default class Class {
/**
* @param foo
*/
quux(foo) {

}
}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":true}]
// Message: Missing JSDoc @param "foo" description.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -14735,6 +14856,40 @@ function quux (foo) {
* @callback
* @param foo
*/

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

}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":true}]

/**
* @param foo - description
*/
export function quux (foo) {

}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":true}]

/**
* @param
*/
module.exports = function quux (foo) {

}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":{"cjs":false}}]

class Class {
/**
* @param foo
*/
quux(foo) {

}
}
// "jsdoc/require-param-description": ["error"|"warn", {"publicOnly":true}]
````


Expand Down Expand Up @@ -17665,6 +17820,16 @@ or if it is `Promise<void>` or `Promise<undefined>`.
<a name="eslint-plugin-jsdoc-rules-require-returns-description-options-34"></a>
#### Options

Accepts one optional options object with the following optional keys.

<a name="user-content-eslint-plugin-jsdoc-rules-require-returns-description-options-34-publiconly-2"></a>
<a name="eslint-plugin-jsdoc-rules-require-returns-description-options-34-publiconly-2"></a>
##### <code>publicOnly</code>

This option will insist that missing jsdoc blocks are only reported for
function bodies / class declarations that are exported from the module.
For more information see the documentation of `require-jsdoc`.

<a name="user-content-eslint-plugin-jsdoc-rules-require-returns-description-options-34-contexts-12"></a>
<a name="eslint-plugin-jsdoc-rules-require-returns-description-options-34-contexts-12"></a>
##### <code>contexts</code>
Expand Down Expand Up @@ -17747,6 +17912,44 @@ function quux () {
}
// Settings: {"jsdoc":{"tagNamePreference":{"returns":false}}}
// Message: Unexpected tag `@returns`

/**
* @returns
*/
export function quux (foo) {

}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":true}]
// Message: Missing JSDoc @returns description.

/**
* @returns
*/
module.exports = function quux (foo) {

}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":{"cjs":true,"esm":false}}]
// Message: Missing JSDoc @returns description.

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

}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":false}]
// Message: Missing JSDoc @returns description.

export default class Class {
/**
* @returns
*/
quux(foo) {

}
}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":true}]
// Message: Missing JSDoc @returns description.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -17811,6 +18014,40 @@ function quux () {
* @callback
* @returns
*/

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

}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":true}]

/**
* @returns - description
*/
export function quux (foo) {

}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":true}]

/**
* @returns
*/
module.exports = function quux (foo) {

}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":{"cjs":false}}]

class Class {
/**
* @returns
*/
quux(foo) {

}
}
// "jsdoc/require-returns-description": ["error"|"warn", {"publicOnly":true}]
````


Expand Down
17 changes: 17 additions & 0 deletions src/exportParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,24 @@ const isUncommentedExport = function (node, sourceCode, opt, settings) {
return isNodeExported(node, parseResult.globalVars, opt);
};

const isPublic = function (node, sourceCode, opt) {
const normalizedOpts = {
ancestorsOnly: Boolean(opt?.ancestorsOnly ?? false),
esm: Boolean(opt?.esm ?? true),
initModuleExports: Boolean(opt?.cjs ?? true),
initWindow: Boolean(opt?.window ?? false),
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this same option normalization is needed elsewhere in require-jsdoc, let's have both reference a new utility (I think just use it also within isUncommentedExport).

if (normalizedOpts.esm && (getExportAncestor(node) || isExportByAncestor(node))) {
return true;
}

const parseResult = parse(sourceCode.ast, node, normalizedOpts);

return isNodeExported(node, parseResult.globalVars, normalizedOpts);
};

export default {
isPublic,
isUncommentedExport,
parse,
};
9 changes: 9 additions & 0 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
stringify as commentStringify,
util,
} from 'comment-parser';
import exportParser from './exportParser.js';
import jsdocUtils from './jsdocUtils';

const {
Expand Down Expand Up @@ -962,6 +963,14 @@ const iterate = (
return;
}

if (
context.options[0] &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could also support a check for settings.publicOnly here in case the publicOnly setting is used in place of an option.

context.options[0].publicOnly &&
!exportParser.isPublic(node, sourceCode, context.options[0].publicOnly)
) {
return;
}

iterator({
context,
globalState,
Expand Down
Loading