Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [unified-signatures] support ignoring overload signatures with different JSDoc comments#10781

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
44 changes: 44 additions & 0 deletionspackages/eslint-plugin/docs/rules/unified-signatures.mdx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,6 +78,50 @@ function f(b: string): void;
</TabItem>
</Tabs>

### `ignoreOverloadsWithDifferentJSDoc`

{/* insert option description */}

Examples of code for this rule with `ignoreOverloadsWithDifferentJSDoc`:

<Tabs>
<TabItem value="❌ Incorrect">

```ts option='{ "ignoreOverloadsWithDifferentJSDoc": true }'
declare function f(x: string): void;
declare function f(x: boolean): void;
/**
* @deprecate
*/
declare function f(x: number): void;
/**
* @deprecate
*/
declare function f(x: null): void;
```

</TabItem>
<TabItem value="✅ Correct">

```ts option='{ "ignoreOverloadsWithDifferentJSDoc": true }'
declare function f(x: string): void;
/**
* This signature does something else.
*/
declare function f(x: boolean): void;
/**
* @async
*/
declare function f(x: number): void;
/**
* @deprecate
*/
declare function f(x: null): void;
```

</TabItem>
</Tabs>

## When Not To Use It

This is purely a stylistic rule to help with readability of function signature overloads.
Expand Down
37 changes: 34 additions & 3 deletionspackages/eslint-plugin/src/rules/unified-signatures.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';

import type { Equal } from '../util';

Expand DownExpand Up@@ -61,6 +61,7 @@ export type MessageIds =
export type Options = [
{
ignoreDifferentlyNamedParameters?: boolean;
ignoreOverloadsWithDifferentJSDoc?: boolean;
},
];

Expand DownExpand Up@@ -91,16 +92,25 @@ export default createRule<Options, MessageIds>({
description:
'Whether two parameters with different names at the same index should be considered different even if their types are the same.',
},
ignoreOverloadsWithDifferentJSDoc: {
type: 'boolean',
description:
'Whether two overloads with different JSDoc comments should be considered different even if their parameter and return types are the same.',
},
},
},
],
},
defaultOptions: [
{
ignoreDifferentlyNamedParameters: false,
ignoreOverloadsWithDifferentJSDoc: false,
},
],
create(context, [{ ignoreDifferentlyNamedParameters }]) {
create(
context,
[{ ignoreDifferentlyNamedParameters, ignoreOverloadsWithDifferentJSDoc }],
) {
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
Expand DownExpand Up@@ -230,6 +240,15 @@ export default createRule<Options, MessageIds>({
}
}

if (ignoreOverloadsWithDifferentJSDoc) {
const aComment = getBlockCommentForNode(getExportingNode(a) ?? a);
const bComment = getBlockCommentForNode(getExportingNode(b) ?? b);

if (aComment?.value !== bComment?.value) {
return false;
}
}

return (
typesAreEqual(a.returnType, b.returnType) &&
// Must take the same type parameters.
Expand DownExpand Up@@ -522,6 +541,18 @@ export default createRule<Options, MessageIds>({
currentScope = scopes.pop();
}

/**
* @returns the first valid JSDoc comment annotating `node`
*/
function getBlockCommentForNode(
node: TSESTree.Node,
): TSESTree.Comment | undefined {
return context.sourceCode
.getCommentsBefore(node)
.reverse()
.find(comment => comment.type === AST_TOKEN_TYPES.Block);
}

function addOverload(
signature: OverloadNode,
key?: string,
Expand DownExpand Up@@ -590,7 +621,7 @@ export default createRule<Options, MessageIds>({
});

function getExportingNode(
node:TSESTree.TSDeclareFunction,
node:SignatureDefinition,
):
| TSESTree.ExportDefaultDeclaration
| TSESTree.ExportNamedDeclaration
Expand Down
View file
Open in desktop

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

Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp