Skip to content

Commit c578367

Browse files
author
Andy
authored
Merge pull request #9969 from Microsoft/class_expression_static_property
Support emitting static properties for classes with no name
2 parents ac96a86 + 0e0220d commit c578367

5 files changed

+32
-5
lines changed

src/compiler/emitter.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -5526,13 +5526,17 @@ const _super = (function (geti, seti) {
55265526
// If the class has static properties, and it's a class expression, then we'll need
55275527
// to specialize the emit a bit. for a class expression of the form:
55285528
//
5529-
// class C { static a = 1; static b = 2; ... }
5529+
// (class C { static a = 1; static b = 2; ... })
55305530
//
55315531
// We'll emit:
55325532
//
5533-
// let C_1 = class C{};
5534-
// C_1.a = 1;
5535-
// C_1.b = 2; // so forth and so on
5533+
// ((C_1 = class C {
5534+
// // Normal class body
5535+
// },
5536+
// C_1.a = 1,
5537+
// C_1.b = 2,
5538+
// C_1));
5539+
// var C_1;
55365540
//
55375541
// This keeps the expression as an expression, while ensuring that the static parts
55385542
// of it have been initialized by the time it is used.
@@ -5541,7 +5545,7 @@ const _super = (function (geti, seti) {
55415545
let generatedName: string;
55425546

55435547
if (isClassExpressionWithStaticProperties) {
5544-
generatedName = getGeneratedNameForNode(node.name);
5548+
generatedName = node.name ? getGeneratedNameForNode(node.name) : makeUniqueName("classExpression");
55455549
const synthesizedNode = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
55465550
synthesizedNode.text = generatedName;
55475551
recordTempDeclaration(synthesizedNode);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [classExpressionWithStaticPropertiesES64.ts]
2+
(class { static x = 0; });
3+
4+
5+
//// [classExpressionWithStaticPropertiesES64.js]
6+
((classExpression_1 = class {
7+
},
8+
classExpression_1.x = 0,
9+
classExpression_1));
10+
var classExpression_1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/classExpressionWithStaticPropertiesES64.ts ===
2+
(class { static x = 0; });
3+
>x : Symbol((Anonymous class).x, Decl(classExpressionWithStaticPropertiesES64.ts, 0, 8))
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/classExpressionWithStaticPropertiesES64.ts ===
2+
(class { static x = 0; });
3+
>(class { static x = 0; }) : typeof (Anonymous class)
4+
>class { static x = 0; } : typeof (Anonymous class)
5+
>x : number
6+
>0 : number
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @target: es6
2+
(class { static x = 0; });

0 commit comments

Comments
 (0)