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

Enable await in ES5 and ES2015 script mode #5852

Merged
merged 3 commits into from
Dec 2, 2015

Conversation

holtwick
Copy link
Contributor

@holtwick holtwick commented Dec 1, 2015

Even though strictly generators are an ES6 feature the real world support is large enough to use the feature in well known environments like node.js or Electron app.

Since the previous output was not working at all anyway it feels like a good compromise to at least emit working code while still having the warning in place.

Even though strictly generators are an ES6 feature the real world support
is large enough to use the feature in well known environments like
node.js or Electron app. Since the previous output was not working at
all anyway it feels like a good compromise to at least emit working code
while still having the warning in place. The user would also need to add
"use strict" on top of her .ts file to make it work with node.js.
@msftclas
Copy link

msftclas commented Dec 1, 2015

Hi @holtwick, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. Real humans will now evaluate your PR.

TTYL, MSBOT;

@holtwick holtwick changed the title Enable await in ES6 and ES2015 script mode Enable await in ES5 and ES2015 script mode Dec 1, 2015
// Even though generators are a ES6 only feature, the functionality is wiedely supported
// in current browsers and latest node, therefore showing some tolerance
const isAsync = isAsyncFunctionLike(node);
if (isAsync && (languageVersion === ScriptTarget.ES6 || languageVersion === ScriptTarget.ES2015 || languageVersion === ScriptTarget.ES5)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the actual change. Sorry, my IDE modified some whitespace ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

please change the condition to just if (isAsync) and i would appreciate it if you fix the indentation issue.

@lbguilherme
Copy link

I disagree. When targeting ES5, only ES5 should be emitted, not ES5.5. This is related to the issue #4692, Granular Targeting.

Your best bet would be to target ES6 and use babel to transpile just what you need to (transpile classes, but leave generators as is).

@holtwick
Copy link
Contributor Author

holtwick commented Dec 1, 2015

@lbguilherme thanks for pointing to #4692. Indeed some target specific code emission sounds reasonable to me. Using Babel or the like instead as a second pass sounds like overkill and probably adds another source of failures for the end user.

As I mentioned before, the transpiler currently emits completely invalid code anyway. So this change would not harm and the error is still presented in my minimal change.

To illustrate what actually changes in the emitted code. This would be TS:

await foo();

With ES5 option the current implementation would emit:

yield foo();

This code can fail for 2 reasons: 1. Because yield is not supported or 2. because the wrapping function would need too look like function *bar().

Instead with this patch applied you get the same output as for ES6, which is:

return __awaiter(this, void 0, Promise, function* () {
    yield foo();
});

The __awaiter and everything else is already emitted anyway.

IMHO it would be great to have that little change, because for projects only targetting node.js, Electron or CEF this is a huge win.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2015

@holtwick, stupid question, why are you emitting to ES5 and not ES6 if you are running on node v4+ or Electron?

@holtwick
Copy link
Contributor Author

holtwick commented Dec 1, 2015

@mhegazy, some ES6 features are not supported by node or Chrome/CEF/Electron right now, like default parameters function x(a=1, b=2) to name one. The TS es6 flag is all or nothing.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2015

not sure this is the right solution though. we make no guarantees in the case of errors. the correct fix is to address #4692. i am open to taking this PR, but i do not think it solves the issue, it just kicks the can down the road until you run into the next blocking issue.

@holtwick
Copy link
Contributor Author

holtwick commented Dec 1, 2015

As I see from https://github.com/Microsoft/TypeScript/wiki/Roadmap async/await support is on the roadmap for 2.0 anyway? That's great news.

I agree that my patch is just a quick fix and that a solution in the context of #4692 or adding some other kind of other flag would be a more elegant and conform solution. Although this little patch would already get most use cases satisfied without any major drawbacks, since the errors are still in place. So if you could merge it, I would be very grateful.

If you like to I can elaborate more on the topic and present a follow up PR. For example the next issue down the road would be the use of async keyword with arrow functions ;)

@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2015

ok then, I had one comment, please update the PR.

@holtwick
Copy link
Contributor Author

holtwick commented Dec 1, 2015

Done. Thanks a lot.

mhegazy added a commit that referenced this pull request Dec 2, 2015
Enable await in ES5 and ES2015 script mode
@mhegazy mhegazy merged commit 19d7e62 into microsoft:master Dec 2, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 2, 2015

thanks!

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
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

4 participants