-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from all commits
b31ac2e
57162e9
b5ac1eb
e79253a
521a0a1
36607b9
21250ed
e41f1ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = `); | ||
emitDeclarationName(node); | ||
write(";"); | ||
} | ||
} | ||
else if (node.parent.kind !== SyntaxKind.SourceFile || (modulekind !== ModuleKind.ES6 && !(node.flags & NodeFlags.Default))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
writeLine(); | ||
emitStart(node); | ||
emitModuleMemberName(node); | ||
|
@@ -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) { | ||
|
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 | ||
|
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 | ||
|
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)) | ||
|
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 | ||
|
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 | ||
|
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)) | ||
|
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 {} |
There was a problem hiding this comment.
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"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
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)