Skip to content
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

Fix for crash on call expression with private identifiers. #42861

Merged
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
11 changes: 10 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21978,7 +21978,16 @@ namespace ts {
const type = getTypeOfDottedName((<PropertyAccessExpression>node).expression, diagnostic);
if (type) {
const name = (<PropertyAccessExpression>node).name;
const prop = getPropertyOfType(type, isPrivateIdentifier(name) ? getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText) : name.escapedText);
let prop: Symbol | undefined;
if (isPrivateIdentifier(name)) {
if (!type.symbol) {
Copy link
Member

Choose a reason for hiding this comment

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

Hm, I dislike relying on type.symbol, but it seems like we can only guarantee getNodeLinks((<PropertyAccessExpression>node).expression).resolvedSymbol exists for Identifier LHSs, since as getTypeOfDottedName recurs, it doesn't set it on other interesting LHS node kinds, like checkPropertyAccessExpressionOrQualifiedName sometimes does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially had an isAnyLike check since I discovered the issue on any, but never and unknown also trigger the issue.

return undefined;
}
prop = getPropertyOfType(type, getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText));
}
else {
prop = getPropertyOfType(type, name.escapedText);
}
return prop && getExplicitTypeOfSymbol(prop, diagnostic);
}
return undefined;
Expand Down
34 changes: 33 additions & 1 deletion tests/baselines/reference/privateNameAndAny.errors.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(5,15): error TS2339: Property '#bar' does not exist on type 'any'.
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(9,9): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(10,9): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(10,15): error TS2339: Property '#bar' does not exist on type 'any'.
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(11,9): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(14,15): error TS2339: Property '#foo' does not exist on type 'never'.
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(15,15): error TS2339: Property '#bar' does not exist on type 'never'.
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(16,15): error TS2339: Property '#foo' does not exist on type 'never'.


==== tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts (1 errors) ====
==== tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts (8 errors) ====
class A {
#foo = true;
method(thing: any) {
thing.#foo; // OK
thing.#bar; // Error
~~~~
!!! error TS2339: Property '#bar' does not exist on type 'any'.
thing.#foo();
}
methodU(thing: unknown) {
thing.#foo;
~~~~~
!!! error TS2571: Object is of type 'unknown'.
thing.#bar;
~~~~~
!!! error TS2571: Object is of type 'unknown'.
~~~~
!!! error TS2339: Property '#bar' does not exist on type 'any'.
thing.#foo();
~~~~~
!!! error TS2571: Object is of type 'unknown'.
}
methodN(thing: never) {
thing.#foo;
~~~~
!!! error TS2339: Property '#foo' does not exist on type 'never'.
thing.#bar;
~~~~
!!! error TS2339: Property '#bar' does not exist on type 'never'.
thing.#foo();
~~~~
!!! error TS2339: Property '#foo' does not exist on type 'never'.
}
};

23 changes: 23 additions & 0 deletions tests/baselines/reference/privateNameAndAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ class A {
method(thing: any) {
thing.#foo; // OK
thing.#bar; // Error
thing.#foo();
}
methodU(thing: unknown) {
thing.#foo;
thing.#bar;
thing.#foo();
}
methodN(thing: never) {
thing.#foo;
thing.#bar;
thing.#foo();
}
};

Expand All @@ -24,6 +35,18 @@ class A {
method(thing) {
__classPrivateFieldGet(thing, _foo); // OK
thing.; // Error
__classPrivateFieldGet(thing, _foo).call(// Error
thing);
}
methodU(thing) {
__classPrivateFieldGet(thing, _foo);
thing.;
__classPrivateFieldGet(thing, _foo).call(thing);
}
methodN(thing) {
__classPrivateFieldGet(thing, _foo);
thing.;
__classPrivateFieldGet(thing, _foo).call(thing);
}
}
_foo = new WeakMap();
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/privateNameAndAny.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ class A {

thing.#bar; // Error
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 2, 11))

thing.#foo();
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 2, 11))
}
methodU(thing: unknown) {
>methodU : Symbol(A.methodU, Decl(privateNameAndAny.ts, 6, 5))
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))

thing.#foo;
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))

thing.#bar;
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))

thing.#foo();
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))
}
methodN(thing: never) {
>methodN : Symbol(A.methodN, Decl(privateNameAndAny.ts, 11, 5))
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))

thing.#foo;
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))

thing.#bar;
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))

thing.#foo();
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))
}
};

39 changes: 39 additions & 0 deletions tests/baselines/reference/privateNameAndAny.types
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,45 @@ class A {
thing.#bar; // Error
>thing.#bar : any
>thing : any

thing.#foo();
>thing.#foo() : any
>thing.#foo : any
>thing : any
}
methodU(thing: unknown) {
>methodU : (thing: unknown) => void
>thing : unknown

thing.#foo;
>thing.#foo : any
>thing : unknown

thing.#bar;
>thing.#bar : any
>thing : unknown

thing.#foo();
>thing.#foo() : any
>thing.#foo : any
>thing : unknown
}
methodN(thing: never) {
>methodN : (thing: never) => void
>thing : never

thing.#foo;
>thing.#foo : any
>thing : never

thing.#bar;
>thing.#bar : any
>thing : never

thing.#foo();
>thing.#foo() : any
>thing.#foo : any
>thing : never
}
};

Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ class A {
method(thing: any) {
thing.#foo; // OK
thing.#bar; // Error
thing.#foo();
}
methodU(thing: unknown) {
thing.#foo;
thing.#bar;
thing.#foo();
}
methodN(thing: never) {
thing.#foo;
thing.#bar;
thing.#foo();
}
};