Skip to content

Commit 78bc368

Browse files
committed
Fix duplicate identifier reporting in classes
Previously declarations in the order method-property would not report an error, but the order property-method would. Now both orders report "Duplicate identifier '{0}'."
1 parent 445421b commit 78bc368

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/compiler/checker.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15780,19 +15780,20 @@ namespace ts {
1578015780
}
1578115781

1578215782
function checkClassForDuplicateDeclarations(node: ClassLikeDeclaration) {
15783-
const enum Accessor {
15783+
const enum Declaration {
1578415784
Getter = 1,
1578515785
Setter = 2,
15786+
Method = 4,
1578615787
Property = Getter | Setter
1578715788
}
1578815789

15789-
const instanceNames = createMap<Accessor>();
15790-
const staticNames = createMap<Accessor>();
15790+
const instanceNames = createMap<Declaration>();
15791+
const staticNames = createMap<Declaration>();
1579115792
for (const member of node.members) {
1579215793
if (member.kind === SyntaxKind.Constructor) {
1579315794
for (const param of (member as ConstructorDeclaration).parameters) {
1579415795
if (isParameterPropertyDeclaration(param)) {
15795-
addName(instanceNames, param.name, (param.name as Identifier).text, Accessor.Property);
15796+
addName(instanceNames, param.name, (param.name as Identifier).text, Declaration.Property);
1579615797
}
1579715798
}
1579815799
}
@@ -15804,25 +15805,34 @@ namespace ts {
1580415805
if (memberName) {
1580515806
switch (member.kind) {
1580615807
case SyntaxKind.GetAccessor:
15807-
addName(names, member.name, memberName, Accessor.Getter);
15808+
addName(names, member.name, memberName, Declaration.Getter);
1580815809
break;
1580915810

1581015811
case SyntaxKind.SetAccessor:
15811-
addName(names, member.name, memberName, Accessor.Setter);
15812+
addName(names, member.name, memberName, Declaration.Setter);
1581215813
break;
1581315814

1581415815
case SyntaxKind.PropertyDeclaration:
15815-
addName(names, member.name, memberName, Accessor.Property);
15816+
addName(names, member.name, memberName, Declaration.Property);
15817+
break;
15818+
15819+
case SyntaxKind.MethodDeclaration:
15820+
addName(names, member.name, memberName, Declaration.Method);
1581615821
break;
1581715822
}
1581815823
}
1581915824
}
1582015825
}
1582115826

15822-
function addName(names: Map<Accessor>, location: Node, name: string, meaning: Accessor) {
15827+
function addName(names: Map<Declaration>, location: Node, name: string, meaning: Declaration) {
1582315828
const prev = names.get(name);
1582415829
if (prev) {
15825-
if (prev & meaning) {
15830+
if (prev & Declaration.Method) {
15831+
if (meaning !== Declaration.Method) {
15832+
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
15833+
}
15834+
}
15835+
else if (prev & meaning) {
1582615836
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
1582715837
}
1582815838
else {

0 commit comments

Comments
 (0)