Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c578367

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

5 files changed

+32-5Lines changed: 32 additions & 5 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/emitter.ts‎

Copy file name to clipboardExpand all lines: src/compiler/emitter.ts
+9-5Lines changed: 9 additions & 5 deletions
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);
Collapse file
+10Lines changed: 10 additions & 0 deletions
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;
Collapse file
+4Lines changed: 4 additions & 0 deletions
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+
Collapse file
+7Lines changed: 7 additions & 0 deletions
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+
Collapse file
+2Lines changed: 2 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.