Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(eslint-plugin): [unified-signatures] handle getter-setter #10818

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 4 commits into from
Feb 24, 2025
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
19 changes: 16 additions & 3 deletions packages/eslint-plugin/src/rules/unified-signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,12 @@ export default createRule<Options, MessageIds>({

// collect overloads
MethodDefinition(node): void {
if (!node.value.body) {
if (!node.value.body && !isGetterOrSetter(node)) {
addOverload(node);
}
},
TSAbstractMethodDefinition(node): void {
if (!node.value.body) {
if (!node.value.body && !isGetterOrSetter(node)) {
addOverload(node);
}
},
Expand All @@ -573,7 +573,11 @@ export default createRule<Options, MessageIds>({
const exportingNode = getExportingNode(node);
addOverload(node, node.id?.name ?? exportingNode?.type, exportingNode);
},
TSMethodSignature: addOverload,
TSMethodSignature(node): void {
if (!isGetterOrSetter(node)) {
addOverload(node);
}
},

// validate scopes
'ClassDeclaration:exit': checkScope,
Expand Down Expand Up @@ -634,3 +638,12 @@ function getStaticParameterName(param: TSESTree.Node): string | undefined {
function isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {
return node.type === AST_NODE_TYPES.Identifier;
}

function isGetterOrSetter(
node:
| TSESTree.MethodDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.TSMethodSignature,
): boolean {
return node.kind === 'get' || node.kind === 'set';
}
18 changes: 18 additions & 0 deletions packages/eslint-plugin/tests/rules/unified-signatures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ function rest(...xs: number[]): Promise<number[]>;
function rest(xs: number[], y: string): Promise<string>;
async function rest(...args: any[], y?: string): Promise<number[] | string> {
return y || args;
}
`,
`
declare class Foo {
get bar();
set bar(x: number);
}
`,
`
interface Foo {
get bar();
set bar(x: number);
}
`,
`
abstract class Foo {
abstract get bar();
abstract set bar(a: unknown);
}
`,
{
Expand Down
Loading