8000 fix(37791): fix incorrect private field access in a computed property… · typescript-bot/TypeScript@841b1a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 841b1a5

Browse files
authored
fix(37791): fix incorrect private field access in a computed property name (microsoft#38135)
1 parent 222f29f commit 841b1a5

22 files changed

+1305
-3
lines changed

src/compiler/transformers/classFields.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ namespace ts {
9090
return visitPropertyDeclaration(node as PropertyDeclaration);
9191
case SyntaxKind.VariableStatement:
9292
return visitVariableStatement(node as VariableStatement);
93-
case SyntaxKind.ComputedPropertyName:
94-
return visitComputedPropertyName(node as ComputedPropertyName);
9593
case SyntaxKind.PropertyAccessExpression:
9694
return visitPropertyAccessExpression(node as PropertyAccessExpression);
9795
case SyntaxKind.PrefixUnaryExpression:
@@ -184,7 +182,7 @@ namespace ts {
184182
let node = visitEachChild(name, visitor, context);
185183
if (some(pendingExpressions)) {
186184
const expressions = pendingExpressions;
187-
expressions.push(name.expression);
185+
expressions.push(node.expression);
188186
pendingExpressions = [];
189187
node = factory.updateComputedPropertyName(
190188
node,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//// [privateNameComputedPropertyName1.ts]
2+
class A {
3+
#a = 'a';
4+
#b: string;
5+
6+
readonly #c = 'c';
7+
readonly #d: string;
8+
9+
#e = '';
10+
11+
constructor() {
12+
this.#b = 'b';
13+
this.#d = 'd';
14+
}
15+
16+
test() {
17+
const data: Record<string, string> = { a: 'a', b: 'b', c: 'c', d: 'd', e: 'e' };
18+
const {
19+
[this.#a]: a,
20+
[this.#b]: b,
21+
[this.#c]: c,
22+
[this.#d]: d,
23+
[this.#e = 'e']: e,
24+
} = data;
25+
console.log(a, b, c, d, e);
26+
27+
const a1 = data[this.#a];
28+
const b1 = data[this.#b];
29+
const c1 = data[this.#c];
30+
const d1 = data[this.#d];
31+
const e1 = data[this.#e];
32+
console.log(a1, b1, c1, d1);
33+
}
34+
}
35+
36+
new A().test();
37+
38+
39+
40+
//// [privateNameComputedPropertyName1.js]
41+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
42+
if (!privateMap.has(receiver)) {
43+
throw new TypeError("attempted to set private field on non-instance");
44+
}
45+
privateMap.set(receiver, value);
46+
return value;
47+
};
48+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
49+
if (!privateMap.has(receiver)) {
50+
throw new TypeError("attempted to get private field on non-instance");
51+
}
52+
return privateMap.get(receiver);
53+
};
54+
var _a, _b, _c, _d, _e;
55+
class A {
56+
constructor() {
57+
_a.set(this, 'a');
58+
_b.set(this, void 0);
59+
_c.set(this, 'c');
60+
_d.set(this, void 0);
61+
_e.set(this, '');
62+
__classPrivateFieldSet(this, _b, 'b');
63+
__classPrivateFieldSet(this, _d, 'd');
64+
}
65+
test() {
66+
const data = { a: 'a', b: 'b', c: 'c', d: 'd', e: 'e' };
67+
const { [__classPrivateFieldGet(this, _a)]: a, [__classPrivateFieldGet(this, _b)]: b, [__classPrivateFieldGet(this, _c)]: c, [__classPrivateFieldGet(this, _d)]: d, [__classPrivateFieldSet(this, _e, 'e')]: e, } = data;
68+
console.log(a, b, c, d, e);
69+
const a1 = data[__classPrivateFieldGet(this, _a)];
70+
const b1 = data[__classPrivateFieldGet(this, _b)];
71+
const c1 = data[__classPrivateFieldGet(this, _c)];
72+
const d1 = data[__classPrivateFieldGet(this, _d)];
73+
const e1 = data[__classPrivateFieldGet(this, _e)];
74+
console.log(a1, b1, c1, d1);
75+
}
76+
}
77+
_a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(), _d = new WeakMap(), _e = new WeakMap();
78+
new A().test();
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNameComputedPropertyName1.ts ===
2+
class A {
3+
>A : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
4+
5+
#a = 'a';
6+
>#a : Symbol(A.#a, Decl(privateNameComputedPropertyName1.ts, 0, 9))
7+
8+
#b: string;
9+
>#b : Symbol(A.#b, Decl(privateNameComputedPropertyName1.ts, 1, 13))
10+
11+
readonly #c = 'c';
12+
>#c : Symbol(A.#c, Decl(privateNameComputedPropertyName1.ts, 2, 15))
13+
14+
readonly #d: string;
15+
>#d : Symbol(A.#d, Decl(privateNameComputedPropertyName1.ts, 4, 22))
16+
17+
#e = '';
18+
>#e : Symbol(A.#e, Decl(privateNameComputedPropertyName1.ts, 5, 24))
19+
20+
constructor() {
21+
this.#b = 'b';
22+
>this.#b : Symbol(A.#b, Decl(privateNameComputedPropertyName1.ts, 1, 13))
23+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
24+
25+
this.#d = 'd';
26+
>this.#d : Symbol(A.#d, Decl(privateNameComputedPropertyName1.ts, 4, 22))
27+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
28+
}
29+
30+
test() {
31+
>test : Symbol(A.test, Decl(privateNameComputedPropertyName1.ts, 12, 5))
32+
33+
const data: Record<string, string> = { a: 'a', b: 'b', c: 'c', d: 'd', e: 'e' };
34+
>data : Symbol(data, Decl(privateNameComputedPropertyName1.ts, 15, 13))
35+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
36+
>a : Symbol(a, Decl(privateNameComputedPropertyName1.ts, 15, 46))
37+
>b : Symbol(b, Decl(privateNameComputedPropertyName1.ts, 15, 54))
38+
>c : Symbol(c, Decl(privateNameComputedPropertyName1.ts, 15, 62))
39+
>d : Symbol(d, Decl(privateNameComputedPropertyName1.ts, 15, 70))
40+
>e : Symbol(e, Decl(privateNameComputedPropertyName1.ts, 15, 78))
41+
42+
const {
43+
[this.#a]: a,
44+
>this.#a : Symbol(A.#a, Decl(privateNameComputedPropertyName1.ts, 0, 9))
45+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
46+
>a : Symbol(a, Decl(privateNameComputedPropertyName1.ts, 16, 15))
47+
48+
[this.#b]: b,
49+
>this.#b : Symbol(A.#b, Decl(privateNameComputedPropertyName1.ts, 1, 13))
50+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
51+
>b : Symbol(b, Decl(privateNameComputedPropertyName1.ts, 17, 25))
52+
53+
[this.#c]: c,
54+
>this.#c : Symbol(A.#c, Decl(privateNameComputedPropertyName1.ts, 2, 15))
55+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
56+
>c : Symbol(c, Decl(privateNameComputedPropertyName1.ts, 18, 25))
57+
58+
[this.#d]: d,
59+
>this.#d : Symbol(A.#d, Decl(privateNameComputedPropertyName1.ts, 4, 22))
60+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
61+
>d : Symbol(d, Decl(privateNameComputedPropertyName1.ts, 19, 25))
62+
63+
[this.#e = 'e']: e,
64+
>this.#e : Symbol(A.#e, Decl(privateNameComputedPropertyName1.ts, 5, 24))
65+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
66+
>e : Symbol(e, Decl(privateNameComputedPropertyName1.ts, 20, 25))
67+
68+
} = data;
69+
>data : Symbol(data, Decl(privateNameComputedPropertyName1.ts, 15, 13))
70+
71+
console.log(a, b, c, d, e);
72+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
73+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
74+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
75+
>a : Symbol(a, Decl(privateNameComputedPropertyName1.ts, 16, 15))
76+
>b : Symbol(b, Decl(privateNameComputedPropertyName1.ts, 17, 25))
77+
>c : Symbol(c, Decl(privateNameComputedPropertyName1.ts, 18, 25))
78+
>d : Symbol(d, Decl(privateNameComputedPropertyName1.ts, 19, 25))
79+
>e : Symbol(e, Decl(privateNameComputedPropertyName1.ts, 20, 25))
80+
81+
const a1 = data[this.#a];
82+
>a1 : Symbol(a1, Decl(privateNameComputedPropertyName1.ts, 25, 13))
83+
>data : Symbol(data, Decl(privateNameComputedPropertyName1.ts, 15, 13))
84+
>this.#a : Symbol(A.#a, Decl(privateNameComputedPropertyName1.ts, 0, 9))
85+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
86+
87+
const b1 = data[this.#b];
88+
>b1 : Symbol(b1, Decl(privateNameComputedPropertyName1.ts, 26, 13))
89+
>data : Symbol(data, Decl(privateNameComputedPropertyName1.ts, 15, 13))
90+
>this.#b : Symbol(A.#b, Decl(privateNameComputedPropertyName1.ts, 1, 13))
91+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
92+
93+
const c1 = data[this.#c];
94+
>c1 : Symbol(c1, Decl(privateNameComputedPropertyName1.ts, 27, 13))
95+
>data : Symbol(data, Decl(privateNameComputedPropertyName1.ts, 15, 13))
96+
>this.#c : Symbol(A.#c, Decl(privateNameComputedPropertyName1.ts, 2, 15))
97+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
98+
99+
const d1 = data[this.#d];
100+
>d1 : Symbol(d1, Decl(privateNameComputedPropertyName1.ts, 28, 13))
101+
>data : Symbol(data, Decl(privateNameComputedPropertyName1.ts, 15, 13))
102+
>this.#d : Symbol(A.#d, Decl(privateNameComputedPropertyName1.ts, 4, 22))
103+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
104+
105+
const e1 = data[this.#e];
106+
>e1 : Symbol(e1, Decl(privateNameComputedPropertyName1.ts, 29, 13))
107+
>data : Symbol(data, Decl(privateNameComputedPropertyName1.ts, 15, 13))
108+
>this.#e : Symbol(A.#e, Decl(privateNameComputedPropertyName1.ts, 5, 24))
109+
>this : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
110+
111+
console.log(a1, b1, c1, d1);
112+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
113+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
114+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
115+
>a1 : Symbol(a1, Decl(privateNameComputedPropertyName1.ts, 25, 13))
116+
>b1 : Symbol(b1, Decl(privateNameComputedPropertyName1.ts, 26, 13))
117+
>c1 : Symbol(c1, Decl(privateNameComputedPropertyName1.ts, 27, 13))
118+
>d1 : Symbol(d1, Decl(privateNameComputedPropertyName1.ts, 28, 13))
119+
}
120+
}
121+
122+
new A().test();
123+
>new A().test : Symbol(A.test, Decl(privateNameComputedPropertyName1.ts, 12, 5))
124+
>A : Symbol(A, Decl(privateNameComputedPropertyName1.ts, 0, 0))
125+
>test : Symbol(A.test, Decl(privateNameComputedPropertyName1.ts, 12, 5))
126+
127+
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNameComputedPropertyName1.ts ===
2+
class A {
3+
>A : A
4+
5+
#a = 'a';
6+
>#a : string
7+
>'a' : "a"
8+
9+
#b: string;
10+
>#b : string
11+
12+
readonly #c = 'c';
13+
>#c : "c"
14+
>'c' : "c"
15+
16+
readonly #d: string;
17+
>#d : string
18+
19+
#e = '';
20+
>#e : string
21+
>'' : ""
22+
23+
constructor() {
24+
this.#b = 'b';
25+
>this.#b = 'b' : "b"
26+
>this.#b : string
27+
>this : this
28+
>'b' : "b"
29+
30+
this.#d = 'd';
31+
>this.#d = 'd' : "d"
32+
>this.#d : string
33+
>this : this
34+
>'d' : "d"
35+
}
36+
37+
test() {
38+
>test : () => void
39+
40+
const data: Record<string, string> = { a: 'a', b: 'b', c: 'c', d: 'd', e: 'e' };
41+
>data : Record<string, string>
42+
>{ a: 'a', b: 'b', c: 'c', d: 'd', e: 'e' } : { a: string; b: string; c: string; d: string; e: string; }
43+
>a : string
44+
>'a' : "a"
45+
>b : string
46+
>'b' : "b"
47+
>c : string
48+
>'c' : "c"
49+
>d : string
50+
>'d' : "d"
51+
>e : string
52+
>'e' : "e"
53+
54+
const {
55+
[this.#a]: a,
56+
>this.#a : string
57+
>this : this
58+
>a : string
59+
60+
[this.#b]: b,
61+
>this.#b : string
62+
>this : this
63+
>b : string
64+
65+
[this.#c]: c,
66+
>this.#c : "c"
67+
>this : this
68+
>c : string
69+
70+
[this.#d]: d,
71+
>this.#d : string
72+
>this : this
73+
>d : string
74+
75+
[this.#e = 'e']: e,
76+
>this.#e = 'e' : "e"
77+
>this.#e : string
78+
>this : this
79+
>'e' : "e"
80+
>e : string
81+
82+
} = data;
83+
>data : Record<string, string>
84+
85+
console.log(a, b, c, d, e);
86+
>console.log(a, b, c, d, e) : void
87+
>console.log : (...data: any[]) => void
88+
>console : Console
89+
>log : (...data: any[]) => void
90+
>a : string
91+
>b : string
92+
>c : string
93+
>d : string
94+
>e : string
95+
96+
const a1 = data[this.#a];
97+
>a1 : string
98+
>data[this.#a] : string
99+
>data : Record<string, string>
100+
>this.#a : string
101+
>this : this
102+
103+
const b1 = data[this.#b];
104+
>b1 : string
105+
>data[this.#b] : string
106+
>data : Record<string, string>
107+
>this.#b : string
108+
>this : this
109+
110+
const c1 = data[this.#c];
111+
>c1 : string
112+
>data[this.#c] : string
113+
>data : Record<string, string>
114+
>this.#c : "c"
115+
>this : this
116+
117+
const d1 = data[this.#d];
118+
>d1 : string
119+
>data[this.#d] : string
120+
>data : Record<string, string>
121+
>this.#d : string
122+
>this : this
123+
124+
const e1 = data[this.#e];
125+
>e1 : string
126+
>data[this.#e] : string
127+
>data : Record<string, string>
128+
>this.#e : string
129+
>this : this
130+
131+
console.log(a1, b1, c1, d1);
132+
>console.log(a1, b1, c1, d1) : void
133+
>console.log : (...data: any[]) => void
134+
>console : Console
135+
>log : (...data: any[]) => void
136+
>a1 : string
137+
>b1 : string
138+
>c1 : string
139+
>d1 : string
140+
}
141+
}
142+
143+
new A().test();
144+
>new A().test() : void
145+
>new A().test : () => void
146+
>new A() : A
147+
>A : typeof A
148+
>test : () => void
149+
150+

0 commit comments

Comments
 (0)
0