Skip to content

Commit a6feb59

Browse files
authored
fix(eslint-plugin): [unified-signatures] does not differentiate truly private methods (#10806)
* fix: support private methods * test: add test * fix: fix lint * test: update test * fix: allow same name for private and non-private methods * fix: update prefix
1 parent d8a4fb6 commit a6feb59

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

‎packages/eslint-plugin/src/rules/unified-signatures.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,15 @@ function getOverloadInfo(node: OverloadNode): string {
620620
default: {
621621
const { key } = node as MethodDefinition;
622622

623-
return isIdentifier(key) ? key.name : (key as TSESTree.Literal).raw;
623+
if (isPrivateIdentifier(key)) {
624+
return `private_identifier_${key.name}`;
625+
}
626+
627+
if (isIdentifier(key)) {
628+
return `identifier_${key.name}`;
629+
}
630+
631+
return (key as TSESTree.Literal).raw;
624632
}
625633
}
626634
}
@@ -639,6 +647,12 @@ function isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {
639647
return node.type === AST_NODE_TYPES.Identifier;
640648
}
641649

650+
function isPrivateIdentifier(
651+
node: TSESTree.Node,
652+
): node is TSESTree.PrivateIdentifier {
653+
return node.type === AST_NODE_TYPES.PrivateIdentifier;
654+
}
655+
642656
function isGetterOrSetter(
643657
node:
644658
| TSESTree.MethodDefinition

‎packages/eslint-plugin/tests/rules/unified-signatures.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ class C {
2929
a(): void;
3030
a(a: number, b: number): void;
3131
a(a?: number, b?: number): void {}
32+
}
33+
`,
34+
`
35+
declare class Example {
36+
privateMethod(a: number): void;
37+
#privateMethod(a: number, b?: string): void;
38+
}
39+
`,
40+
`
41+
declare class Example {
42+
#privateMethod1(a: number): void;
43+
#privateMethod2(a: number, b?: string): void;
3244
}
3345
`,
3446
// No error for arity difference greater than 1.
@@ -510,6 +522,25 @@ type T = {
510522
},
511523
],
512524
},
525+
{
526+
code: `
527+
declare class Example {
528+
#privateMethod(a: number): void;
529+
#privateMethod(a: number, b?: string): void;
530+
}
531+
`,
532+
errors: [
533+
{
534+
column: 29,
535+
data: {
536+
failureStringStart:
537+
'These overloads can be combined into one signature',
538+
},
539+
line: 4,
540+
messageId: 'omittingSingleParameter',
541+
},
542+
],
543+
},
513544
{
514545
// Works for constructor.
515546
code: `

0 commit comments

Comments
 (0)