Skip to content

Unify the way anonymous class symbols are printed in error messages #731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10845,7 +10845,7 @@ func (c *Checker) checkPrivateIdentifierPropertyAccess(leftType *Type, right *as
return true
}
}
c.error(right, diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier, diagName, scanner.DeclarationNameToString(typeClass.Name()))
c.error(right, diagnostics.Property_0_is_not_accessible_outside_class_1_because_it_has_a_private_identifier, diagName, c.SymbolToString(typeClass.Symbol()))
return true
}
return false
Expand Down
5 changes: 1 addition & 4 deletions internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/microsoft/typescript-go/internal/compiler/diagnostics"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/jsnum"
"github.com/microsoft/typescript-go/internal/scanner"
)

type SignatureCheckMode uint32
Expand Down Expand Up @@ -4272,9 +4271,7 @@ func (r *Relater) reportUnmatchedProperty(source *Type, target *Type, unmatchedP
privateIdentifierDescription := unmatchedProperty.ValueDeclaration.Name().Text()
symbolTableKey := binder.GetSymbolNameForPrivateIdentifier(source.symbol, privateIdentifierDescription)
if r.c.getPropertyOfType(source, symbolTableKey) != nil {
sourceName := scanner.DeclarationNameToString(ast.GetNameOfDeclaration(source.symbol.ValueDeclaration))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the previous behavior here was already diverging slightly from Strada for those anonymous classes (IMHO, in a good way)

targetName := scanner.DeclarationNameToString(ast.GetNameOfDeclaration(target.symbol.ValueDeclaration))
r.reportError(diagnostics.Property_0_in_type_1_refers_to_a_different_member_that_cannot_be_accessed_from_within_type_2, privateIdentifierDescription, sourceName, targetName)
r.reportError(diagnostics.Property_0_in_type_1_refers_to_a_different_member_that_cannot_be_accessed_from_within_type_2, privateIdentifierDescription, r.c.SymbolToString(source.symbol), r.c.SymbolToString(target.symbol))
return
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
privateNameMethodClassExpression2.ts(5,6): error TS18013: Property '#method' is not accessible outside class '(Anonymous class)' because it has a private identifier.
privateNameMethodClassExpression2.ts(9,6): error TS18013: Property '#field' is not accessible outside class '(Anonymous class)' because it has a private identifier.


==== privateNameMethodClassExpression2.ts (2 errors) ====
new (class {
#method() {
return 42;
}
})().#method; // error
~~~~~~~
!!! error TS18013: Property '#method' is not accessible outside class '(Anonymous class)' because it has a private identifier.

new (class {
#field = 42;
})().#field; // error
~~~~~~
!!! error TS18013: Property '#field' is not accessible outside class '(Anonymous class)' because it has a private identifier.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/privateNameMethodClassExpression2.ts] ////

=== privateNameMethodClassExpression2.ts ===
new (class {
#method() {
>#method : Symbol(#method, Decl(privateNameMethodClassExpression2.ts, 0, 12))

return 42;
}
})().#method; // error

new (class {
#field = 42;
>#field : Symbol(#field, Decl(privateNameMethodClassExpression2.ts, 6, 12))

})().#field; // error

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/privateNameMethodClassExpression2.ts] ////

=== privateNameMethodClassExpression2.ts ===
new (class {
>new (class { #method() { return 42; }})().#method : any
>new (class { #method() { return 42; }})() : (Anonymous class)
>(class { #method() { return 42; }}) : typeof (Anonymous class)
>class { #method() { return 42; }} : typeof (Anonymous class)

#method() {
>#method : () => number

return 42;
>42 : 42
}
})().#method; // error

new (class {
>new (class { #field = 42;})().#field : any
>new (class { #field = 42;})() : (Anonymous class)
>(class { #field = 42;}) : typeof (Anonymous class)
>class { #field = 42;} : typeof (Anonymous class)

#field = 42;
>#field : number
>42 : 42

})().#field; // error

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
privateNamesUnique-6.ts(6,7): error TS2322: Type '(Anonymous class)' is not assignable to type 'A'.
Property '#foo' in type '(Anonymous class)' refers to a different member that cannot be accessed from within type 'A'.


==== privateNamesUnique-6.ts (1 errors) ====
class A {
#foo: number;
}

// error
const test: A = new (class {
~~~~
!!! error TS2322: Type '(Anonymous class)' is not assignable to type 'A'.
!!! error TS2322: Property '#foo' in type '(Anonymous class)' refers to a different member that cannot be accessed from within type 'A'.
Comment on lines +13 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strada would print (Anonymous class) in the first line here and then (anonymous) in the second line. So I think this change unifying it is a good thing.

Note this is a new test case, not one from Strada.

#foo: number;
})();

20 changes: 20 additions & 0 deletions testdata/baselines/reference/compiler/privateNamesUnique-6.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/privateNamesUnique-6.ts] ////

=== privateNamesUnique-6.ts ===
class A {
>A : Symbol(A, Decl(privateNamesUnique-6.ts, 0, 0))

#foo: number;
>#foo : Symbol(#foo, Decl(privateNamesUnique-6.ts, 0, 9))
}

// error
const test: A = new (class {
>test : Symbol(test, Decl(privateNamesUnique-6.ts, 5, 5))
>A : Symbol(A, Decl(privateNamesUnique-6.ts, 0, 0))

#foo: number;
>#foo : Symbol(#foo, Decl(privateNamesUnique-6.ts, 5, 28))

})();

22 changes: 22 additions & 0 deletions testdata/baselines/reference/compiler/privateNamesUnique-6.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/privateNamesUnique-6.ts] ////

=== privateNamesUnique-6.ts ===
class A {
>A : A

#foo: number;
>#foo : number
}

// error
const test: A = new (class {
>test : A
>new (class { #foo: number;})() : (Anonymous class)
>(class { #foo: number;}) : typeof (Anonymous class)
>class { #foo: number;} : typeof (Anonymous class)

#foo: number;
>#foo : number

})();

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
privateNameMethodClassExpression.ts(9,17): error TS18013: Property '#method' is not accessible outside class '(Missing)' because it has a private identifier.
privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is not accessible outside class '(Missing)' because it has a private identifier.
privateNameMethodClassExpression.ts(9,17): error TS18013: Property '#method' is not accessible outside class 'C' because it has a private identifier.
privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is not accessible outside class 'C' because it has a private identifier.


==== privateNameMethodClassExpression.ts (2 errors) ====
Expand All @@ -13,9 +13,9 @@ privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is
console.log(C.getInstance().getField());
C.getInstance().#method; // Error
~~~~~~~
!!! error TS18013: Property '#method' is not accessible outside class '(Missing)' because it has a private identifier.
!!! error TS18013: Property '#method' is not accessible outside class 'C' because it has a private identifier.
C.getInstance().#field; // Error
~~~~~~
!!! error TS18013: Property '#field' is not accessible outside class '(Missing)' because it has a private identifier.
!!! error TS18013: Property '#field' is not accessible outside class 'C' because it has a private identifier.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strada would print (anonymous) here but this has an inferrable~ name from the declaration and I think it's better to use it. Using it is not a new thing to do.



Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
@@= skipped -0, +0 lines =@@
-privateNameMethodClassExpression.ts(9,17): error TS18013: Property '#method' is not accessible outside class '(anonymous)' because it has a private identifier.
-privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is not accessible outside class '(anonymous)' because it has a private identifier.
+privateNameMethodClassExpression.ts(9,17): error TS18013: Property '#method' is not accessible outside class '(Missing)' because it has a private identifier.
+privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is not accessible outside class '(Missing)' because it has a private identifier.
+privateNameMethodClassExpression.ts(9,17): error TS18013: Property '#method' is not accessible outside class 'C' because it has a private identifier.
+privateNameMethodClassExpression.ts(10,17): error TS18013: Property '#field' is not accessible outside class 'C' because it has a private identifier.


==== privateNameMethodClassExpression.ts (2 errors) ====
Expand All @@ -13,10 +13,10 @@
C.getInstance().#method; // Error
~~~~~~~
-!!! error TS18013: Property '#method' is not accessible outside class '(anonymous)' because it has a private identifier.
+!!! error TS18013: Property '#method' is not accessible outside class '(Missing)' because it has a private identifier.
+!!! error TS18013: Property '#method' is not accessible outside class 'C' because it has a private identifier.
C.getInstance().#field; // Error
~~~~~~
-!!! error TS18013: Property '#field' is not accessible outside class '(anonymous)' because it has a private identifier.
+!!! error TS18013: Property '#field' is not accessible outside class '(Missing)' because it has a private identifier.
+!!! error TS18013: Property '#field' is not accessible outside class 'C' because it has a private identifier.


1 change: 1 addition & 0 deletions testdata/submoduleAccepted.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Diff files to instead write to submoduleAccepted as "accepted" changes.
conformance/privateNameMethodClassExpression.errors.txt.diff
13 changes: 13 additions & 0 deletions testdata/tests/cases/compiler/privateNameMethodClassExpression2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strict: true
// @target: es2015
// @noEmit: true

new (class {
#method() {
return 42;
}
})().#method; // error

new (class {
#field = 42;
})().#field; // error
13 changes: 13 additions & 0 deletions testdata/tests/cases/compiler/privateNamesUnique-6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strict: true
// @strictPropertyInitialization: false
// @target: es6
// @noEmit: true

class A {
#foo: number;
}

// error
const test: A = new (class {
#foo: number;
})();