Skip to content

Commit b564ef6

Browse files
Improved duplicate identifier checks for static private class elements.
1 parent 23f6c67 commit b564ef6

File tree

8 files changed

+3308
-298
lines changed

8 files changed

+3308
-298
lines changed

src/compiler/checker.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ namespace ts {
240240
SetAccessor = 2,
241241
PropertyAssignment = 4,
242242
Method = 8,
243+
PrivateStatic = 16,
243244
GetOrSetAccessor = GetAccessor | SetAccessor,
244245
PropertyAssignmentOrMethod = PropertyAssignment | Method,
245246
}
@@ -32131,29 +32132,32 @@ namespace ts {
3213132132
const isStatic = hasSyntacticModifier(member, ModifierFlags.Static);
3213232133
const name = member.name;
3213332134
if (!name) {
32134-
return;
32135+
continue;
3213532136
}
32137+
const isPrivate = isPrivateIdentifier(name);
32138+
const isPrivateStatic = isPrivate && isStatic ? DeclarationMeaning.PrivateStatic : 0;
3213632139
const names =
32137-
isPrivateIdentifier(name) ? privateIdentifiers :
32140+
isPrivate ? privateIdentifiers :
3213832141
isStatic ? staticNames :
3213932142
instanceNames;
32143+
3214032144
const memberName = name && getPropertyNameForPropertyNameNode(name);
3214132145
if (memberName) {
3214232146
switch (member.kind) {
3214332147
case SyntaxKind.GetAccessor:
32144-
addName(names, name, memberName, DeclarationMeaning.GetAccessor);
32148+
addName(names, name, memberName, DeclarationMeaning.GetAccessor | isPrivateStatic);
3214532149
break;
3214632150

3214732151
case SyntaxKind.SetAccessor:
32148-
addName(names, name, memberName, DeclarationMeaning.SetAccessor);
32152+
addName(names, name, memberName, DeclarationMeaning.SetAccessor | isPrivateStatic);
3214932153
break;
3215032154

3215132155
case SyntaxKind.PropertyDeclaration:
32152-
addName(names, name, memberName, DeclarationMeaning.GetOrSetAccessor);
32156+
addName(names, name, memberName, DeclarationMeaning.GetOrSetAccessor | isPrivateStatic);
3215332157
break;
3215432158

3215532159
case SyntaxKind.MethodDeclaration:
32156-
addName(names, name, memberName, DeclarationMeaning.Method);
32160+
addName(names, name, memberName, DeclarationMeaning.Method | isPrivateStatic);
3215732161
break;
3215832162
}
3215932163
}
@@ -32163,16 +32167,25 @@ namespace ts {
3216332167
function addName(names: UnderscoreEscapedMap<DeclarationMeaning>, location: Node, name: __String, meaning: DeclarationMeaning) {
3216432168
const prev = names.get(name);
3216532169
if (prev) {
32166-
if (prev & DeclarationMeaning.Method) {
32167-
if (meaning !== DeclarationMeaning.Method) {
32168-
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
32169-
}
32170-
}
32171-
else if (prev & meaning) {
32172-
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
32170+
// For private identifiers, do not allow mixing of static and instance members with the same name
32171+
if((prev & DeclarationMeaning.PrivateStatic) !== (meaning & DeclarationMeaning.PrivateStatic)) {
32172+
error(location, Diagnostics.Duplicate_identifier_0_Private_elements_with_the_same_private_name_must_all_be_either_static_or_instance, getTextOfNode(location));
3217332173
}
3217432174
else {
32175-
names.set(name, prev | meaning);
32175+
const prevIsMethod = !!(prev & DeclarationMeaning.Method);
32176+
const isMethod = !!(meaning & DeclarationMeaning.Method);
32177+
if (prevIsMethod || isMethod) {
32178+
if (prevIsMethod !== isMethod) {
32179+
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
32180+
}
32181+
// If this is a method/method duplication is might be an overload, so this will be handled when overloads are considered
32182+
}
32183+
else if (prev & meaning & ~DeclarationMeaning.PrivateStatic) {
32184+
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
32185+
}
32186+
else {
32187+
names.set(name, prev | meaning);
32188+
}
3217632189
}
3217732190
}
3217832191
else {

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3264,11 +3264,15 @@
32643264
"category": "Error",
32653265
"code": 2802
32663266
},
3267-
32683267
"Cannot assign to private method '{0}'. Private methods are not writable.": {
32693268
"category": "Error",
32703269
"code": 2803
32713270
},
3271+
"Duplicate identifier '{0}'. Private elements with the same private name must all be either static or instance.": {
3272+
"category": "Error",
3273+
"code": 2804
3274+
},
3275+
32723276
"Import declaration '{0}' is using private name '{1}'.": {
32733277
"category": "Error",
32743278
"code": 4000

0 commit comments

Comments
 (0)