diff --git a/src/babel/index.js b/src/babel/index.js index f0f998106..569ff7a7d 100644 --- a/src/babel/index.js +++ b/src/babel/index.js @@ -14,15 +14,20 @@ const buildTagger = template(` })(); `); -const buildNewClassProperty = (t, classPropertyName, newMethodName) => { - const returnExpression = t.callExpression( +const buildNewClassProperty = (t, classPropertyName, newMethodName, isAsync) => { + let returnExpression = t.callExpression( t.memberExpression(t.thisExpression(), newMethodName), [t.spreadElement(t.identifier('params'))] ); + if (isAsync) { + returnExpression = t.awaitExpression(returnExpression); + } + const newArrowFunction = t.arrowFunctionExpression( [t.restElement(t.identifier('params'))], - returnExpression + returnExpression, + isAsync ); return t.classProperty(classPropertyName, newArrowFunction); }; @@ -176,6 +181,7 @@ module.exports = function plugin(args) { // class property node value is nullable if (node.value && node.value.type === 'ArrowFunctionExpression') { + const isAsync = node.value.async; const params = node.value.params; const newIdentifier = t.identifier(`__${node.key.name}__REACT_HOT_LOADER__`); @@ -187,11 +193,12 @@ module.exports = function plugin(args) { // create a new method on the class that the original class property function // calls, since the method is able to be replaced by RHL const newMethod = t.classMethod('method', newIdentifier, params, newMethodBody); + newMethod.async = isAsync; path.insertAfter(newMethod); // replace the original class property function with a function that calls // the new class method created above - path.replaceWith(buildNewClassProperty(t, node.key, newIdentifier)); + path.replaceWith(buildNewClassProperty(t, node.key, newIdentifier, isAsync)); } } }); diff --git a/test/babel/fixtures/async-functions/.babelrc b/test/babel/fixtures/async-functions/.babelrc new file mode 100644 index 000000000..e4d66502a --- /dev/null +++ b/test/babel/fixtures/async-functions/.babelrc @@ -0,0 +1,6 @@ +{ + "plugins": [ + "syntax-class-properties", + "../../../../src/babel" + ] +} diff --git a/test/babel/fixtures/async-functions/actual.js b/test/babel/fixtures/async-functions/actual.js new file mode 100644 index 000000000..7307e91b7 --- /dev/null +++ b/test/babel/fixtures/async-functions/actual.js @@ -0,0 +1,5 @@ +class Foo { + bar = async (a, b) => { + return await a(b); + }; +} diff --git a/test/babel/fixtures/async-functions/expected.js b/test/babel/fixtures/async-functions/expected.js new file mode 100644 index 000000000..6684b19e2 --- /dev/null +++ b/test/babel/fixtures/async-functions/expected.js @@ -0,0 +1,21 @@ +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__); +})(); + +;