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

Conversation

vvakame
Copy link
Contributor

@vvakame vvakame commented Jun 21, 2015

implementation for #2719

@teppeis
Copy link

teppeis commented Jun 21, 2015

GJ! 👍

@@ -2709,6 +2709,17 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
}
else {
if (node.flags & NodeFlags.Default) {
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
write("Object.defineProperty(exports, \"__esModule\", {");
Copy link

Choose a reason for hiding this comment

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

Object.defineProperty is NOT available in ES3.
If compile target is ES3, you may not use it.
Just exports.__esModule = true; like babel's loose mode.

Choose a reason for hiding this comment

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

If you want to emulate this automatically, putting the define property behind a languageVersion >= ScriptTarget.ES5 and adding a write("exports.__esModule = true"); for es3 would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks! applied.

if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
if (languageVersion >= ScriptTarget.ES5) {
write("Object.defineProperty(exports, \"__esModule\", {");
writeLine();
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we put it on one line. i.e. loose the indent and the new line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vvakame
Copy link
Contributor Author

vvakame commented Jul 3, 2015

how is it?

@RyanCavanaugh
Copy link
Member

Ping @mhegazy

@mhegazy
Copy link
Contributor

mhegazy commented Jul 6, 2015

Sorry for the delay. i had a comment that i wanted to fix as i merge the review, (!hasProperty(currentSourceFile.identifiers, "___esModule" is a bit too aggressive, as a local var with the name "__esModule" would trigger this behavior. though uncommon, i think we should only look for exported properties. i will take care of it, and hopefully get this change in today. sorry for the delay.

@@ -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.

@vvakame
Copy link
Contributor Author

vvakame commented Jul 12, 2015

@mhegazy @JsonFreeman thanks for reviewing. I make a new commit. is it reflect your opinions?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 13, 2015

👍

@vvakame
Copy link
Contributor Author

vvakame commented Jul 15, 2015

@JsonFreeman I want to assert calling context of emitEs6ExportDefaultCompat.
I think read function code is easier than reading context.

@JsonFreeman
Copy link
Contributor

Can you change the conditions to Debug.assert calls then?

@vvakame
Copy link
Contributor Author

vvakame commented Jul 15, 2015

@JsonFreeman sure!

@@ -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)

@vvakame
Copy link
Contributor Author

vvakame commented Jul 21, 2015

@JsonFreeman 💭

@JsonFreeman
Copy link
Contributor

Sorry @vvakame, I did not see... This is good to go 👍 Thanks!

JsonFreeman added a commit that referenced this pull request Jul 21, 2015
improve  module loading interoperability for babel
@JsonFreeman JsonFreeman merged commit 4e0acc9 into microsoft:master Jul 21, 2015
@vvakame vvakame deleted the addEsModule branch July 21, 2015 10:36
@vvakame
Copy link
Contributor Author

vvakame commented Jul 21, 2015

✨ 😸 ✨

@mhegazy
Copy link
Contributor

mhegazy commented Jul 21, 2015

thanks @vvakame !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants