Merge pull request #3257 from horpto/bugfix/T2765

[2765] transform-async-to-generator doesn't work for `this` inside arrow
This commit is contained in:
Amjad Masad
2016-01-17 14:14:56 -08:00
7 changed files with 100 additions and 1 deletions

View File

@@ -14,11 +14,24 @@ let buildWrapper = template(`
})
`);
let arrowBuildWrapper = template(`
(() => {
var ref = FUNCTION;
return (PARAMS) => ref.apply(this, arguments);
})
`);
let awaitVisitor = {
Function(path) {
path.skip();
},
ArrowFunctionExpression(path) {
if (!path.node.async) {
path.arrowFunctionToShadowed();
}
},
AwaitExpression({ node }) {
node.type = "YieldExpression";
}
@@ -42,9 +55,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 +75,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;

View File

@@ -0,0 +1,10 @@
let TestClass = {
name: "John Doe",
testMethodFailure() {
return new Promise(async (resolve) => {
console.log(this);
setTimeout(resolve, 1000);
});
}
};

View File

@@ -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);
})());
}
};

View File

@@ -0,0 +1,13 @@
async function s(x) {
let t = async (y) => {
let r = async (z) => {
await z;
return this.x;
}
await r;
return this.g(r);
}
await t;
return this.h(t);
}

View File

@@ -0,0 +1,28 @@
let s = function () {
var ref = babelHelpers.asyncToGenerator(function* (x) {
let t = (() => {
var _this2 = this;
var ref = babelHelpers.asyncToGenerator(function* (y) {
let r = (() => {
var _this = this;
var ref = babelHelpers.asyncToGenerator(function* (z) {
yield z;
return _this.x;
});
return _x3 => ref.apply(this, arguments);
})();
yield r;
return _this2.g(r);
});
return _x2 => ref.apply(this, arguments);
})();
yield t;
return this.h(t);
});
return function s(_x) {
return ref.apply(this, arguments);
};
}();

View File

@@ -0,0 +1,6 @@
class Class {
async method() {
this;
() => this;
}
}

View File

@@ -0,0 +1,12 @@
class Class {
method() {
var _this = this;
return babelHelpers.asyncToGenerator(function* () {
_this;
(function () {
return _this;
});
})();
}
}