Skip to content

feat(require-jsdoc): add on-by-default skipInterveningOverloadedDeclarations option #1452

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

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
8 changes: 7 additions & 1 deletion .README/rules/require-jsdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,20 @@ apply to any context; see `contexts` for line counts per context.
An optional message to add to the inserted JSDoc block. Defaults to the
empty string.

### `skipInterveningOverloadedDeclarations`

If `true`, will skip above uncommented overloaded functions to check
for a comment block (e.g., at the top of a set of overloaded functions).
Defaults to `true`.

## Context and settings

|||
|---|---|
|Context|`ArrowFunctionExpression`, `ClassDeclaration`, `ClassExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
|Tags|N/A|
|Recommended|true|
|Options|`publicOnly`, `require`, `contexts`, `exemptEmptyConstructors`, `exemptEmptyFunctions`, `enableFixer`, `minLineCount`, `fixerMessage`|
|Options|`publicOnly`, `require`, `contexts`, `exemptEmptyConstructors`, `exemptEmptyFunctions`, `enableFixer`, `minLineCount`, `fixerMessage`, `skipInterveningOverloadedDeclarations`|

## Failing examples

Expand Down
49 changes: 48 additions & 1 deletion docs/rules/require-jsdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [`enableFixer`](#user-content-require-jsdoc-options-enablefixer)
* [`minLineCount`](#user-content-require-jsdoc-options-minlinecount)
* [`fixerMessage`](#user-content-require-jsdoc-options-fixermessage)
* [`skipInterveningOverloadedDeclarations`](#user-content-require-jsdoc-options-skipinterveningoverloadeddeclarations)
* [Context and settings](#user-content-require-jsdoc-context-and-settings)
* [Failing examples](#user-content-require-jsdoc-failing-examples)
* [Passing examples](#user-content-require-jsdoc-passing-examples)
Expand Down Expand Up @@ -157,6 +158,14 @@ apply to any context; see `contexts` for line counts per context.
An optional message to add to the inserted JSDoc block. Defaults to the
empty string.

<a name="user-content-require-jsdoc-options-skipinterveningoverloadeddeclarations"></a>
<a name="require-jsdoc-options-skipinterveningoverloadeddeclarations"></a>
### <code>skipInterveningOverloadedDeclarations</code>

If `true`, will skip above uncommented overloaded functions to check
for a comment block (e.g., at the top of a set of overloaded functions).
Defaults to `true`.

<a name="user-content-require-jsdoc-context-and-settings"></a>
<a name="require-jsdoc-context-and-settings"></a>
## Context and settings
Expand All @@ -166,7 +175,7 @@ empty string.
|Context|`ArrowFunctionExpression`, `ClassDeclaration`, `ClassExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
|Tags|N/A|
|Recommended|true|
|Options|`publicOnly`, `require`, `contexts`, `exemptEmptyConstructors`, `exemptEmptyFunctions`, `enableFixer`, `minLineCount`, `fixerMessage`|
|Options|`publicOnly`, `require`, `contexts`, `exemptEmptyConstructors`, `exemptEmptyFunctions`, `enableFixer`, `minLineCount`, `fixerMessage`, `skipInterveningOverloadedDeclarations`|

<a name="user-content-require-jsdoc-failing-examples"></a>
<a name="require-jsdoc-failing-examples"></a>
Expand Down Expand Up @@ -1041,6 +1050,32 @@ export class B implements A, B {
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["MethodDefinition"]}]
// Message: Missing JSDoc comment.

/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
* Test function without param.
*/
function myFunction(): void;
function myFunction(foo?: string) {}
// "jsdoc/require-jsdoc": ["error"|"warn", {"skipInterveningOverloadedDeclarations":false}]
// Message: Missing JSDoc comment.

/**
* Test function without param.
*/
function myFunction(): void;
/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
function myFunction(foo?: string) {}
// "jsdoc/require-jsdoc": ["error"|"warn", {"skipInterveningOverloadedDeclarations":false}]
// Message: Missing JSDoc comment.
````


Expand Down Expand Up @@ -1944,6 +1979,7 @@ export function arrayMap<Target, Source extends Array<unknown>>(data: Source, ca
export function arrayMap<Target, Source extends AnyArrayType>(data: Source, callback: MapCallback<Target, Source>): AnyArrayType<Target> {
return data.map(callback);
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"skipInterveningOverloadedDeclarations":true}]

export interface A {
a: string;
Expand All @@ -1960,5 +1996,16 @@ export class B implements A {
}
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["MethodDefinition"]}]

/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
* Test function without param.
*/
function myFunction(): void;
function myFunction(foo?: string) {}
````

11 changes: 11 additions & 0 deletions docs/rules/require-param.md
Original file line number Diff line number Diff line change
Expand Up @@ -1842,5 +1842,16 @@ const inner = (c: number, d: string): void => {
*/
function quux (a, b) {}
// "jsdoc/require-param": ["error"|"warn", {"ignoreWhenAllParamsMissing":true}]

/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
* Test function without param.
*/
function myFunction(): void;
function myFunction(foo?: string) {}
````

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": "http://gajus.com"
},
"dependencies": {
"@es-joy/jsdoccomment": "~0.53.0",
"@es-joy/jsdoccomment": "~0.54.0",
"are-docs-informative": "^0.0.2",
"comment-parser": "1.4.1",
"debug": "^4.4.1",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/rules/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ const OPTIONS_SCHEMA = {
},
type: 'object',
},
skipInterveningOverloadedDeclarations: {
default: true,
type: 'boolean',
},
},
type: 'object',
};
Expand Down Expand Up @@ -302,6 +306,7 @@ const getOption = (context, baseObject, option, key) => {
* enableFixer: boolean,
* exemptEmptyConstructors: boolean,
* exemptEmptyFunctions: boolean,
* skipInterveningOverloadedDeclarations: boolean,
* fixerMessage: string,
* minLineCount: undefined|import('../iterateJsdoc.js').Integer,
* publicOnly: boolean|{[key: string]: boolean|undefined}
Expand All @@ -317,6 +322,7 @@ const getOptions = (context, settings) => {
fixerMessage = '',
minLineCount = undefined,
publicOnly,
skipInterveningOverloadedDeclarations = true,
} = context.options[0] || {};

return {
Expand Down Expand Up @@ -386,6 +392,7 @@ const getOptions = (context, settings) => {
/** @type {import('json-schema').JSONSchema4Object} */
(OPTIONS_SCHEMA.properties).require,
),
skipInterveningOverloadedDeclarations,
};
};

Expand All @@ -411,6 +418,7 @@ export default {
fixerMessage,
minLineCount,
require: requireOption,
skipInterveningOverloadedDeclarations,
} = opts;

const publicOnly =
Expand Down Expand Up @@ -476,7 +484,11 @@ export default {
}
}

const jsDocNode = getJSDocComment(sourceCode, node, settings);
const jsDocNode = getJSDocComment(
sourceCode, node, settings, {
checkOverloads: skipInterveningOverloadedDeclarations,
},
);

if (jsDocNode) {
return;
Expand Down
108 changes: 108 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4296,6 +4296,92 @@ function quux (foo) {
}
`,
},
{
code: `
/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
* Test function without param.
*/
function myFunction(): void;
function myFunction(foo?: string) {}
`,
errors: [
{
line: 11,
message: 'Missing JSDoc comment.',
},
],
languageOptions: {
parser: typescriptEslintParser,
},
options: [
{
skipInterveningOverloadedDeclarations: false,
},
],
output: `
/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
* Test function without param.
*/
function myFunction(): void;
/**
*
*/
function myFunction(foo?: string) {}
`,
},
{
code: `
/**
* Test function without param.
*/
function myFunction(): void;
/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
function myFunction(foo?: string) {}
`,
errors: [
{
line: 11,
message: 'Missing JSDoc comment.',
},
],
languageOptions: {
parser: typescriptEslintParser,
},
options: [
{
skipInterveningOverloadedDeclarations: false,
},
],
output: `
/**
* Test function without param.
*/
function myFunction(): void;
/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
*
*/
function myFunction(foo?: string) {}
`,
},
],
valid: [
{
Expand Down Expand Up @@ -6442,6 +6528,11 @@ function quux (foo) {
languageOptions: {
parser: typescriptEslintParser,
},
options: [
{
skipInterveningOverloadedDeclarations: true,
},
],
},
{
code: `
Expand Down Expand Up @@ -6471,5 +6562,22 @@ function quux (foo) {
},
],
},
{
code: `
/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
* Test function without param.
*/
function myFunction(): void;
function myFunction(foo?: string) {}
`,
languageOptions: {
parser: typescriptEslintParser,
},
},
],
});
18 changes: 18 additions & 0 deletions test/rules/assertions/requireParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -3677,5 +3677,23 @@ export default /** @type {import('../index.js').TestCases} */ ({
},
],
},
{
code: `
/**
* Test function with param.
* @param foo - Test param.
*/
function myFunction(foo: string): void;
/**
* Test function without param.
*/
function myFunction(): void;
function myFunction(foo?: string) {}
`,
languageOptions: {
parser: typescriptEslintParser,
sourceType: 'module',
},
},
],
});
Loading