From f408f28a027655faefcfabeb50c0f3b9f0b148b6 Mon Sep 17 00:00:00 2001 From: Andrew Johnston Date: Sat, 21 May 2016 16:18:48 -0400 Subject: [PATCH] Fix: assign _this to `this` when there is no Superclass - Fixes T7364 Closes gh-3508 --- .../internal-plugins/shadow-functions.js | 5 ++- .../test/fixtures/regression/T7364/actual.js | 17 ++++++++++ .../fixtures/regression/T7364/expected.js | 32 +++++++++++++++++++ .../fixtures/regression/T7364/options.json | 7 ++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/actual.js create mode 100644 packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/expected.js create mode 100644 packages/babel-plugin-transform-class-properties/test/fixtures/regression/T7364/options.json 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" + ] +}