Fix: assign _this to this when there is no Superclass - Fixes T7364

Closes gh-3508
This commit is contained in:
Andrew Johnston 2016-05-21 16:18:48 -04:00 committed by Henry Zhu
parent 8532bb8334
commit f408f28a02
4 changed files with 60 additions and 1 deletions

View File

@ -92,7 +92,10 @@ function remap(path, key) {
fnPath.setData(key, id);
if (key === "this" && fnPath.isMethod({kind: "constructor"})) {
let classPath = fnPath.findParent((p) => p.isClass());
let hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass);
if (key === "this" && fnPath.isMethod({kind: "constructor"}) && hasSuperClass) {
fnPath.scope.push({ id });
fnPath.traverse(superVisitor, { id });

View File

@ -0,0 +1,17 @@
class MyClass {
myAsyncMethod = async () => {
console.log(this);
}
}
(class MyClass2 {
myAsyncMethod = async () => {
console.log(this);
}
})
export default class MyClass3 {
myAsyncMethod = async () => {
console.log(this);
}
}

View File

@ -0,0 +1,32 @@
class MyClass {
constructor() {
var _this = this;
this.myAsyncMethod = babelHelpers.asyncToGenerator(function* () {
console.log(_this);
});
}
}
(class MyClass2 {
constructor() {
var _this2 = this;
this.myAsyncMethod = babelHelpers.asyncToGenerator(function* () {
console.log(_this2);
});
}
});
export default class MyClass3 {
constructor() {
var _this3 = this;
this.myAsyncMethod = babelHelpers.asyncToGenerator(function* () {
console.log(_this3);
});
}
}

View File

@ -0,0 +1,7 @@
{
"plugins": [
"external-helpers",
"transform-async-to-generator",
"transform-class-properties"
]
}