Skip to content

Commit 7aa2e82

Browse files
committed
fix(46824): remove duplicate private identifier errors
1 parent 2bbdb31 commit 7aa2e82

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28235,12 +28235,18 @@ namespace ts {
2823528235
if (!getContainingClass(privId)) {
2823628236
return grammarErrorOnNode(privId, Diagnostics.Private_identifiers_are_not_allowed_outside_class_bodies);
2823728237
}
28238-
if (!isExpressionNode(privId)) {
28239-
return grammarErrorOnNode(privId, Diagnostics.Private_identifiers_are_only_allowed_in_class_bodies_and_may_only_be_used_as_part_of_a_class_member_declaration_property_access_or_on_the_left_hand_side_of_an_in_expression);
28240-
}
28241-
if (!getSymbolForPrivateIdentifierExpression(privId)) {
28242-
return grammarErrorOnNode(privId, Diagnostics.Cannot_find_name_0, idText(privId));
28238+
28239+
if (!isForInStatement(privId.parent)) {
28240+
if (!isExpressionNode(privId)) {
28241+
return grammarErrorOnNode(privId, Diagnostics.Private_identifiers_are_only_allowed_in_class_bodies_and_may_only_be_used_as_part_of_a_class_member_declaration_property_access_or_on_the_left_hand_side_of_an_in_expression);
28242+
}
28243+
28244+
const isInOperation = isBinaryExpression(privId.parent) && privId.parent.operatorToken.kind === SyntaxKind.InKeyword;
28245+
if (!getSymbolForPrivateIdentifierExpression(privId) && !isInOperation) {
28246+
return grammarErrorOnNode(privId, Diagnostics.Cannot_find_name_0, idText(privId));
28247+
}
2824328248
}
28249+
2824428250
return false;
2824528251
}
2824628252

tests/baselines/reference/privateNameInInExpression.errors.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(21,29): error TS2571: Object is of type 'unknown'.
2-
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(23,19): error TS2304: Cannot find name '#fiel'.
32
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(23,19): error TS2339: Property '#fiel' does not exist on type 'any'.
43
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(25,20): error TS1451: Private identifiers are only allowed in class bodies and may only be used as part of a class member declaration, property access, or on the left-hand-side of an 'in' expression
5-
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(27,14): error TS1451: Private identifiers are only allowed in class bodies and may only be used as part of a class member declaration, property access, or on the left-hand-side of an 'in' expression
4+
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(27,14): error TS2304: Cannot find name '#field'.
65
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(27,14): error TS2406: The left-hand side of a 'for...in' statement must be a variable or a property access.
76
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(29,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'boolean'.
87
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(43,27): error TS2531: Object is possibly 'null'.
98
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(114,12): error TS18016: Private identifiers are not allowed outside class bodies.
109

1110

12-
==== tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts (9 errors) ====
11+
==== tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts (8 errors) ====
1312
class Foo {
1413
#field = 1;
1514
static #staticField = 2;
@@ -36,8 +35,6 @@ tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.t
3635

3736
const b = #fiel in v; // Bad - typo in privateID
3837
~~~~~
39-
!!! error TS2304: Cannot find name '#fiel'.
40-
~~~~~
4138
!!! error TS2339: Property '#fiel' does not exist on type 'any'.
4239

4340
const c = (#field) in v; // Bad - privateID is not an expression on its own
@@ -46,7 +43,7 @@ tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.t
4643

4744
for (#field in v) { /**/ } // Bad - 'in' not allowed
4845
~~~~~~
49-
!!! error TS1451: Private identifiers are only allowed in class bodies and may only be used as part of a class member declaration, property access, or on the left-hand-side of an 'in' expression
46+
!!! error TS2304: Cannot find name '#field'.
5047
~~~~~~
5148
!!! error TS2406: The left-hand side of a 'for...in' statement must be a variable or a property access.
5249

0 commit comments

Comments
 (0)