From f2fdc74d1bbce9c57b51477446e5846d595cc07c Mon Sep 17 00:00:00 2001 From: horpto <__singleton__@hackerdom.ru> Date: Sun, 10 Jan 2016 21:52:04 +0500 Subject: [PATCH] [2765] transform-async-to-generator doesn't work for `this` inside arrow function --- .../src/index.js | 15 ++++++++++++++- .../async-arrow-in-method/actual.js | 11 +++++++++++ .../async-arrow-in-method/expected.js | 15 +++++++++++++++ .../object-method-with-arrows/actual.js | 6 ++++++ .../object-method-with-arrows/expected.js | 12 ++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/actual.js create mode 100644 packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/expected.js create mode 100644 packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/actual.js create mode 100644 packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/expected.js diff --git a/packages/babel-helper-remap-async-to-generator/src/index.js b/packages/babel-helper-remap-async-to-generator/src/index.js index ea9e55e8d5..8d37ec91b0 100644 --- a/packages/babel-helper-remap-async-to-generator/src/index.js +++ b/packages/babel-helper-remap-async-to-generator/src/index.js @@ -14,11 +14,22 @@ let buildWrapper = template(` }) `); +let arrowBuildWrapper = template(` + (() => { + var ref = FUNCTION; + return (PARAMS) => ref.apply(this, arguments); + }) +`); + let awaitVisitor = { Function(path) { path.skip(); }, + ArrowFunctionExpression(path) { + path.arrowFunctionToShadowed(); + }, + AwaitExpression({ node }) { node.type = "YieldExpression"; } @@ -42,9 +53,11 @@ function classOrObjectMethod(path: NodePath, callId: Object) { function plainFunction(path: NodePath, callId: Object) { let node = path.node; + let wrapper = buildWrapper; if (path.isArrowFunctionExpression()) { path.arrowFunctionToShadowed(); + wrapper = arrowBuildWrapper; } node.async = false; @@ -60,7 +73,7 @@ function plainFunction(path: NodePath, callId: Object) { } let built = t.callExpression(callId, [node]); - let container = buildWrapper({ + let container = wrapper({ FUNCTION: built, PARAMS: node.params.map(() => path.scope.generateUidIdentifier("x")) }).expression; diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/actual.js new file mode 100644 index 0000000000..17f2cb6ce8 --- /dev/null +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/actual.js @@ -0,0 +1,11 @@ +let TestClass = { + name: "John Doe", + + testMethodFailure() + { + return new Promise(async (resolve) => { + console.log(this); + setTimeout(resolve, 1000); + }); + } +}; diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/expected.js new file mode 100644 index 0000000000..4d3d6e8b43 --- /dev/null +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/async-arrow-in-method/expected.js @@ -0,0 +1,15 @@ +let TestClass = { + name: "John Doe", + + testMethodFailure() { + return new Promise((() => { + var _this = this; + + var ref = babelHelpers.asyncToGenerator(function* (resolve) { + console.log(_this); + setTimeout(resolve, 1000); + }); + return _x => ref.apply(this, arguments); + })()); + } +}; diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/actual.js new file mode 100644 index 0000000000..e69bddc32a --- /dev/null +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/actual.js @@ -0,0 +1,6 @@ +class Class { + async method() { + this; + () => this; + } +} diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/expected.js new file mode 100644 index 0000000000..809d505d9f --- /dev/null +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/object-method-with-arrows/expected.js @@ -0,0 +1,12 @@ +class Class { + method() { + var _this = this; + + return babelHelpers.asyncToGenerator(function* () { + _this; + (function () { + return _this; + }); + })(); + } +}