From a36c1b4a9235b3a6418e950efa1b4289c4a86111 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 24 Apr 2015 11:20:29 +0100 Subject: [PATCH] rewrite `this` in shadowed functions inside native derived class constructors - fixes #1340 --- .../transformers/es6/classes.js | 20 +++++++++++++++++++ .../es6.classes/native-constructors/actual.js | 7 +++++++ .../native-constructors/expected.js | 18 +++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/babel/transformation/transformers/es6/classes.js b/src/babel/transformation/transformers/es6/classes.js index 88f2f160b8..bf3fe9b413 100644 --- a/src/babel/transformation/transformers/es6/classes.js +++ b/src/babel/transformation/transformers/es6/classes.js @@ -37,6 +37,22 @@ var collectPropertyReferencesVisitor = { } }; +var constructorVisitor = traverse.explode({ + ThisExpression: { + enter(node, parent, scope, ref) { + return ref; + } + }, + + Function: { + enter(node) { + if (!node.shadow) { + this.skip(); + } + } + } +}); + var verifyConstructorVisitor = traverse.explode({ MethodDefinition: { enter() { @@ -581,6 +597,10 @@ class ClassTransformer { fnPath.scope.rename(this.classRef.name); } + if (this.isNativeSuper) { + fnPath.traverse(constructorVisitor, this.nativeSuperRef); + } + var construct = this.constructor; var fn = method.value; diff --git a/test/core/fixtures/transformation/es6.classes/native-constructors/actual.js b/test/core/fixtures/transformation/es6.classes/native-constructors/actual.js index 17e9a3582b..2868340efd 100644 --- a/test/core/fixtures/transformation/es6.classes/native-constructors/actual.js +++ b/test/core/fixtures/transformation/es6.classes/native-constructors/actual.js @@ -8,3 +8,10 @@ class Bar extends Array { this.foo = "bar"; } } + +class Baz extends Array { + constructor() { + super(); + (() => this) + } +} diff --git a/test/core/fixtures/transformation/es6.classes/native-constructors/expected.js b/test/core/fixtures/transformation/es6.classes/native-constructors/expected.js index de637609fa..6b104753a0 100644 --- a/test/core/fixtures/transformation/es6.classes/native-constructors/expected.js +++ b/test/core/fixtures/transformation/es6.classes/native-constructors/expected.js @@ -33,3 +33,21 @@ var Bar = (function (_Array2) { babelHelpers.inherits(Bar, _Array2); return Bar; })(Array); + +var Baz = (function (_Array3) { + function Baz() { + babelHelpers.classCallCheck(this, Baz); + + var _this3 = new _Array3(); + + _this3.__proto__ = Baz.prototype; + + (function () { + return _this3; + }); + return _this3; + } + + babelHelpers.inherits(Baz, _Array3); + return Baz; +})(Array);