Skip to content

Handle default class exports when targeting es6 with non-es6 module kinds #5648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5578,9 +5578,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 = `);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in ES3 we should emit this as exports["defualt"]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never mind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be regular quotation marks? (no embedded variables)

emitDeclarationName(node);
write(";");
}
}
else if (node.parent.kind !== SyntaxKind.SourceFile || (modulekind !== ModuleKind.ES6 && !(node.flags & NodeFlags.Default))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just modulekind !== ModuleKind.ES6 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(modulekind !== ModuleKind.ES6 && !(node.flags & NodeFlags.Default)) || (node.parent.kind !== SyntaxKind.SourceFile) is needed, since this handles non-es6 non-default exports and namespace exports. ES6 nondefault exports get handled by the export modifier far above.

writeLine();
emitStart(node);
emitModuleMemberName(node);
Expand All @@ -5589,13 +5611,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) {
Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/decoratedDefaultExportsGetExportedAmd.js
Original file line number Diff line number Diff line change
@@ -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;
});
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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);
}
}
});
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

31 changes: 31 additions & 0 deletions tests/baselines/reference/decoratedDefaultExportsGetExportedUmd.js
Original file line number Diff line number Diff line change
@@ -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;
});
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

10 changes: 10 additions & 0 deletions tests/baselines/reference/defaultExportsGetExportedAmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [defaultExportsGetExportedAmd.ts]
export default class Foo {}


//// [defaultExportsGetExportedAmd.js]
define(["require", "exports"], function (require, exports) {
class Foo {
}
exports.default = Foo;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsAmd/defaultExportsGetExportedAmd.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedAmd.ts, 0, 0))

4 changes: 4 additions & 0 deletions tests/baselines/reference/defaultExportsGetExportedAmd.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsAmd/defaultExportsGetExportedAmd.ts ===
export default class Foo {}
>Foo : Foo

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [defaultExportsGetExportedCommonjs.ts]
export default class Foo {}


//// [defaultExportsGetExportedCommonjs.js]
class Foo {
}
exports.default = Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsCommonjs/defaultExportsGetExportedCommonjs.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedCommonjs.ts, 0, 0))

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

16 changes: 16 additions & 0 deletions tests/baselines/reference/defaultExportsGetExportedSystem.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsSystem/defaultExportsGetExportedSystem.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedSystem.ts, 0, 0))

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

17 changes: 17 additions & 0 deletions tests/baselines/reference/defaultExportsGetExportedUmd.js
Original file line number Diff line number Diff line change
@@ -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;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsUmd/defaultExportsGetExportedUmd.ts ===
export default class Foo {}
>Foo : Symbol(Foo, Decl(defaultExportsGetExportedUmd.ts, 0, 0))

4 changes: 4 additions & 0 deletions tests/baselines/reference/defaultExportsGetExportedUmd.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/moduleExportsUmd/defaultExportsGetExportedUmd.ts ===
export default class Foo {}
>Foo : Foo

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @target: ES6
// @experimentalDecorators: true
// @module: amd

var decorator: ClassDecorator;

@decorator
export default class Foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @target: ES6
// @module: amd
export default class Foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @target: ES6
// @experimentalDecorators: true
// @module: commonjs

var decorator: ClassDecorator;

@decorator
export default class Foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @target: ES6
// @module: commonjs
export default class Foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @target: ES6
// @experimentalDecorators: true
// @module: system

var decorator: ClassDecorator;

@decorator
export default class Foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @target: ES6
// @module: system
export default class Foo {}
Loading