diff --git a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js index 95db393195..a6e2ae6ccd 100644 --- a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js +++ b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js @@ -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 }); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/actual.js new file mode 100644 index 0000000000..1ddb4a1e78 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/actual.js @@ -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); + } +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/expected.js new file mode 100644 index 0000000000..864596a22f --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/expected.js @@ -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); + }); + } + +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/options.json b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/options.json new file mode 100644 index 0000000000..f6afa6e24c --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "external-helpers", + "transform-async-to-generator", + "transform-class-properties" + ] +}