From 3878bd812c73bdd18b1011be59515dad985940fd Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 3 May 2015 23:45:26 +0100 Subject: [PATCH] remove native super inheritance from classes - fixes #1424 --- .../transformers/es6/classes.js | 52 ++---------------- .../es6.classes/native-constructors/actual.js | 17 ------ .../es6.classes/native-constructors/exec.js | 27 ---------- .../native-constructors/expected.js | 53 ------------------- 4 files changed, 5 insertions(+), 144 deletions(-) delete mode 100644 test/core/fixtures/transformation/es6.classes/native-constructors/actual.js delete mode 100644 test/core/fixtures/transformation/es6.classes/native-constructors/exec.js delete mode 100644 test/core/fixtures/transformation/es6.classes/native-constructors/expected.js diff --git a/src/babel/transformation/transformers/es6/classes.js b/src/babel/transformation/transformers/es6/classes.js index 27fee12443..2a4fe261cc 100644 --- a/src/babel/transformation/transformers/es6/classes.js +++ b/src/babel/transformation/transformers/es6/classes.js @@ -96,10 +96,6 @@ var verifyConstructorVisitor = traverse.explode({ if (state.hasSuper && !state.hasBareSuper) { throw this.errorWithNode("'this' is not allowed before super()"); } - - if (state.isNativeSuper) { - return state.nativeSuperRef; - } } } }); @@ -154,15 +150,6 @@ class ClassTransformer { // - var superClass = this.node.superClass; - this.isNativeSuper = superClass && t.isIdentifier(superClass) && t.NATIVE_TYPE_NAMES.indexOf(superClass.name) >= 0; - - if (this.isNativeSuper) { - this.nativeSuperRef = this.scope.generateUidIdentifier("this"); - } - - // - var body = this.body; // @@ -234,12 +221,6 @@ class ClassTransformer { } } - if (this.isNativeSuper) { - // we've determined this is inheriting from a native class so return the constructed - // instance - constructorBody.body.push(t.returnStatement(this.nativeSuperRef)); - } - if (this.className) { // named class with only a constructor if (body.length === 1) return t.toExpression(body[0]); @@ -344,9 +325,7 @@ class ClassTransformer { if (!this.hasConstructor && this.hasSuper) { var helperName = "class-super-constructor-call"; if (this.isLoose) helperName += "-loose"; - if (this.isNativeSuper) helperName = "class-super-native-constructor-call"; constructorBody.body.push(util.template(helperName, { - NATIVE_REF: this.nativeSuperRef, CLASS_NAME: this.classRef, SUPER_NAME: this.superName }, true)); @@ -475,12 +454,10 @@ class ClassTransformer { verifyConstructor(path: TraversalPath) { var state = { - nativeSuperRef: this.nativeSuperRef, - isNativeSuper: this.isNativeSuper, - hasBareSuper: false, - bareSuper: null, - hasSuper: this.hasSuper, - file: this.file + hasBareSuper: false, + bareSuper: null, + hasSuper: this.hasSuper, + file: this.file }; path.get("value").traverse(verifyConstructorVisitor, state); @@ -490,22 +467,7 @@ class ClassTransformer { if (!state.hasBareSuper && this.hasSuper) { throw path.errorWithNode("Derived constructor must call super()"); } - - if (this.isNativeSuper && this.bareSuper) { - this.bareSuper.replaceWithMultiple([ - t.variableDeclaration("var", [ - t.variableDeclarator(this.nativeSuperRef, t.newExpression(this.superName, this.bareSuper.node.arguments)) - ]), - - t.expressionStatement(t.assignmentExpression( - "=", - t.memberExpression(this.nativeSuperRef, t.identifier("__proto__")), - t.memberExpression(this.classRef, t.identifier("prototype")) - )), - t.expressionStatement(this.nativeSuperRef) - ]); - } - } + } /** * Push a method to its respective mutatorMap. @@ -605,10 +567,6 @@ 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 deleted file mode 100644 index 2868340efd..0000000000 --- a/test/core/fixtures/transformation/es6.classes/native-constructors/actual.js +++ /dev/null @@ -1,17 +0,0 @@ -class Foo extends Array { - -} - -class Bar extends Array { - constructor() { - super(); - this.foo = "bar"; - } -} - -class Baz extends Array { - constructor() { - super(); - (() => this) - } -} diff --git a/test/core/fixtures/transformation/es6.classes/native-constructors/exec.js b/test/core/fixtures/transformation/es6.classes/native-constructors/exec.js deleted file mode 100644 index 0bccf26aa8..0000000000 --- a/test/core/fixtures/transformation/es6.classes/native-constructors/exec.js +++ /dev/null @@ -1,27 +0,0 @@ -class Foo extends Array { - -} - -class Bar extends Array { - constructor() { - super(); - this.foo = "bar"; - } -} - -var foo = new Foo; -assert.ok(Array.isArray(foo)); -foo.push(1); -foo.push(2); -assert.equal(foo[0], 1); -assert.equal(foo[1], 2); -assert.equal(foo.length, 2); - -var bar = new Bar; -assert.ok(Array.isArray(bar)); -assert.equal(bar.foo, "bar"); -bar.push(1); -bar.push(2); -assert.equal(bar[0], 1); -assert.equal(bar[1], 2); -assert.equal(bar.length, 2); diff --git a/test/core/fixtures/transformation/es6.classes/native-constructors/expected.js b/test/core/fixtures/transformation/es6.classes/native-constructors/expected.js deleted file mode 100644 index 6b104753a0..0000000000 --- a/test/core/fixtures/transformation/es6.classes/native-constructors/expected.js +++ /dev/null @@ -1,53 +0,0 @@ -"use strict"; - -var Foo = (function (_Array) { - function Foo() { - babelHelpers.classCallCheck(this, Foo); - - if (_Array != null) { - var _this = new (babelHelpers.bind.apply(_Array, [null].concat(babelHelpers.slice.call(arguments))))(); - - _this.__proto__ = Foo.prototype; - return _this; - } - - return _this; - } - - babelHelpers.inherits(Foo, _Array); - return Foo; -})(Array); - -var Bar = (function (_Array2) { - function Bar() { - babelHelpers.classCallCheck(this, Bar); - - var _this2 = new _Array2(); - - _this2.__proto__ = Bar.prototype; - - _this2.foo = "bar"; - return _this2; - } - - 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);