Skip to content

Commit 1d5add5

Browse files
authored
Emit computed property temps even w/o init w/useDefineForClassFields (microsoft#34406)
Fixes microsoft#33857
1 parent 241de73 commit 1d5add5

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

src/compiler/transformers/classFields.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ namespace ts {
132132
// Create a temporary variable to store a computed property name (if necessary).
133133
// If it's not inlineable, then we emit an expression after the class which assigns
134134
// the property name to the temporary variable.
135-
const expr = getPropertyNameExpressionIfNeeded(node.name, !!node.initializer);
135+
const expr = getPropertyNameExpressionIfNeeded(node.name, !!node.initializer || !!context.getCompilerOptions().useDefineForClassFields);
136136
if (expr && !isSimpleInlineableExpression(expr)) {
137137
(pendingExpressions || (pendingExpressions = [])).push(expr);
138138
}
@@ -145,7 +145,7 @@ namespace ts {
145145
}
146146

147147
const savedPendingExpressions = pendingExpressions;
148-
pendingExpressions = undefined!;
148+
pendingExpressions = undefined;
149149

150150
const extendsClauseElement = getEffectiveBaseTypeNode(node);
151151
const isDerivedClass = !!(extendsClauseElement && skipOuterExpressions(extendsClauseElement.expression).kind !== SyntaxKind.NullKeyword);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [instanceMemberWithComputedPropertyName2.ts]
2+
// https://github.com/microsoft/TypeScript/issues/33857
3+
"use strict";
4+
const x = 1;
5+
class C {
6+
[x]: string;
7+
}
8+
9+
10+
//// [instanceMemberWithComputedPropertyName2.js]
11+
// https://github.com/microsoft/TypeScript/issues/33857
12+
"use strict";
13+
var _a;
14+
const x = 1;
15+
class C {
16+
constructor() {
17+
Object.defineProperty(this, _a, {
18+
enumerable: true,
19+
configurable: true,
20+
writable: true,
21+
value: void 0
22+
});
23+
}
24+
}
25+
_a = x;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/instanceMemberWithComputedPropertyName2.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/33857
3+
"use strict";
4+
const x = 1;
5+
>x : Symbol(x, Decl(instanceMemberWithComputedPropertyName2.ts, 2, 5))
6+
7+
class C {
8+
>C : Symbol(C, Decl(instanceMemberWithComputedPropertyName2.ts, 2, 12))
9+
10+
[x]: string;
11+
>[x] : Symbol(C[x], Decl(instanceMemberWithComputedPropertyName2.ts, 3, 9))
12+
>x : Symbol(x, Decl(instanceMemberWithComputedPropertyName2.ts, 2, 5))
13+
}
14+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/instanceMemberWithComputedPropertyName2.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/33857
3+
"use strict";
4+
>"use strict" : "use strict"
5+
6+
const x = 1;
7+
>x : 1
8+
>1 : 1
9+
10+
class C {
11+
>C : C
12+
13+
[x]: string;
14+
>[x] : string
15+
>x : 1
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// https://github.com/microsoft/TypeScript/issues/33857
2+
// @useDefineForClassFields: true
3+
// @target: es2015
4+
"use strict";
5+
const x = 1;
6+
class C {
7+
[x]: string;
8+
}

0 commit comments

Comments
 (0)