Skip to content

Fix for computed properties in instance initializers #31517

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

Merged
merged 1 commit into from
May 22, 2019
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
2 changes: 2 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4527,6 +4527,8 @@ namespace ts {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return generateNameForMethodOrAccessor(<MethodDeclaration | AccessorDeclaration>node);
case SyntaxKind.ComputedPropertyName:
return makeTempVariableName(TempFlags.Auto, /*reserveInNestedScopes*/ true);
default:
return makeTempVariableName(TempFlags.Auto);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [instanceMemberWithComputedPropertyName.ts]
// https://github.com/microsoft/TypeScript/issues/30953
const x = 1;
class C {
[x] = true;
constructor() {
const { a, b } = { a: 1, b: 2 };
}
}

//// [instanceMemberWithComputedPropertyName.js]
var _a;
// https://github.com/microsoft/TypeScript/issues/30953
var x = 1;
var C = /** @class */ (function () {
function C() {
this[_a] = true;
var _b = { a: 1, b: 2 }, a = _b.a, b = _b.b;
}
return C;
}());
_a = x;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/instanceMemberWithComputedPropertyName.ts ===
// https://github.com/microsoft/TypeScript/issues/30953
const x = 1;
>x : Symbol(x, Decl(instanceMemberWithComputedPropertyName.ts, 1, 5))

class C {
>C : Symbol(C, Decl(instanceMemberWithComputedPropertyName.ts, 1, 12))

[x] = true;
>[x] : Symbol(C[x], Decl(instanceMemberWithComputedPropertyName.ts, 2, 9))
>x : Symbol(x, Decl(instanceMemberWithComputedPropertyName.ts, 1, 5))

constructor() {
const { a, b } = { a: 1, b: 2 };
>a : Symbol(a, Decl(instanceMemberWithComputedPropertyName.ts, 5, 15))
>b : Symbol(b, Decl(instanceMemberWithComputedPropertyName.ts, 5, 18))
>a : Symbol(a, Decl(instanceMemberWithComputedPropertyName.ts, 5, 26))
>b : Symbol(b, Decl(instanceMemberWithComputedPropertyName.ts, 5, 32))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/instanceMemberWithComputedPropertyName.ts ===
// https://github.com/microsoft/TypeScript/issues/30953
const x = 1;
>x : 1
>1 : 1

class C {
>C : C

[x] = true;
>[x] : boolean
>x : 1
>true : true

constructor() {
const { a, b } = { a: 1, b: 2 };
>a : number
>b : number
>{ a: 1, b: 2 } : { a: number; b: number; }
>a : number
>1 : 1
>b : number
>2 : 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// https://github.com/microsoft/TypeScript/issues/30953
const x = 1;
class C {
[x] = true;
constructor() {
const { a, b } = { a: 1, b: 2 };
}
}