Support emitting static properties for classes with no name

This commit is contained in:
Andy Hanson 2016-07-27 05:43:00 -07:00
parent bd6d2c0251
commit 4e778c0790
5 changed files with 32 additions and 5 deletions

View File

@ -5526,13 +5526,17 @@ const _super = (function (geti, seti) {
// If the class has static properties, and it's a class expression, then we'll need
// to specialize the emit a bit. for a class expression of the form:
//
// class C { static a = 1; static b = 2; ... }
// (class C { static a = 1; static b = 2; ... })
//
// We'll emit:
//
// let C_1 = class C{};
// C_1.a = 1;
// C_1.b = 2; // so forth and so on
// ((C_1 = class C {
// // Normal class body
// },
// C_1.a = 1,
// C_1.b = 2,
// C_1));
// var C_1;
//
// This keeps the expression as an expression, while ensuring that the static parts
// of it have been initialized by the time it is used.
@ -5541,7 +5545,7 @@ const _super = (function (geti, seti) {
let generatedName: string;
if (isClassExpressionWithStaticProperties) {
generatedName = getGeneratedNameForNode(node.name);
generatedName = node.name ? getGeneratedNameForNode(node.name) : makeUniqueName("classExpression");
const synthesizedNode = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
synthesizedNode.text = generatedName;
recordTempDeclaration(synthesizedNode);

View File

@ -0,0 +1,10 @@
//// [classExpressionWithStaticPropertiesES64.ts]
(class { static x = 0; });
//// [classExpressionWithStaticPropertiesES64.js]
((classExpression_1 = class {
},
classExpression_1.x = 0,
classExpression_1));
var classExpression_1;

View File

@ -0,0 +1,4 @@
=== tests/cases/compiler/classExpressionWithStaticPropertiesES64.ts ===
(class { static x = 0; });
>x : Symbol((Anonymous class).x, Decl(classExpressionWithStaticPropertiesES64.ts, 0, 8))

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/classExpressionWithStaticPropertiesES64.ts ===
(class { static x = 0; });
>(class { static x = 0; }) : typeof (Anonymous class)
>class { static x = 0; } : typeof (Anonymous class)
>x : number
>0 : number

View File

@ -0,0 +1,2 @@
// @target: es6
(class { static x = 0; });