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

react-hot-loader/babel creates invalid code with async arrow functions #370

Closed
Jessidhia opened this issue Sep 20, 2016 · 8 comments
Closed
Labels

Comments

@Jessidhia
Copy link

Jessidhia commented Sep 20, 2016

On a class (that is not a React component), there is a method defined through an arrow function:

show = async (id, type) => {
  const { data } = await apiRequest(/*...*/)

Without the es2015 preset (only stage-2, react, and react-hot-loader/babel), this gets transformed to:

_createClass(ClassName, [{
    key: '__show__REACT_HOT_LOADER__',
    value: function __show__REACT_HOT_LOADER__(id, type) {
      var { data } = await apiRequest(/*...*/);

This creates an await statement not inside an async function, which is invalid syntax.

This happens with beta.4, but rolling back to (the previously working?) beta.3 does not seem to help.

@Jessidhia
Copy link
Author

Correction: this happens even with the es2015 preset, as the plugin run before presets, and it deletes the async keyword that the transform-async-to-generator relies on.

@jquense
Copy link
Contributor

jquense commented Sep 20, 2016

JUST SPENT 6 HOURS TRYING TO SOLVE THIS

😭

(not yelling at anyone just expressing relief that i finally located the issue)

@calesce calesce added the bug label Sep 20, 2016
@calesce
Copy link
Collaborator

calesce commented Sep 20, 2016

😞 That's no good! Sorry @jquense!

It makes sense that this would happen in beta.4, since we added the class properties transform, and the plugin is not yet aware of async functions.

@jquense
Copy link
Contributor

jquense commented Sep 20, 2016

I think it should be a straight forward fix, test case:

actual

class Foo {
  bar = async (a, b) => {
    return await a(b);
  };
}

expected

var _this = this;

class Foo {
  bar = async (...params) => await _this.__bar__REACT_HOT_LOADER__(...params);

  async __bar__REACT_HOT_LOADER__(a, b) {
    return await a(b);
  }

}
;

(function () {
  if (typeof __REACT_HOT_LOADER__ === 'undefined') {
    return;
  }

  __REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();

;

@calesce
Copy link
Collaborator

calesce commented Sep 20, 2016

@jquense Awesome, thanks!

@calesce
Copy link
Collaborator

calesce commented Sep 22, 2016

Out in 3.0.0-beta.5.

@calesce calesce closed this as completed Sep 22, 2016
@SDSLeon
Copy link

SDSLeon commented Sep 22, 2016

webpack 2, 3.0.0-beta.5 'this' context lost in async arrow functions;

name = async () => {
this is undefined;
}

@jquense
Copy link
Contributor

jquense commented Sep 22, 2016

seems like that would be true for any class prop function? I wonder why...

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

No branches or pull requests

4 participants