Skip to content

Commit 427cce4

Browse files
Added tests for private static fields methods and accessors.
1 parent c00c5e0 commit 427cce4

File tree

108 files changed

+4671
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+4671
-15
lines changed
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts(6,13): error TS2339: Property '#prop' does not exist on type '{}'.
2-
tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts(8,14): error TS2339: Property '#prop' does not exist on type '{}'.
1+
tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts(7,13): error TS2339: Property '#prop' does not exist on type '{}'.
2+
tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts(9,14): error TS2339: Property '#prop' does not exist on type '{}'.
3+
tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts(12,17): error TS2339: Property '#propStatic' does not exist on type '{ prototype: C; }'.
4+
tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts(14,15): error TS2339: Property '#propStatic' does not exist on type '{ prototype: C; }'.
35

46

5-
==== tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts (2 errors) ====
7+
==== tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpread.ts (4 errors) ====
68
class C {
79
#prop = 1;
10+
static #propStatic = 1;
811

912
method(other: C) {
1013
const obj = { ...other };
@@ -15,5 +18,14 @@ tests/cases/conformance/classes/members/privateNames/privateNameAndObjectRestSpr
1518
rest.#prop;
1619
~~~~~
1720
!!! error TS2339: Property '#prop' does not exist on type '{}'.
21+
22+
const statics = { ... C};
23+
statics.#propStatic
24+
~~~~~~~~~~~
25+
!!! error TS2339: Property '#propStatic' does not exist on type '{ prototype: C; }'.
26+
const { ...sRest } = C;
27+
sRest.#propStatic;
28+
~~~~~~~~~~~
29+
!!! error TS2339: Property '#propStatic' does not exist on type '{ prototype: C; }'.
1830
}
1931
}

tests/baselines/reference/privateNameAndObjectRestSpread.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
//// [privateNameAndObjectRestSpread.ts]
22
class C {
33
#prop = 1;
4+
static #propStatic = 1;
45

56
method(other: C) {
67
const obj = { ...other };
78
obj.#prop;
89
const { ...rest } = other;
910
rest.#prop;
11+
12+
const statics = { ... C};
13+
statics.#propStatic
14+
const { ...sRest } = C;
15+
sRest.#propStatic;
1016
}
1117
}
1218

@@ -18,6 +24,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
1824
}
1925
return privateMap.get(receiver);
2026
};
27+
var __classStaticPrivateFieldGet = (this && this.__classStaticPrivateFieldGet) || function (receiver, classConstructor, propertyDescriptor) {
28+
if (receiver !== classConstructor) {
29+
throw new TypeError("Private static access of wrong provenance");
30+
}
31+
return propertyDescriptor.value;
32+
};
2133
var __rest = (this && this.__rest) || function (s, e) {
2234
var t = {};
2335
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
@@ -29,7 +41,7 @@ var __rest = (this && this.__rest) || function (s, e) {
2941
}
3042
return t;
3143
};
32-
var _C_prop;
44+
var _C_prop, _C_propStatic;
3345
class C {
3446
constructor() {
3547
_C_prop.set(this, 1);
@@ -39,6 +51,11 @@ class C {
3951
__classPrivateFieldGet(obj, _C_prop);
4052
const rest = __rest(other, []);
4153
__classPrivateFieldGet(rest, _C_prop);
54+
const statics = Object.assign({}, C);
55+
__classStaticPrivateFieldGet(statics, C, _C_propStatic);
56+
const sRest = __rest(C, []);
57+
__classStaticPrivateFieldGet(sRest, C, _C_propStatic);
4258
}
4359
}
4460
_C_prop = new WeakMap();
61+
_C_propStatic = { value: 1 };

tests/baselines/reference/privateNameAndObjectRestSpread.symbols

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,40 @@ class C {
55
#prop = 1;
66
>#prop : Symbol(C.#prop, Decl(privateNameAndObjectRestSpread.ts, 0, 9))
77

8+
static #propStatic = 1;
9+
>#propStatic : Symbol(C.#propStatic, Decl(privateNameAndObjectRestSpread.ts, 1, 14))
10+
811
method(other: C) {
9-
>method : Symbol(C.method, Decl(privateNameAndObjectRestSpread.ts, 1, 14))
10-
>other : Symbol(other, Decl(privateNameAndObjectRestSpread.ts, 3, 11))
12+
>method : Symbol(C.method, Decl(privateNameAndObjectRestSpread.ts, 2, 27))
13+
>other : Symbol(other, Decl(privateNameAndObjectRestSpread.ts, 4, 11))
1114
>C : Symbol(C, Decl(privateNameAndObjectRestSpread.ts, 0, 0))
1215

1316
const obj = { ...other };
14-
>obj : Symbol(obj, Decl(privateNameAndObjectRestSpread.ts, 4, 13))
15-
>other : Symbol(other, Decl(privateNameAndObjectRestSpread.ts, 3, 11))
17+
>obj : Symbol(obj, Decl(privateNameAndObjectRestSpread.ts, 5, 13))
18+
>other : Symbol(other, Decl(privateNameAndObjectRestSpread.ts, 4, 11))
1619

1720
obj.#prop;
18-
>obj : Symbol(obj, Decl(privateNameAndObjectRestSpread.ts, 4, 13))
21+
>obj : Symbol(obj, Decl(privateNameAndObjectRestSpread.ts, 5, 13))
1922

2023
const { ...rest } = other;
21-
>rest : Symbol(rest, Decl(privateNameAndObjectRestSpread.ts, 6, 15))
22-
>other : Symbol(other, Decl(privateNameAndObjectRestSpread.ts, 3, 11))
24+
>rest : Symbol(rest, Decl(privateNameAndObjectRestSpread.ts, 7, 15))
25+
>other : Symbol(other, Decl(privateNameAndObjectRestSpread.ts, 4, 11))
2326

2427
rest.#prop;
25-
>rest : Symbol(rest, Decl(privateNameAndObjectRestSpread.ts, 6, 15))
28+
>rest : Symbol(rest, Decl(privateNameAndObjectRestSpread.ts, 7, 15))
29+
30+
const statics = { ... C};
31+
>statics : Symbol(statics, Decl(privateNameAndObjectRestSpread.ts, 10, 13))
32+
>C : Symbol(C, Decl(privateNameAndObjectRestSpread.ts, 0, 0))
33+
34+
statics.#propStatic
35+
>statics : Symbol(statics, Decl(privateNameAndObjectRestSpread.ts, 10, 13))
36+
37+
const { ...sRest } = C;
38+
>sRest : Symbol(sRest, Decl(privateNameAndObjectRestSpread.ts, 12, 15))
39+
>C : Symbol(C, Decl(privateNameAndObjectRestSpread.ts, 0, 0))
40+
41+
sRest.#propStatic;
42+
>sRest : Symbol(sRest, Decl(privateNameAndObjectRestSpread.ts, 12, 15))
2643
}
2744
}

tests/baselines/reference/privateNameAndObjectRestSpread.types

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class C {
44

55
#prop = 1;
66
>#prop : number
7+
>1 : 1
8+
9+
static #propStatic = 1;
10+
>#propStatic : number
711
>1 : 1
812

913
method(other: C) {
@@ -26,5 +30,22 @@ class C {
2630
rest.#prop;
2731
>rest.#prop : any
2832
>rest : {}
33+
34+
const statics = { ... C};
35+
>statics : { prototype: C; }
36+
>{ ... C} : { prototype: C; }
37+
>C : typeof C
38+
39+
statics.#propStatic
40+
>statics.#propStatic : any
41+
>statics : { prototype: C; }
42+
43+
const { ...sRest } = C;
44+
>sRest : { prototype: C; }
45+
>C : typeof C
46+
47+
sRest.#propStatic;
48+
>sRest.#propStatic : any
49+
>sRest : { prototype: C; }
2950
}
3051
}

tests/baselines/reference/privateNameFieldsESNext.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ class C {
3535
this.#a = "hello";
3636
console.log(this.#b);
3737
}
38-
static #m;
38+
static #m = "test";
3939
static #x;
4040
static test() {
4141
console.log(this.#m);
4242
console.log(this.#x = "test");
4343
}
4444
#something;
4545
}
46-
C.#m = "test";
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessors.ts(9,12): error TS2540: Cannot assign to '#roProp' because it is a read-only property.
2+
3+
4+
==== tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessors.ts (1 errors) ====
5+
class A1 {
6+
static get #prop() { return ""; }
7+
static set #prop(param: string) { }
8+
9+
static get #roProp() { return ""; }
10+
11+
constructor(name: string) {
12+
A1.#prop = "";
13+
A1.#roProp = ""; // Error
14+
~~~~~~~
15+
!!! error TS2540: Cannot assign to '#roProp' because it is a read-only property.
16+
console.log(A1.#prop);
17+
console.log(A1.#roProp);
18+
}
19+
}
20+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [privateNameStaticAccessors.ts]
2+
class A1 {
3+
static get #prop() { return ""; }
4+
static set #prop(param: string) { }
5+
6+
static get #roProp() { return ""; }
7+
8+
constructor(name: string) {
9+
A1.#prop = "";
10+
A1.#roProp = ""; // Error
11+
console.log(A1.#prop);
12+
console.log(A1.#roProp);
13+
}
14+
}
15+
16+
17+
//// [privateNameStaticAccessors.js]
18+
"use strict";
19+
var __classStaticPrivateAccessorSet = (this && this.__classStaticPrivateAccessorSet) || function (receiver, classConstructor, fn, value) {
20+
if (receiver !== classConstructor) {
21+
throw new TypeError("Private static access of wrong provenance");
22+
}
23+
fn.call(receiver, value);
24+
return value;
25+
};
26+
var __classStaticPrivateReadonly = (this && this.__classStaticPrivateReadonly) || function () {
27+
throw new TypeError("Private static element is not writable");
28+
};
29+
var __classStaticPrivateAccessorGet = (this && this.__classStaticPrivateAccessorGet) || function (receiver, classConstructor, fn) {
30+
if (receiver !== classConstructor) {
31+
throw new TypeError("Private static access of wrong provenance");
32+
}
33+
return fn.call(receiver);
34+
};
35+
var _A1_prop_get, _A1_prop_set, _A1_roProp_get;
36+
class A1 {
37+
constructor(name) {
38+
__classStaticPrivateAccessorSet(A1, A1, _A1_prop_set, "");
39+
__classStaticPrivateReadonly(A1, ""); // Error
40+
console.log(__classStaticPrivateAccessorGet(A1, A1, _A1_prop_get));
41+
console.log(__classStaticPrivateAccessorGet(A1, A1, _A1_roProp_get));
42+
}
43+
}
44+
_A1_prop_get = function _A1_prop_get() { return ""; }, _A1_prop_set = function _A1_prop_set(param) { }, _A1_roProp_get = function _A1_roProp_get() { return ""; };
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessors.ts ===
2+
class A1 {
3+
>A1 : Symbol(A1, Decl(privateNameStaticAccessors.ts, 0, 0))
4+
5+
static get #prop() { return ""; }
6+
>#prop : Symbol(A1.#prop, Decl(privateNameStaticAccessors.ts, 0, 10), Decl(privateNameStaticAccessors.ts, 1, 37))
7+
8+
static set #prop(param: string) { }
9+
>#prop : Symbol(A1.#prop, Decl(privateNameStaticAccessors.ts, 0, 10), Decl(privateNameStaticAccessors.ts, 1, 37))
10+
>param : Symbol(param, Decl(privateNameStaticAccessors.ts, 2, 21))
11+
12+
static get #roProp() { return ""; }
13+
>#roProp : Symbol(A1.#roProp, Decl(privateNameStaticAccessors.ts, 2, 39))
14+
15+
constructor(name: string) {
16+
>name : Symbol(name, Decl(privateNameStaticAccessors.ts, 6, 16))
17+
18+
A1.#prop = "";
19+
>A1.#prop : Symbol(A1.#prop, Decl(privateNameStaticAccessors.ts, 0, 10), Decl(privateNameStaticAccessors.ts, 1, 37))
20+
>A1 : Symbol(A1, Decl(privateNameStaticAccessors.ts, 0, 0))
21+
22+
A1.#roProp = ""; // Error
23+
>A1.#roProp : Symbol(A1.#roProp, Decl(privateNameStaticAccessors.ts, 2, 39))
24+
>A1 : Symbol(A1, Decl(privateNameStaticAccessors.ts, 0, 0))
25+
26+
console.log(A1.#prop);
27+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
28+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
29+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
30+
>A1.#prop : Symbol(A1.#prop, Decl(privateNameStaticAccessors.ts, 0, 10), Decl(privateNameStaticAccessors.ts, 1, 37))
31+
>A1 : Symbol(A1, Decl(privateNameStaticAccessors.ts, 0, 0))
32+
33+
console.log(A1.#roProp);
34+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
35+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
36+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
37+
>A1.#roProp : Symbol(A1.#roProp, Decl(privateNameStaticAccessors.ts, 2, 39))
38+
>A1 : Symbol(A1, Decl(privateNameStaticAccessors.ts, 0, 0))
39+
}
40+
}
41+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessors.ts ===
2+
class A1 {
3+
>A1 : A1
4+
5+
static get #prop() { return ""; }
6+
>#prop : string
7+
>"" : ""
8+
9+
static set #prop(param: string) { }
10+
>#prop : string
11+
>param : string
12+
13+
static get #roProp() { return ""; }
14+
>#roProp : string
15+
>"" : ""
16+
17+
constructor(name: string) {
18+
>name : string
19+
20+
A1.#prop = "";
21+
>A1.#prop = "" : ""
22+
>A1.#prop : string
23+
>A1 : typeof A1
24+
>"" : ""
25+
26+
A1.#roProp = ""; // Error
27+
>A1.#roProp = "" : ""
28+
>A1.#roProp : any
29+
>A1 : typeof A1
30+
>"" : ""
31+
32+
console.log(A1.#prop);
33+
>console.log(A1.#prop) : void
34+
>console.log : (...data: any[]) => void
35+
>console : Console
36+
>log : (...data: any[]) => void
37+
>A1.#prop : string
38+
>A1 : typeof A1
39+
40+
console.log(A1.#roProp);
41+
>console.log(A1.#roProp) : void
42+
>console.log : (...data: any[]) => void
43+
>console : Console
44+
>log : (...data: any[]) => void
45+
>A1.#roProp : string
46+
>A1 : typeof A1
47+
}
48+
}
49+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessorsAccess.ts(16,4): error TS18013: Property '#prop' is not accessible outside class 'A2' because it has a private identifier.
2+
tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessorsAccess.ts(19,8): error TS18013: Property '#prop' is not accessible outside class 'A2' because it has a private identifier.
3+
tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessorsAccess.ts(24,12): error TS18013: Property '#prop' is not accessible outside class 'A2' because it has a private identifier.
4+
5+
6+
==== tests/cases/conformance/classes/members/privateNames/privateNameStaticAccessorsAccess.ts (3 errors) ====
7+
export {}
8+
class A2 {
9+
static get #prop() { return ""; }
10+
static set #prop(param: string) { }
11+
12+
constructor() {
13+
console.log(A2.#prop);
14+
let a: typeof A2 = A2;
15+
a.#prop;
16+
function foo (){
17+
a.#prop;
18+
}
19+
}
20+
}
21+
22+
A2.#prop; // Error
23+
~~~~~
24+
!!! error TS18013: Property '#prop' is not accessible outside class 'A2' because it has a private identifier.
25+
26+
function foo (){
27+
A2.#prop; // Error
28+
~~~~~
29+
!!! error TS18013: Property '#prop' is not accessible outside class 'A2' because it has a private identifier.
30+
}
31+
32+
class B2 {
33+
m() {
34+
A2.#prop;
35+
~~~~~
36+
!!! error TS18013: Property '#prop' is not accessible outside class 'A2' because it has a private identifier.
37+
}
38+
}
39+

0 commit comments

Comments
 (0)