Skip to content
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

improve module loading interoperability for babel #3586

Merged
merged 6 commits into from
Jul 21, 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
25 changes: 24 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3012,6 +3012,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
return result;
}

function emitEs6ExportDefaultCompat(node: Node) {
if (node.parent.kind === SyntaxKind.SourceFile) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be an assert too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JsonFreeman I think this line is required. if this line changes into Debug.assert, we got a assertion error in wrong context.

  3 failing

  1) compiler tests for tests/cases/compiler/moduleElementsInWrongContext.ts "before all" hook:
     Error: Debug Failure. False expression:
      at Object.assert (eval at <anonymous> (built/local/run.js:52497:13), <anonymous>:1533:23)

  2) compiler tests for tests/cases/compiler/moduleElementsInWrongContext2.ts "before all" hook:
     Error: Debug Failure. False expression:
      at Object.assert (eval at <anonymous> (built/local/run.js:52497:13), <anonymous>:1533:23)

  3) compiler tests for tests/cases/compiler/moduleElementsInWrongContext3.ts "before all" hook:
     Error: Debug Failure. False expression:
      at Object.assert (eval at <anonymous> (built/local/run.js:52497:13), <anonymous>:1533:23)

Debug.assert(!!(node.flags & NodeFlags.Default) || node.kind === SyntaxKind.ExportAssignment);
// only allow export default at a source file level
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
if (!currentSourceFile.symbol.exports["___esModule"]) {
if (languageVersion === ScriptTarget.ES5) {
// default value of configurable, enumerable, writable are `false`.
write("Object.defineProperty(exports, \"__esModule\", { value: true });");
writeLine();
}
else if (languageVersion === ScriptTarget.ES3) {
write("exports.__esModule = true;");
writeLine();
}
}
}
}
}

function emitExportMemberAssignment(node: FunctionLikeDeclaration | ClassDeclaration) {
if (node.flags & NodeFlags.Export) {
writeLine();
Expand All @@ -3034,9 +3054,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}
else {
if (node.flags & NodeFlags.Default) {
emitEs6ExportDefaultCompat(node);
if (languageVersion === ScriptTarget.ES3) {
write("exports[\"default\"]");
} else {
}
else {
write("exports.default");
}
}
Expand Down Expand Up @@ -5529,6 +5551,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(")");
}
else {
emitEs6ExportDefaultCompat(node);
emitContainingModuleName(node);
if (languageVersion === ScriptTarget.ES3) {
write("[\"default\"] = ");
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/reference/es5-commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [es5-commonjs.ts]

export default class A
{
constructor ()
{

}

public B()
{
return 42;
}
}


//// [es5-commonjs.js]
var A = (function () {
function A() {
}
A.prototype.B = function () {
return 42;
};
return A;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = A;
17 changes: 17 additions & 0 deletions tests/baselines/reference/es5-commonjs.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/es5-commonjs.ts ===

export default class A
>A : Symbol(A, Decl(es5-commonjs.ts, 0, 0))
{
constructor ()
{

}

public B()
>B : Symbol(B, Decl(es5-commonjs.ts, 6, 5))
{
return 42;
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/es5-commonjs.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/es5-commonjs.ts ===

export default class A
>A : A
{
constructor ()
{

}

public B()
>B : () => number
{
return 42;
>42 : number
}
}

8 changes: 8 additions & 0 deletions tests/baselines/reference/es5-commonjs2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [es5-commonjs2.ts]

export default 1;


//// [es5-commonjs2.js]
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = 1;
5 changes: 5 additions & 0 deletions tests/baselines/reference/es5-commonjs2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=== tests/cases/compiler/es5-commonjs2.ts ===

No type information for this code.export default 1;
No type information for this code.
No type information for this code.
5 changes: 5 additions & 0 deletions tests/baselines/reference/es5-commonjs2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=== tests/cases/compiler/es5-commonjs2.ts ===

No type information for this code.export default 1;
No type information for this code.
No type information for this code.
9 changes: 9 additions & 0 deletions tests/baselines/reference/es5-commonjs3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [es5-commonjs3.ts]

export default "test";
export var __esModule = 1;


//// [es5-commonjs3.js]
exports.default = "test";
exports.__esModule = 1;
6 changes: 6 additions & 0 deletions tests/baselines/reference/es5-commonjs3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/es5-commonjs3.ts ===

export default "test";
export var __esModule = 1;
>__esModule : Symbol(__esModule, Decl(es5-commonjs3.ts, 2, 10))

7 changes: 7 additions & 0 deletions tests/baselines/reference/es5-commonjs3.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/es5-commonjs3.ts ===

export default "test";
export var __esModule = 1;
>__esModule : number
>1 : number

28 changes: 28 additions & 0 deletions tests/baselines/reference/es5-commonjs4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [es5-commonjs4.ts]

export default class A
{
constructor ()
{

}

public B()
{
return 42;
}
}
export var __esModule = 1;


//// [es5-commonjs4.js]
var A = (function () {
function A() {
}
A.prototype.B = function () {
return 42;
};
return A;
})();
exports.default = A;
exports.__esModule = 1;
19 changes: 19 additions & 0 deletions tests/baselines/reference/es5-commonjs4.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/es5-commonjs4.ts ===

export default class A
>A : Symbol(A, Decl(es5-commonjs4.ts, 0, 0))
{
constructor ()
{

}

public B()
>B : Symbol(B, Decl(es5-commonjs4.ts, 6, 5))
{
return 42;
}
}
export var __esModule = 1;
>__esModule : Symbol(__esModule, Decl(es5-commonjs4.ts, 13, 10))

21 changes: 21 additions & 0 deletions tests/baselines/reference/es5-commonjs4.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/es5-commonjs4.ts ===

export default class A
>A : A
{
constructor ()
{

}

public B()
>B : () => number
{
return 42;
>42 : number
}
}
export var __esModule = 1;
>__esModule : number
>1 : number

13 changes: 13 additions & 0 deletions tests/baselines/reference/es5-commonjs5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [es5-commonjs5.ts]

export default function () {
return "test";
}


//// [es5-commonjs5.js]
function default_1() {
return "test";
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
7 changes: 7 additions & 0 deletions tests/baselines/reference/es5-commonjs5.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/es5-commonjs5.ts ===

No type information for this code.export default function () {
No type information for this code. return "test";
No type information for this code.}
No type information for this code.
No type information for this code.
7 changes: 7 additions & 0 deletions tests/baselines/reference/es5-commonjs5.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/es5-commonjs5.ts ===

export default function () {
return "test";
>"test" : string
}

10 changes: 10 additions & 0 deletions tests/baselines/reference/es5-commonjs6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [es5-commonjs6.ts]

export default "test";
var __esModule = 1;


//// [es5-commonjs6.js]
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = "test";
var __esModule = 1;
6 changes: 6 additions & 0 deletions tests/baselines/reference/es5-commonjs6.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/es5-commonjs6.ts ===

export default "test";
var __esModule = 1;
>__esModule : Symbol(__esModule, Decl(es5-commonjs6.ts, 2, 3))

7 changes: 7 additions & 0 deletions tests/baselines/reference/es5-commonjs6.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/es5-commonjs6.ts ===

export default "test";
var __esModule = 1;
>__esModule : number
>1 : number

34 changes: 34 additions & 0 deletions tests/baselines/reference/es5-system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [es5-system.ts]

export default class A
{
constructor ()
{

}

public B()
{
return 42;
}
}


//// [es5-system.js]
System.register([], function(exports_1) {
var A;
return {
setters:[],
execute: function() {
A = (function () {
function A() {
}
A.prototype.B = function () {
return 42;
};
return A;
})();
exports_1("default", A);
}
}
});
17 changes: 17 additions & 0 deletions tests/baselines/reference/es5-system.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/es5-system.ts ===

export default class A
>A : Symbol(A, Decl(es5-system.ts, 0, 0))
{
constructor ()
{

}

public B()
>B : Symbol(B, Decl(es5-system.ts, 6, 5))
{
return 42;
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/es5-system.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/es5-system.ts ===

export default class A
>A : A
{
constructor ()
{

}

public B()
>B : () => number
{
return 42;
>42 : number
}
}

1 change: 1 addition & 0 deletions tests/baselines/reference/es5-umd3.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export default class A
};
return A;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = A;
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var C = (function () {
C.prototype.method = function () { };
return C;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = C;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var default_1 = (function () {
default_1.prototype.method = function () { };
return default_1;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var C = (function () {
};
return C;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = C;
var after = new C();
var t = C;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/es5ExportDefaultExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default (1 + 2);


//// [es5ExportDefaultExpression.js]
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (1 + 2);


Expand Down
Loading