Skip to content

Commit

Permalink
Fix for computed properties in instance initializers (#31517)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed May 22, 2019
1 parent b36c8a0 commit 7611c5b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/compiler/emitter.ts
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
@@ -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;
@@ -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))
}
}
@@ -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
}
}
@@ -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 };
}
}

0 comments on commit 7611c5b

Please sign in to comment.