Skip to content

fix(eslint-plugin): [unified-signatures] does not differentiate truly private methods #10806

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
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
16 changes: 15 additions & 1 deletion packages/eslint-plugin/src/rules/unified-signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,15 @@ function getOverloadInfo(node: OverloadNode): string {
default: {
const { key } = node as MethodDefinition;

return isIdentifier(key) ? key.name : (key as TSESTree.Literal).raw;
if (isPrivateIdentifier(key)) {
return `private_identifier_${key.name}`;
}

if (isIdentifier(key)) {
return `identifier_${key.name}`;
}

return (key as TSESTree.Literal).raw;
}
}
}
Expand All @@ -639,6 +647,12 @@ function isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {
return node.type === AST_NODE_TYPES.Identifier;
}

function isPrivateIdentifier(
node: TSESTree.Node,
): node is TSESTree.PrivateIdentifier {
return node.type === AST_NODE_TYPES.PrivateIdentifier;
}

function isGetterOrSetter(
node:
| TSESTree.MethodDefinition
Expand Down
31 changes: 31 additions & 0 deletions packages/eslint-plugin/tests/rules/unified-signatures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class C {
a(): void;
a(a: number, b: number): void;
a(a?: number, b?: number): void {}
}
`,
`
declare class Example {
privateMethod(a: number): void;
#privateMethod(a: number, b?: string): void;
}
`,
`
declare class Example {
#privateMethod1(a: number): void;
#privateMethod2(a: number, b?: string): void;
}
`,
// No error for arity difference greater than 1.
Expand Down Expand Up @@ -510,6 +522,25 @@ type T = {
},
],
},
{
code: `
declare class Example {
#privateMethod(a: number): void;
#privateMethod(a: number, b?: string): void;
}
`,
errors: [
{
column: 29,
data: {
failureStringStart:
'These overloads can be combined into one signature',
},
line: 4,
messageId: 'omittingSingleParameter',
},
],
},
{
// Works for constructor.
code: `
Expand Down
Loading