From 8360bc7961a3e3a5052e7d608c2a1cbfddac226c Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 8 Jun 2016 16:43:56 -0700 Subject: [PATCH] Add handleing for classes --- src/compiler/emitter.ts | 12 ++++- .../reference/es6modulekindWithES5Target.js | 51 +++++++++++++++++++ .../es6modulekindWithES5Target.symbols | 41 +++++++++++++++ .../es6modulekindWithES5Target.types | 45 ++++++++++++++++ .../reference/es6modulekindWithES5Target2.js | 19 +++++++ .../es6modulekindWithES5Target2.symbols | 15 ++++++ .../es6modulekindWithES5Target2.types | 17 +++++++ .../reference/es6modulekindWithES5Target3.js | 30 +++++++++++ .../es6modulekindWithES5Target3.symbols | 22 ++++++++ .../es6modulekindWithES5Target3.types | 24 +++++++++ .../reference/es6modulekindWithES5Target4.js | 12 +++++ .../es6modulekindWithES5Target4.symbols | 8 +++ .../es6modulekindWithES5Target4.types | 8 +++ .../compiler/es6modulekindWithES5Target.ts | 20 ++++++++ .../compiler/es6modulekindWithES5Target2.ts | 8 +++ .../compiler/es6modulekindWithES5Target3.ts | 12 +++++ .../compiler/es6modulekindWithES5Target4.ts | 5 ++ 17 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/es6modulekindWithES5Target.js create mode 100644 tests/baselines/reference/es6modulekindWithES5Target.symbols create mode 100644 tests/baselines/reference/es6modulekindWithES5Target.types create mode 100644 tests/baselines/reference/es6modulekindWithES5Target2.js create mode 100644 tests/baselines/reference/es6modulekindWithES5Target2.symbols create mode 100644 tests/baselines/reference/es6modulekindWithES5Target2.types create mode 100644 tests/baselines/reference/es6modulekindWithES5Target3.js create mode 100644 tests/baselines/reference/es6modulekindWithES5Target3.symbols create mode 100644 tests/baselines/reference/es6modulekindWithES5Target3.types create mode 100644 tests/baselines/reference/es6modulekindWithES5Target4.js create mode 100644 tests/baselines/reference/es6modulekindWithES5Target4.symbols create mode 100644 tests/baselines/reference/es6modulekindWithES5Target4.types create mode 100644 tests/cases/compiler/es6modulekindWithES5Target.ts create mode 100644 tests/cases/compiler/es6modulekindWithES5Target2.ts create mode 100644 tests/cases/compiler/es6modulekindWithES5Target3.ts create mode 100644 tests/cases/compiler/es6modulekindWithES5Target4.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f225b4cacf8..55b8656f5e8 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -5559,7 +5559,11 @@ const _super = (function (geti, seti) { } function emitClassLikeDeclarationBelowES6(node: ClassLikeDeclaration) { + const isES6ExportedClass = isES6ExportedDeclaration(node); if (node.kind === SyntaxKind.ClassDeclaration) { + if (isES6ExportedClass && !(node.flags & NodeFlags.Default)) { + write("export "); + } // source file level classes in system modules are hoisted so 'var's for them are already defined if (!shouldHoistDeclarationInSystemJsModule(node)) { write("var "); @@ -5629,9 +5633,15 @@ const _super = (function (geti, seti) { } emitEnd(node); - if (node.kind === SyntaxKind.ClassDeclaration) { + if (node.kind === SyntaxKind.ClassDeclaration && !isES6ExportedClass) { emitExportMemberAssignment(node); } + else if (isES6ExportedClass && (node.flags & NodeFlags.Default)) { + writeLine(); + write("export default "); + emitDeclarationName(node); + write(";"); + } } function emitClassMemberPrefix(node: ClassLikeDeclaration, member: Node) { diff --git a/tests/baselines/reference/es6modulekindWithES5Target.js b/tests/baselines/reference/es6modulekindWithES5Target.js new file mode 100644 index 00000000000..bab55ec7a2c --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target.js @@ -0,0 +1,51 @@ +//// [es6modulekindWithES5Target.ts] + +export class C { + static s = 0; + p = 1; + method() { } +} + +declare function foo(...args: any[]): any; +@foo +export class D { + static s = 0; + p = 1; + method() { } +} + +class E { } +export {E}; + +//// [es6modulekindWithES5Target.js] +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +export var C = (function () { + function C() { + this.p = 1; + } + C.prototype.method = function () { }; + C.s = 0; + return C; +}()); +export var D = (function () { + function D() { + this.p = 1; + } + D.prototype.method = function () { }; + D.s = 0; + D = __decorate([ + foo + ], D); + return D; +}()); +var E = (function () { + function E() { + } + return E; +}()); +export { E }; diff --git a/tests/baselines/reference/es6modulekindWithES5Target.symbols b/tests/baselines/reference/es6modulekindWithES5Target.symbols new file mode 100644 index 00000000000..4fe53ced68d --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target.symbols @@ -0,0 +1,41 @@ +=== tests/cases/compiler/es6modulekindWithES5Target.ts === + +export class C { +>C : Symbol(C, Decl(es6modulekindWithES5Target.ts, 0, 0)) + + static s = 0; +>s : Symbol(C.s, Decl(es6modulekindWithES5Target.ts, 1, 16)) + + p = 1; +>p : Symbol(C.p, Decl(es6modulekindWithES5Target.ts, 2, 17)) + + method() { } +>method : Symbol(C.method, Decl(es6modulekindWithES5Target.ts, 3, 10)) +} + +declare function foo(...args: any[]): any; +>foo : Symbol(foo, Decl(es6modulekindWithES5Target.ts, 5, 1)) +>args : Symbol(args, Decl(es6modulekindWithES5Target.ts, 7, 21)) + +@foo +>foo : Symbol(foo, Decl(es6modulekindWithES5Target.ts, 5, 1)) + +export class D { +>D : Symbol(D, Decl(es6modulekindWithES5Target.ts, 7, 42)) + + static s = 0; +>s : Symbol(D.s, Decl(es6modulekindWithES5Target.ts, 9, 16)) + + p = 1; +>p : Symbol(D.p, Decl(es6modulekindWithES5Target.ts, 10, 17)) + + method() { } +>method : Symbol(D.method, Decl(es6modulekindWithES5Target.ts, 11, 10)) +} + +class E { } +>E : Symbol(E, Decl(es6modulekindWithES5Target.ts, 13, 1)) + +export {E}; +>E : Symbol(E, Decl(es6modulekindWithES5Target.ts, 16, 8)) + diff --git a/tests/baselines/reference/es6modulekindWithES5Target.types b/tests/baselines/reference/es6modulekindWithES5Target.types new file mode 100644 index 00000000000..14e0533faaa --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/es6modulekindWithES5Target.ts === + +export class C { +>C : C + + static s = 0; +>s : number +>0 : number + + p = 1; +>p : number +>1 : number + + method() { } +>method : () => void +} + +declare function foo(...args: any[]): any; +>foo : (...args: any[]) => any +>args : any[] + +@foo +>foo : (...args: any[]) => any + +export class D { +>D : D + + static s = 0; +>s : number +>0 : number + + p = 1; +>p : number +>1 : number + + method() { } +>method : () => void +} + +class E { } +>E : E + +export {E}; +>E : typeof E + diff --git a/tests/baselines/reference/es6modulekindWithES5Target2.js b/tests/baselines/reference/es6modulekindWithES5Target2.js new file mode 100644 index 00000000000..4cfe8bc6d4b --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target2.js @@ -0,0 +1,19 @@ +//// [es6modulekindWithES5Target2.ts] + +export default class C { + static s = 0; + p = 1; + method() { } +} + + +//// [es6modulekindWithES5Target2.js] +var C = (function () { + function C() { + this.p = 1; + } + C.prototype.method = function () { }; + C.s = 0; + return C; +}()); +export default C; diff --git a/tests/baselines/reference/es6modulekindWithES5Target2.symbols b/tests/baselines/reference/es6modulekindWithES5Target2.symbols new file mode 100644 index 00000000000..762121612eb --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target2.symbols @@ -0,0 +1,15 @@ +=== tests/cases/compiler/es6modulekindWithES5Target2.ts === + +export default class C { +>C : Symbol(C, Decl(es6modulekindWithES5Target2.ts, 0, 0)) + + static s = 0; +>s : Symbol(C.s, Decl(es6modulekindWithES5Target2.ts, 1, 24)) + + p = 1; +>p : Symbol(C.p, Decl(es6modulekindWithES5Target2.ts, 2, 17)) + + method() { } +>method : Symbol(C.method, Decl(es6modulekindWithES5Target2.ts, 3, 10)) +} + diff --git a/tests/baselines/reference/es6modulekindWithES5Target2.types b/tests/baselines/reference/es6modulekindWithES5Target2.types new file mode 100644 index 00000000000..04b5e5b9905 --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target2.types @@ -0,0 +1,17 @@ +=== tests/cases/compiler/es6modulekindWithES5Target2.ts === + +export default class C { +>C : C + + static s = 0; +>s : number +>0 : number + + p = 1; +>p : number +>1 : number + + method() { } +>method : () => void +} + diff --git a/tests/baselines/reference/es6modulekindWithES5Target3.js b/tests/baselines/reference/es6modulekindWithES5Target3.js new file mode 100644 index 00000000000..bfe55d2a573 --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target3.js @@ -0,0 +1,30 @@ +//// [es6modulekindWithES5Target3.ts] + + +declare function foo(...args: any[]): any; +@foo +export default class D { + static s = 0; + p = 1; + method() { } +} + +//// [es6modulekindWithES5Target3.js] +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var D = (function () { + function D() { + this.p = 1; + } + D.prototype.method = function () { }; + D.s = 0; + D = __decorate([ + foo + ], D); + return D; +}()); +export default D; diff --git a/tests/baselines/reference/es6modulekindWithES5Target3.symbols b/tests/baselines/reference/es6modulekindWithES5Target3.symbols new file mode 100644 index 00000000000..3fd26b9033f --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target3.symbols @@ -0,0 +1,22 @@ +=== tests/cases/compiler/es6modulekindWithES5Target3.ts === + + +declare function foo(...args: any[]): any; +>foo : Symbol(foo, Decl(es6modulekindWithES5Target3.ts, 0, 0)) +>args : Symbol(args, Decl(es6modulekindWithES5Target3.ts, 2, 21)) + +@foo +>foo : Symbol(foo, Decl(es6modulekindWithES5Target3.ts, 0, 0)) + +export default class D { +>D : Symbol(D, Decl(es6modulekindWithES5Target3.ts, 2, 42)) + + static s = 0; +>s : Symbol(D.s, Decl(es6modulekindWithES5Target3.ts, 4, 24)) + + p = 1; +>p : Symbol(D.p, Decl(es6modulekindWithES5Target3.ts, 5, 17)) + + method() { } +>method : Symbol(D.method, Decl(es6modulekindWithES5Target3.ts, 6, 10)) +} diff --git a/tests/baselines/reference/es6modulekindWithES5Target3.types b/tests/baselines/reference/es6modulekindWithES5Target3.types new file mode 100644 index 00000000000..5bc8c69c01a --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target3.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/es6modulekindWithES5Target3.ts === + + +declare function foo(...args: any[]): any; +>foo : (...args: any[]) => any +>args : any[] + +@foo +>foo : (...args: any[]) => any + +export default class D { +>D : D + + static s = 0; +>s : number +>0 : number + + p = 1; +>p : number +>1 : number + + method() { } +>method : () => void +} diff --git a/tests/baselines/reference/es6modulekindWithES5Target4.js b/tests/baselines/reference/es6modulekindWithES5Target4.js new file mode 100644 index 00000000000..b34e68b8111 --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target4.js @@ -0,0 +1,12 @@ +//// [es6modulekindWithES5Target4.ts] + +class E { } +export default E; + +//// [es6modulekindWithES5Target4.js] +var E = (function () { + function E() { + } + return E; +}()); +export default E; diff --git a/tests/baselines/reference/es6modulekindWithES5Target4.symbols b/tests/baselines/reference/es6modulekindWithES5Target4.symbols new file mode 100644 index 00000000000..fdf5bdc6197 --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target4.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6modulekindWithES5Target4.ts === + +class E { } +>E : Symbol(E, Decl(es6modulekindWithES5Target4.ts, 0, 0)) + +export default E; +>E : Symbol(E, Decl(es6modulekindWithES5Target4.ts, 0, 0)) + diff --git a/tests/baselines/reference/es6modulekindWithES5Target4.types b/tests/baselines/reference/es6modulekindWithES5Target4.types new file mode 100644 index 00000000000..cd0852cab1a --- /dev/null +++ b/tests/baselines/reference/es6modulekindWithES5Target4.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6modulekindWithES5Target4.ts === + +class E { } +>E : E + +export default E; +>E : E + diff --git a/tests/cases/compiler/es6modulekindWithES5Target.ts b/tests/cases/compiler/es6modulekindWithES5Target.ts new file mode 100644 index 00000000000..72117932229 --- /dev/null +++ b/tests/cases/compiler/es6modulekindWithES5Target.ts @@ -0,0 +1,20 @@ +// @target: es5 +// @module: es2015 +// @experimentalDecorators: true + +export class C { + static s = 0; + p = 1; + method() { } +} + +declare function foo(...args: any[]): any; +@foo +export class D { + static s = 0; + p = 1; + method() { } +} + +class E { } +export {E}; \ No newline at end of file diff --git a/tests/cases/compiler/es6modulekindWithES5Target2.ts b/tests/cases/compiler/es6modulekindWithES5Target2.ts new file mode 100644 index 00000000000..01f32fd0da0 --- /dev/null +++ b/tests/cases/compiler/es6modulekindWithES5Target2.ts @@ -0,0 +1,8 @@ +// @target: es5 +// @module: es2015 + +export default class C { + static s = 0; + p = 1; + method() { } +} diff --git a/tests/cases/compiler/es6modulekindWithES5Target3.ts b/tests/cases/compiler/es6modulekindWithES5Target3.ts new file mode 100644 index 00000000000..af407271685 --- /dev/null +++ b/tests/cases/compiler/es6modulekindWithES5Target3.ts @@ -0,0 +1,12 @@ +// @target: es5 +// @module: es2015 +// @experimentalDecorators: true + + +declare function foo(...args: any[]): any; +@foo +export default class D { + static s = 0; + p = 1; + method() { } +} \ No newline at end of file diff --git a/tests/cases/compiler/es6modulekindWithES5Target4.ts b/tests/cases/compiler/es6modulekindWithES5Target4.ts new file mode 100644 index 00000000000..709dd791fb4 --- /dev/null +++ b/tests/cases/compiler/es6modulekindWithES5Target4.ts @@ -0,0 +1,5 @@ +// @target: es5 +// @module: es2015 + +class E { } +export default E; \ No newline at end of file