Skip to content

Commit 7611c5b

Browse files
authored
Fix for computed properties in instance initializers (#31517)
1 parent b36c8a0 commit 7611c5b

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4527,6 +4527,8 @@ namespace ts {
45274527
case SyntaxKind.GetAccessor:
45284528
case SyntaxKind.SetAccessor:
45294529
return generateNameForMethodOrAccessor(<MethodDeclaration | AccessorDeclaration>node);
4530+
case SyntaxKind.ComputedPropertyName:
4531+
return makeTempVariableName(TempFlags.Auto, /*reserveInNestedScopes*/ true);
45304532
default:
45314533
return makeTempVariableName(TempFlags.Auto);
45324534
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [instanceMemberWithComputedPropertyName.ts]
2+
// https://github.com/microsoft/TypeScript/issues/30953
3+
const x = 1;
4+
class C {
5+
[x] = true;
6+
constructor() {
7+
const { a, b } = { a: 1, b: 2 };
8+
}
9+
}
10+
11+
//// [instanceMemberWithComputedPropertyName.js]
12+
var _a;
13+
// https://github.com/microsoft/TypeScript/issues/30953
14+
var x = 1;
15+
var C = /** @class */ (function () {
16+
function C() {
17+
this[_a] = true;
18+
var _b = { a: 1, b: 2 }, a = _b.a, b = _b.b;
19+
}
20+
return C;
21+
}());
22+
_a = x;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/instanceMemberWithComputedPropertyName.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/30953
3+
const x = 1;
4+
>x : Symbol(x, Decl(instanceMemberWithComputedPropertyName.ts, 1, 5))
5+
6+
class C {
7+
>C : Symbol(C, Decl(instanceMemberWithComputedPropertyName.ts, 1, 12))
8+
9+
[x] = true;
10+
>[x] : Symbol(C[x], Decl(instanceMemberWithComputedPropertyName.ts, 2, 9))
11+
>x : Symbol(x, Decl(instanceMemberWithComputedPropertyName.ts, 1, 5))
12+
13+
constructor() {
14+
const { a, b } = { a: 1, b: 2 };
15+
>a : Symbol(a, Decl(instanceMemberWithComputedPropertyName.ts, 5, 15))
16+
>b : Symbol(b, Decl(instanceMemberWithComputedPropertyName.ts, 5, 18))
17+
>a : Symbol(a, Decl(instanceMemberWithComputedPropertyName.ts, 5, 26))
18+
>b : Symbol(b, Decl(instanceMemberWithComputedPropertyName.ts, 5, 32))
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/instanceMemberWithComputedPropertyName.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/30953
3+
const x = 1;
4+
>x : 1
5+
>1 : 1
6+
7+
class C {
8+
>C : C
9+
10+
[x] = true;
11+
>[x] : boolean
12+
>x : 1
13+
>true : true
14+
15+
constructor() {
16+
const { a, b } = { a: 1, b: 2 };
17+
>a : number
18+
>b : number
19+
>{ a: 1, b: 2 } : { a: number; b: number; }
20+
>a : number
21+
>1 : 1
22+
>b : number
23+
>2 : 2
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// https://github.com/microsoft/TypeScript/issues/30953
2+
const x = 1;
3+
class C {
4+
[x] = true;
5+
constructor() {
6+
const { a, b } = { a: 1, b: 2 };
7+
}
8+
}

0 commit comments

Comments
 (0)