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 3 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
21 changes: 20 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2687,6 +2687,22 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
return result;
}

function emitEs6ExportDefaultCompat() {
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
if (!hasProperty(currentSourceFile.identifiers, "___esModule")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, this should check only the properties of the module object.

if (languageVersion >= ScriptTarget.ES5) {
// default value of configurable, enumerable, writable are `false`.
write("Object.defineProperty(exports, \"__esModule\", { value: true });");
writeLine();
}
else {
write("exports.__esModule = true;");
writeLine();
Copy link
Contributor

Choose a reason for hiding this comment

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

Move writeLine after the if-else so it only appears once. Not a big deal though.

}
}
}
}

function emitExportMemberAssignment(node: FunctionLikeDeclaration | ClassDeclaration) {
if (node.flags & NodeFlags.Export) {
writeLine();
Expand All @@ -2709,9 +2725,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
}
else {
if (node.flags & NodeFlags.Default) {
emitEs6ExportDefaultCompat();
Copy link
Contributor

Choose a reason for hiding this comment

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

It was tricky to convince myself that this would not get called in ES6. To do so, I had to look at the calls to emitExportMemberAssignment. I found two:

  1. emitClassLikeDeclarationBelowES6 (this one was obviously below ES6)
  2. emitSignatureAndBody (this one was not obvious)

The call from emitSignatureAndBody is guarded by !isES6ExportedDeclaration. For isES6ExportedDeclaration to be false, any of its 3 disjuncts could be false. We want the ES6 one to be false, so let's trying to prove the other 2 true.

  1. !!(node.flags & NodeFlags.Export): This is true because of line 2707
  2. node.parent.kind === SyntaxKind.SourceFile: This is true by the rules of our language. We only allow export default at a source file level, or an ambient external module. We don't emit javascript for an ambient external module, so in legal code, it must be in a source file.

I think the following would help with static reasoning. Take the above check node.parent === currentSourceFile and guard both the System and the default cases with that. Possibly also change it to node.parent.kind === SyntaxKind.SourceFile to make it even clearer.

if (languageVersion === ScriptTarget.ES3) {
write("exports[\"default\"]");
} else {
}
else {
write("exports.default");
}
}
Expand Down Expand Up @@ -4867,6 +4885,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
write(")");
}
else {
emitEs6ExportDefaultCompat();
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
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default function f() { }

//// [es5ExportDefaultFunctionDeclaration.js]
function f() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = f;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default function () { }

//// [es5ExportDefaultFunctionDeclaration2.js]
function 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 @@ -13,6 +13,7 @@ var before = func();
function func() {
return func;
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = func;
var after = func();

Expand Down
Loading