Skip to content

Commit 5ae7962

Browse files
committed
Unify the way anonymous class symbols are printed in error messages
1 parent e6a50a7 commit 5ae7962

10 files changed

Lines changed: 176 additions & 9 deletions

src/compiler/checker.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24237,13 +24237,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2423724237
const privateIdentifierDescription = unmatchedProperty.valueDeclaration.name.escapedText;
2423824238
const symbolTableKey = getSymbolNameForPrivateIdentifier(source.symbol, privateIdentifierDescription);
2423924239
if (symbolTableKey && getPropertyOfType(source, symbolTableKey)) {
24240-
const sourceName = factory.getDeclarationName(source.symbol.valueDeclaration);
24241-
const targetName = factory.getDeclarationName(target.symbol.valueDeclaration);
2424224240
reportError(
2424324241
Diagnostics.Property_0_in_type_1_refers_to_a_different_member_that_cannot_be_accessed_from_within_type_2,
2424424242
diagnosticName(privateIdentifierDescription),
24245-
diagnosticName(sourceName.escapedText === "" ? anon : sourceName),
24246-
diagnosticName(targetName.escapedText === "" ? anon : targetName),
24243+
symbolToString(source.symbol),
24244+
symbolToString(target.symbol),
2424724245
);
2424824246
return;
2424924247
}
@@ -34681,7 +34679,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3468134679
right,
3468234680
Diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier,
3468334681
diagName,
34684-
diagnosticName(typeClass.name || anon),
34682+
symbolToString(typeClass.symbol),
3468534683
);
3468634684
return true;
3468734685
}

tests/baselines/reference/privateNameMethodClassExpression.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
privateNameMethodClassExpression.ts(9,17): error TS18013: Property '#method' is not accessible outside class '(anonymous)' because it has a private identifier.
2-
privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is not accessible outside class '(anonymous)' because it has a private identifier.
1+
privateNameMethodClassExpression.ts(9,17): error TS18013: Property '#method' is not accessible outside class 'C' because it has a private identifier.
2+
privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is not accessible outside class 'C' because it has a private identifier.
33

44

55
==== privateNameMethodClassExpression.ts (2 errors) ====
@@ -13,9 +13,9 @@ privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is
1313
console.log(C.getInstance().getField());
1414
C.getInstance().#method; // Error
1515
~~~~~~~
16-
!!! error TS18013: Property '#method' is not accessible outside class '(anonymous)' because it has a private identifier.
16+
!!! error TS18013: Property '#method' is not accessible outside class 'C' because it has a private identifier.
1717
C.getInstance().#field; // Error
1818
~~~~~~
19-
!!! error TS18013: Property '#field' is not accessible outside class '(anonymous)' because it has a private identifier.
19+
!!! error TS18013: Property '#field' is not accessible outside class 'C' because it has a private identifier.
2020

2121

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
privateNameMethodClassExpression2.ts(5,6): error TS18013: Property '#method' is not accessible outside class '(Anonymous class)' because it has a private identifier.
2+
privateNameMethodClassExpression2.ts(9,6): error TS18013: Property '#field' is not accessible outside class '(Anonymous class)' because it has a private identifier.
3+
4+
5+
==== privateNameMethodClassExpression2.ts (2 errors) ====
6+
new (class {
7+
#method() {
8+
return 42;
9+
}
10+
})().#method; // error
11+
~~~~~~~
12+
!!! error TS18013: Property '#method' is not accessible outside class '(Anonymous class)' because it has a private identifier.
13+
14+
new (class {
15+
#field = 42;
16+
})().#field; // error
17+
~~~~~~
18+
!!! error TS18013: Property '#field' is not accessible outside class '(Anonymous class)' because it has a private identifier.
19+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/privateNameMethodClassExpression2.ts] ////
2+
3+
=== privateNameMethodClassExpression2.ts ===
4+
new (class {
5+
#method() {
6+
>#method : Symbol((Anonymous class).#method, Decl(privateNameMethodClassExpression2.ts, 0, 12))
7+
8+
return 42;
9+
}
10+
})().#method; // error
11+
12+
new (class {
13+
#field = 42;
14+
>#field : Symbol((Anonymous class).#field, Decl(privateNameMethodClassExpression2.ts, 6, 12))
15+
16+
})().#field; // error
17+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/privateNameMethodClassExpression2.ts] ////
2+
3+
=== privateNameMethodClassExpression2.ts ===
4+
new (class {
5+
>new (class { #method() { return 42; }})().#method : any
6+
> : ^^^
7+
>new (class { #method() { return 42; }})() : (Anonymous class)
8+
> : ^^^^^^^^^^^^^^^^^
9+
>(class { #method() { return 42; }}) : typeof (Anonymous class)
10+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
11+
>class { #method() { return 42; }} : typeof (Anonymous class)
12+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
#method() {
15+
>#method : () => number
16+
> : ^^^^^^^^^^^^
17+
18+
return 42;
19+
>42 : 42
20+
> : ^^
21+
}
22+
})().#method; // error
23+
24+
new (class {
25+
>new (class { #field = 42;})().#field : any
26+
> : ^^^
27+
>new (class { #field = 42;})() : (Anonymous class)
28+
> : ^^^^^^^^^^^^^^^^^
29+
>(class { #field = 42;}) : typeof (Anonymous class)
30+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
31+
>class { #field = 42;} : typeof (Anonymous class)
32+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
33+
34+
#field = 42;
35+
>#field : number
36+
> : ^^^^^^
37+
>42 : 42
38+
> : ^^
39+
40+
})().#field; // error
41+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
privateNamesUnique-6.ts(6,7): error TS2322: Type '(Anonymous class)' is not assignable to type 'A'.
2+
Property '#foo' in type '(Anonymous class)' refers to a different member that cannot be accessed from within type 'A'.
3+
4+
5+
==== privateNamesUnique-6.ts (1 errors) ====
6+
class A {
7+
#foo: number;
8+
}
9+
10+
// error
11+
const test: A = new (class {
12+
~~~~
13+
!!! error TS2322: Type '(Anonymous class)' is not assignable to type 'A'.
14+
!!! error TS2322: Property '#foo' in type '(Anonymous class)' refers to a different member that cannot be accessed from within type 'A'.
15+
#foo: number;
16+
})();
17+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/privateNamesUnique-6.ts] ////
2+
3+
=== privateNamesUnique-6.ts ===
4+
class A {
5+
>A : Symbol(A, Decl(privateNamesUnique-6.ts, 0, 0))
6+
7+
#foo: number;
8+
>#foo : Symbol(A.#foo, Decl(privateNamesUnique-6.ts, 0, 9))
9+
}
10+
11+
// error
12+
const test: A = new (class {
13+
>test : Symbol(test, Decl(privateNamesUnique-6.ts, 5, 5))
14+
>A : Symbol(A, Decl(privateNamesUnique-6.ts, 0, 0))
15+
16+
#foo: number;
17+
>#foo : Symbol((Anonymous class).#foo, Decl(privateNamesUnique-6.ts, 5, 28))
18+
19+
})();
20+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/privateNamesUnique-6.ts] ////
2+
3+
=== privateNamesUnique-6.ts ===
4+
class A {
5+
>A : A
6+
> : ^
7+
8+
#foo: number;
9+
>#foo : number
10+
> : ^^^^^^
11+
}
12+
13+
// error
14+
const test: A = new (class {
15+
>test : A
16+
> : ^
17+
>new (class { #foo: number;})() : (Anonymous class)
18+
> : ^^^^^^^^^^^^^^^^^
19+
>(class { #foo: number;}) : typeof (Anonymous class)
20+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
21+
>class { #foo: number;} : typeof (Anonymous class)
22+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
23+
24+
#foo: number;
25+
>#foo : number
26+
> : ^^^^^^
27+
28+
})();
29+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @strict: true
2+
// @target: es2015
3+
// @noEmit: true
4+
5+
new (class {
6+
#method() {
7+
return 42;
8+
}
9+
})().#method; // error
10+
11+
new (class {
12+
#field = 42;
13+
})().#field; // error
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @strict: true
2+
// @strictPropertyInitialization: false
3+
// @target: es6
4+
// @noEmit: true
5+
6+
class A {
7+
#foo: number;
8+
}
9+
10+
// error
11+
const test: A = new (class {
12+
#foo: number;
13+
})();

0 commit comments

Comments
 (0)