Skip to content

Commit a0a506b

Browse files
author
Yui T
committed
Emit class declaration with static property assignment
1 parent da12d46 commit a0a506b

7 files changed

+62
-5
lines changed

src/compiler/emitter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4677,10 +4677,17 @@ module ts {
46774677
writeLine();
46784678
emitConstructorOfClass(node, baseTypeNode);
46794679
emitMemberFunctionsAboveES6(node);
4680+
decreaseIndent();
46804681
writeLine();
4682+
emitToken(SyntaxKind.CloseBraceToken, node.members.end);
46814683
scopeEmitEnd();
4682-
decreaseIndent();
4683-
write("}");
4684+
4685+
// Emit static property assignment. Because classDeclaration is lexically evaluated, it is safe to emit static property assignment after classDeclaration
4686+
// From ES6 specification:
4687+
// HasLexicalDeclaration (N) : Determines if the argument identifier has a binding in this environment record that was created using
4688+
// a lexical declaration such as a LexicalDeclaration or a ClassDeclaration.
4689+
writeLine();
4690+
emitMemberAssignments(node, NodeFlags.Static);
46844691
}
46854692

46864693
function emitClassDeclarationBelowES6(node: ClassDeclaration) {

tests/baselines/reference/emitClassDeclarationWithConstructorPropertyAssignmentInES6.js renamed to tests/baselines/reference/emitClassDeclarationWithPropertyAssignmentInES6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//// [emitClassDeclarationWithConstructorPropertyAssignmentInES6.ts]
1+
//// [emitClassDeclarationWithPropertyAssignmentInES6.ts]
22
class C {
33
x: string = "Hello world";
44
}
@@ -24,7 +24,7 @@ class F extends D{
2424
}
2525
}
2626

27-
//// [emitClassDeclarationWithConstructorPropertyAssignmentInES6.js]
27+
//// [emitClassDeclarationWithPropertyAssignmentInES6.js]
2828
class C {
2929
constructor() {
3030
this.x = "Hello world";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
=== tests/cases/conformance/es6/classDeclaration/emitClassDeclarationWithConstructorPropertyAssignmentInES6.ts ===
1+
=== tests/cases/conformance/es6/classDeclaration/emitClassDeclarationWithPropertyAssignmentInES6.ts ===
22
class C {
33
>C : C
44

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [emitClassDeclarationWithStaticPropertyAssignmentInES6.ts]
2+
class C {
3+
static z: string = "Foo";
4+
}
5+
6+
class D {
7+
x = 20000;
8+
static b = true;
9+
}
10+
11+
12+
//// [emitClassDeclarationWithStaticPropertyAssignmentInES6.js]
13+
class C {
14+
constructor() {
15+
}
16+
}
17+
C.z = "Foo";
18+
class D {
19+
constructor() {
20+
this.x = 20000;
21+
}
22+
}
23+
D.b = true;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/conformance/es6/classDeclaration/emitClassDeclarationWithStaticPropertyAssignmentInES6.ts ===
2+
class C {
3+
>C : C
4+
5+
static z: string = "Foo";
6+
>z : string
7+
}
8+
9+
class D {
10+
>D : D
11+
12+
x = 20000;
13+
>x : number
14+
15+
static b = true;
16+
>b : boolean
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @target:es6
2+
class C {
3+
static z: string = "Foo";
4+
}
5+
6+
class D {
7+
x = 20000;
8+
static b = true;
9+
}

0 commit comments

Comments
 (0)