Merge pull request #5648 from weswigham/default-commonjs-es6

Handle default class exports when targeting es6 with non-es6 module kinds
This commit is contained in:
Wesley Wigham 2015-11-25 17:15:06 -08:00
commit ec775ba4aa
33 changed files with 352 additions and 8 deletions

View file

@ -5571,9 +5571,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitDecoratorsOfClass(node);
}
if (!(node.flags & NodeFlags.Export)) {
return;
}
// If this is an exported class, but not on the top level (i.e. on an internal
// module), export it
if (!isES6ExportedDeclaration(node) && (node.flags & NodeFlags.Export)) {
if (node.flags & NodeFlags.Default) {
// if this is a top level default export of decorated class, write the export after the declaration.
writeLine();
if (thisNodeIsDecorated && modulekind === ModuleKind.ES6) {
write("export default ");
emitDeclarationName(node);
write(";");
}
else if (modulekind === ModuleKind.System) {
write(`${exportFunctionForFile}("default", `);
emitDeclarationName(node);
write(");");
}
else if (modulekind !== ModuleKind.ES6) {
write(`exports.default = `);
emitDeclarationName(node);
write(";");
}
}
else if (node.parent.kind !== SyntaxKind.SourceFile || (modulekind !== ModuleKind.ES6 && !(node.flags & NodeFlags.Default))) {
writeLine();
emitStart(node);
emitModuleMemberName(node);
@ -5582,13 +5604,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitEnd(node);
write(";");
}
else if (isES6ExportedDeclaration(node) && (node.flags & NodeFlags.Default) && thisNodeIsDecorated) {
// if this is a top level default export of decorated class, write the export after the declaration.
writeLine();
write("export default ");
emitDeclarationName(node);
write(";");
}
}
function emitClassLikeDeclarationBelowES6(node: ClassLikeDeclaration) {

View file

@ -0,0 +1,24 @@
//// [decoratedDefaultExportsGetExportedAmd.ts]
var decorator: ClassDecorator;
@decorator
export default class Foo {}
//// [decoratedDefaultExportsGetExportedAmd.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;
};
define(["require", "exports"], function (require, exports) {
var decorator;
let Foo = class {
};
Foo = __decorate([
decorator
], Foo);
exports.default = Foo;
});

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsAmd/decoratedDefaultExportsGetExportedAmd.ts ===
var decorator: ClassDecorator;
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedAmd.ts, 1, 3))
>ClassDecorator : Symbol(ClassDecorator, Decl(lib.d.ts, --, --))
@decorator
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedAmd.ts, 1, 3))
export default class Foo {}
>Foo : Symbol(Foo, Decl(decoratedDefaultExportsGetExportedAmd.ts, 1, 30))

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsAmd/decoratedDefaultExportsGetExportedAmd.ts ===
var decorator: ClassDecorator;
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
>ClassDecorator : <TFunction extends Function>(target: TFunction) => TFunction | void
@decorator
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,22 @@
//// [decoratedDefaultExportsGetExportedCommonjs.ts]
var decorator: ClassDecorator;
@decorator
export default class Foo {}
//// [decoratedDefaultExportsGetExportedCommonjs.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 decorator;
let Foo = class {
};
Foo = __decorate([
decorator
], Foo);
exports.default = Foo;

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsCommonjs/decoratedDefaultExportsGetExportedCommonjs.ts ===
var decorator: ClassDecorator;
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedCommonjs.ts, 1, 3))
>ClassDecorator : Symbol(ClassDecorator, Decl(lib.d.ts, --, --))
@decorator
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedCommonjs.ts, 1, 3))
export default class Foo {}
>Foo : Symbol(Foo, Decl(decoratedDefaultExportsGetExportedCommonjs.ts, 1, 30))

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsCommonjs/decoratedDefaultExportsGetExportedCommonjs.ts ===
var decorator: ClassDecorator;
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
>ClassDecorator : <TFunction extends Function>(target: TFunction) => TFunction | void
@decorator
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,29 @@
//// [decoratedDefaultExportsGetExportedSystem.ts]
var decorator: ClassDecorator;
@decorator
export default class Foo {}
//// [decoratedDefaultExportsGetExportedSystem.js]
System.register([], function(exports_1) {
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 decorator, Foo;
return {
setters:[],
execute: function() {
let Foo = class {
};
Foo = __decorate([
decorator
], Foo);
exports_1("default", Foo);
}
}
});

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsSystem/decoratedDefaultExportsGetExportedSystem.ts ===
var decorator: ClassDecorator;
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedSystem.ts, 1, 3))
>ClassDecorator : Symbol(ClassDecorator, Decl(lib.d.ts, --, --))
@decorator
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedSystem.ts, 1, 3))
export default class Foo {}
>Foo : Symbol(Foo, Decl(decoratedDefaultExportsGetExportedSystem.ts, 1, 30))

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsSystem/decoratedDefaultExportsGetExportedSystem.ts ===
var decorator: ClassDecorator;
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
>ClassDecorator : <TFunction extends Function>(target: TFunction) => TFunction | void
@decorator
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,31 @@
//// [decoratedDefaultExportsGetExportedUmd.ts]
var decorator: ClassDecorator;
@decorator
export default class Foo {}
//// [decoratedDefaultExportsGetExportedUmd.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;
};
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
var decorator;
let Foo = class {
};
Foo = __decorate([
decorator
], Foo);
exports.default = Foo;
});

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsUmd/decoratedDefaultExportsGetExportedUmd.ts ===
var decorator: ClassDecorator;
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedUmd.ts, 1, 3))
>ClassDecorator : Symbol(ClassDecorator, Decl(lib.d.ts, --, --))
@decorator
>decorator : Symbol(decorator, Decl(decoratedDefaultExportsGetExportedUmd.ts, 1, 3))
export default class Foo {}
>Foo : Symbol(Foo, Decl(decoratedDefaultExportsGetExportedUmd.ts, 1, 30))

View file

@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/moduleExportsUmd/decoratedDefaultExportsGetExportedUmd.ts ===
var decorator: ClassDecorator;
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
>ClassDecorator : <TFunction extends Function>(target: TFunction) => TFunction | void
@decorator
>decorator : <TFunction extends Function>(target: TFunction) => TFunction | void
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,10 @@
//// [defaultExportsGetExportedAmd.ts]
export default class Foo {}
//// [defaultExportsGetExportedAmd.js]
define(["require", "exports"], function (require, exports) {
class Foo {
}
exports.default = Foo;
});

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsAmd/defaultExportsGetExportedAmd.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedAmd.ts, 0, 0))

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsAmd/defaultExportsGetExportedAmd.ts ===
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,8 @@
//// [defaultExportsGetExportedCommonjs.ts]
export default class Foo {}
//// [defaultExportsGetExportedCommonjs.js]
class Foo {
}
exports.default = Foo;

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsCommonjs/defaultExportsGetExportedCommonjs.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedCommonjs.ts, 0, 0))

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsCommonjs/defaultExportsGetExportedCommonjs.ts ===
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,16 @@
//// [defaultExportsGetExportedSystem.ts]
export default class Foo {}
//// [defaultExportsGetExportedSystem.js]
System.register([], function(exports_1) {
var Foo;
return {
setters:[],
execute: function() {
class Foo {
}
exports_1("default", Foo);
}
}
});

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsSystem/defaultExportsGetExportedSystem.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedSystem.ts, 0, 0))

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsSystem/defaultExportsGetExportedSystem.ts ===
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,17 @@
//// [defaultExportsGetExportedUmd.ts]
export default class Foo {}
//// [defaultExportsGetExportedUmd.js]
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
class Foo {
}
exports.default = Foo;
});

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsUmd/defaultExportsGetExportedUmd.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedUmd.ts, 0, 0))

View file

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsUmd/defaultExportsGetExportedUmd.ts ===
export default class Foo {}
>Foo : Foo

View file

@ -0,0 +1,8 @@
// @target: ES6
// @experimentalDecorators: true
// @module: amd
var decorator: ClassDecorator;
@decorator
export default class Foo {}

View file

@ -0,0 +1,3 @@
// @target: ES6
// @module: amd
export default class Foo {}

View file

@ -0,0 +1,8 @@
// @target: ES6
// @experimentalDecorators: true
// @module: commonjs
var decorator: ClassDecorator;
@decorator
export default class Foo {}

View file

@ -0,0 +1,3 @@
// @target: ES6
// @module: commonjs
export default class Foo {}

View file

@ -0,0 +1,8 @@
// @target: ES6
// @experimentalDecorators: true
// @module: system
var decorator: ClassDecorator;
@decorator
export default class Foo {}

View file

@ -0,0 +1,3 @@
// @target: ES6
// @module: system
export default class Foo {}

View file

@ -0,0 +1,8 @@
// @target: ES6
// @experimentalDecorators: true
// @module: umd
var decorator: ClassDecorator;
@decorator
export default class Foo {}

View file

@ -0,0 +1,3 @@
// @target: ES6
// @module: umd
export default class Foo {}