Skip to content

Commit e7bd898

Browse files
committed
Improve tests for private identifiers in optional chains
1 parent 0156c57 commit e7bd898

9 files changed

+510
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
privateIdentifierChain.1.ts(23,17): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
2+
privateIdentifierChain.1.ts(24,17): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
3+
privateIdentifierChain.1.ts(25,22): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
4+
5+
6+
==== privateIdentifierChain.1.ts (3 errors) ====
7+
class A {
8+
a?: A
9+
#b?: A;
10+
getA(): A {
11+
return new A();
12+
}
13+
constructor() {
14+
this.a = this;
15+
// None of these should error
16+
this?.#b;
17+
this?.a.#b;
18+
this?.getA().#b;
19+
}
20+
}
21+
22+
class B {
23+
a?: A
24+
getA(): A {
25+
return new A();
26+
}
27+
constructor() {
28+
this.a = new A();
29+
this.a?.#b; // Error
30+
~~
31+
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
32+
this?.a.#b; // Error
33+
~~
34+
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
35+
this?.getA().#b; // Error
36+
~~
37+
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
38+
}
39+
}
40+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//// [tests/cases/conformance/expressions/optionalChaining/privateIdentifierChain/privateIdentifierChain.1.ts] ////
2+
3+
//// [privateIdentifierChain.1.ts]
4+
class A {
5+
a?: A
6+
#b?: A;
7+
getA(): A {
8+
return new A();
9+
}
10+
constructor() {
11+
this.a = this;
12+
// None of these should error
13+
this?.#b;
14+
this?.a.#b;
15+
this?.getA().#b;
16+
}
17+
}
18+
19+
class B {
20+
a?: A
21+
getA(): A {
22+
return new A();
23+
}
24+
constructor() {
25+
this.a = new A();
26+
this.a?.#b; // Error
27+
this?.a.#b; // Error
28+
this?.getA().#b; // Error
29+
}
30+
}
31+
32+
33+
//// [privateIdentifierChain.1.js]
34+
"use strict";
35+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
36+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
37+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
38+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
39+
};
40+
var _A_b;
41+
class A {
42+
getA() {
43+
return new A();
44+
}
45+
constructor() {
46+
_A_b.set(this, void 0);
47+
this.a = this;
48+
// None of these should error
49+
__classPrivateFieldGet(this, _A_b, "f");
50+
__classPrivateFieldGet(this === null || this === void 0 ? void 0 : this.a, _A_b, "f");
51+
__classPrivateFieldGet(this === null || this === void 0 ? void 0 : this.getA(), _A_b, "f");
52+
}
53+
}
54+
_A_b = new WeakMap();
55+
class B {
56+
getA() {
57+
return new A();
58+
}
59+
constructor() {
60+
var _a;
61+
this.a = new A();
62+
(_a = this.a) === null || _a === void 0 ? void 0 : _a.; // Error
63+
this === null || this === void 0 ? void 0 : this.a.; // Error
64+
this === null || this === void 0 ? void 0 : this.getA().; // Error
65+
}
66+
}

tests/baselines/reference/privateIdentifierChain.1.symbols renamed to tests/baselines/reference/privateIdentifierChain.1(target=es2015).symbols

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,41 @@ class A {
4545
}
4646
}
4747

48+
class B {
49+
>B : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
50+
51+
a?: A
52+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
53+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
54+
55+
getA(): A {
56+
>getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
57+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
58+
59+
return new A();
60+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
61+
}
62+
constructor() {
63+
this.a = new A();
64+
>this.a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
65+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
66+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
67+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
68+
69+
this.a?.#b; // Error
70+
>this.a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
71+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
72+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
73+
74+
this?.a.#b; // Error
75+
>this?.a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
76+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
77+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
78+
79+
this?.getA().#b; // Error
80+
>this?.getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
81+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
82+
>getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
83+
}
84+
}
85+

tests/baselines/reference/privateIdentifierChain.1.types renamed to tests/baselines/reference/privateIdentifierChain.1(target=es2015).types

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,70 @@ class A {
6767
}
6868
}
6969

70+
class B {
71+
>B : B
72+
> : ^
73+
74+
a?: A
75+
>a : A | undefined
76+
> : ^^^^^^^^^^^^^
77+
78+
getA(): A {
79+
>getA : () => A
80+
> : ^^^^^^
81+
82+
return new A();
83+
>new A() : A
84+
> : ^
85+
>A : typeof A
86+
> : ^^^^^^^^
87+
}
88+
constructor() {
89+
this.a = new A();
90+
>this.a = new A() : A
91+
> : ^
92+
>this.a : A | undefined
93+
> : ^^^^^^^^^^^^^
94+
>this : this
95+
> : ^^^^
96+
>a : A | undefined
97+
> : ^^^^^^^^^^^^^
98+
>new A() : A
99+
> : ^
100+
>A : typeof A
101+
> : ^^^^^^^^
102+
103+
this.a?.#b; // Error
104+
>this.a?.#b : any
105+
> : ^^^
106+
>this.a : A
107+
> : ^
108+
>this : this
109+
> : ^^^^
110+
>a : A
111+
> : ^
112+
113+
this?.a.#b; // Error
114+
>this?.a.#b : any
115+
> : ^^^
116+
>this?.a : A
117+
> : ^
118+
>this : this
119+
> : ^^^^
120+
>a : A
121+
> : ^
122+
123+
this?.getA().#b; // Error
124+
>this?.getA().#b : any
125+
> : ^^^
126+
>this?.getA() : A
127+
> : ^
128+
>this?.getA : () => A
129+
> : ^^^^^^
130+
>this : this
131+
> : ^^^^
132+
>getA : () => A
133+
> : ^^^^^^
134+
}
135+
}
136+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
privateIdentifierChain.1.ts(23,17): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
2+
privateIdentifierChain.1.ts(24,17): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
3+
privateIdentifierChain.1.ts(25,22): error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
4+
5+
6+
==== privateIdentifierChain.1.ts (3 errors) ====
7+
class A {
8+
a?: A
9+
#b?: A;
10+
getA(): A {
11+
return new A();
12+
}
13+
constructor() {
14+
this.a = this;
15+
// None of these should error
16+
this?.#b;
17+
this?.a.#b;
18+
this?.getA().#b;
19+
}
20+
}
21+
22+
class B {
23+
a?: A
24+
getA(): A {
25+
return new A();
26+
}
27+
constructor() {
28+
this.a = new A();
29+
this.a?.#b; // Error
30+
~~
31+
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
32+
this?.a.#b; // Error
33+
~~
34+
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
35+
this?.getA().#b; // Error
36+
~~
37+
!!! error TS18013: Property '#b' is not accessible outside class 'A' because it has a private identifier.
38+
}
39+
}
40+

tests/baselines/reference/privateIdentifierChain.1.js renamed to tests/baselines/reference/privateIdentifierChain.1(target=esnext).js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ class A {
1515
this?.getA().#b;
1616
}
1717
}
18+
19+
class B {
20+
a?: A
21+
getA(): A {
22+
return new A();
23+
}
24+
constructor() {
25+
this.a = new A();
26+
this.a?.#b; // Error
27+
this?.a.#b; // Error
28+
this?.getA().#b; // Error
29+
}
30+
}
1831

1932

2033
//// [privateIdentifierChain.1.js]
@@ -32,3 +45,14 @@ class A {
3245
this?.getA().#b;
3346
}
3447
}
48+
class B {
49+
getA() {
50+
return new A();
51+
}
52+
constructor() {
53+
this.a = new A();
54+
this.a?.#b; // Error
55+
this?.a.#b; // Error
56+
this?.getA().#b; // Error
57+
}
58+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//// [tests/cases/conformance/expressions/optionalChaining/privateIdentifierChain/privateIdentifierChain.1.ts] ////
2+
3+
=== privateIdentifierChain.1.ts ===
4+
class A {
5+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
6+
7+
a?: A
8+
>a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
9+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
10+
11+
#b?: A;
12+
>#b : Symbol(A.#b, Decl(privateIdentifierChain.1.ts, 1, 9))
13+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
14+
15+
getA(): A {
16+
>getA : Symbol(A.getA, Decl(privateIdentifierChain.1.ts, 2, 11))
17+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
18+
19+
return new A();
20+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
21+
}
22+
constructor() {
23+
this.a = this;
24+
>this.a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
25+
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
26+
>a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
27+
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
28+
29+
// None of these should error
30+
this?.#b;
31+
>this?.#b : Symbol(A.#b, Decl(privateIdentifierChain.1.ts, 1, 9))
32+
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
33+
34+
this?.a.#b;
35+
>this?.a.#b : Symbol(A.#b, Decl(privateIdentifierChain.1.ts, 1, 9))
36+
>this?.a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
37+
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
38+
>a : Symbol(A.a, Decl(privateIdentifierChain.1.ts, 0, 9))
39+
40+
this?.getA().#b;
41+
>this?.getA().#b : Symbol(A.#b, Decl(privateIdentifierChain.1.ts, 1, 9))
42+
>this?.getA : Symbol(A.getA, Decl(privateIdentifierChain.1.ts, 2, 11))
43+
>this : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
44+
>getA : Symbol(A.getA, Decl(privateIdentifierChain.1.ts, 2, 11))
45+
}
46+
}
47+
48+
class B {
49+
>B : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
50+
51+
a?: A
52+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
53+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
54+
55+
getA(): A {
56+
>getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
57+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
58+
59+
return new A();
60+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
61+
}
62+
constructor() {
63+
this.a = new A();
64+
>this.a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
65+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
66+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
67+
>A : Symbol(A, Decl(privateIdentifierChain.1.ts, 0, 0))
68+
69+
this.a?.#b; // Error
70+
>this.a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
71+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
72+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
73+
74+
this?.a.#b; // Error
75+
>this?.a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
76+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
77+
>a : Symbol(B.a, Decl(privateIdentifierChain.1.ts, 15, 9))
78+
79+
this?.getA().#b; // Error
80+
>this?.getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
81+
>this : Symbol(B, Decl(privateIdentifierChain.1.ts, 13, 1))
82+
>getA : Symbol(B.getA, Decl(privateIdentifierChain.1.ts, 16, 9))
83+
}
84+
}
85+

0 commit comments

Comments
 (0)