diff --git a/lib/6to5/templates/class-inherits-properties.js b/lib/6to5/templates/class-inherits-properties.js deleted file mode 100644 index c1cbccab49..0000000000 --- a/lib/6to5/templates/class-inherits-properties.js +++ /dev/null @@ -1 +0,0 @@ -CLASS_NAME.__proto__ = SUPER_NAME; diff --git a/lib/6to5/templates/class-inherits-prototype.js b/lib/6to5/templates/class-inherits-prototype.js deleted file mode 100644 index 343298086e..0000000000 --- a/lib/6to5/templates/class-inherits-prototype.js +++ /dev/null @@ -1,8 +0,0 @@ -CLASS_NAME.prototype = Object.create(SUPER_NAME.prototype, { - constructor: { - value: CLASS_NAME, - enumerable: false, - writable: true, - configurable: true - } -}); diff --git a/lib/6to5/templates/extends.js b/lib/6to5/templates/extends.js new file mode 100644 index 0000000000..bc950dfd9a --- /dev/null +++ b/lib/6to5/templates/extends.js @@ -0,0 +1,11 @@ +(function (child, parent) { + child.prototype = Object.create(parent.prototype, { + constructor: { + value: child, + enumerable: false, + writable: true, + configurable: true + } + }); + child.__proto__ = parent; +}) diff --git a/lib/6to5/transformers/classes.js b/lib/6to5/transformers/classes.js index 6b8e02d950..0dac35520e 100644 --- a/lib/6to5/transformers/classes.js +++ b/lib/6to5/transformers/classes.js @@ -1,6 +1,6 @@ var traverse = require("../traverse"); var util = require("../util"); -var b = require("ast-types").builders; +var b = require("acorn-ast-types").builders; var _ = require("lodash"); exports.ClassDeclaration = function (node, parent, file) { @@ -50,17 +50,7 @@ var buildClass = function (node, file) { var returnStatement = body.pop(); if (superName) { - // inherit prototype - body.push(util.template("class-inherits-prototype", { - SUPER_NAME: superName, - CLASS_NAME: className - }, true)); - - // inherit static properties - body.push(util.template("class-inherits-properties", { - SUPER_NAME: superName, - CLASS_NAME: className - }, true)); + body.push(b.expressionStatement(b.callExpression(file.addDeclaration("extends"), [className, superName]))); container.arguments.push(superClassArgument); container.callee.params.push(superClassCallee); diff --git a/test/fixtures/syntax/classes/accessing-super-class/actual.js b/test/fixtures/syntax/classes/accessing-super-class/actual.js index c27fcb5ecb..64b349152c 100644 --- a/test/fixtures/syntax/classes/accessing-super-class/actual.js +++ b/test/fixtures/syntax/classes/accessing-super-class/actual.js @@ -1,26 +1,26 @@ class Test extends Foo { constructor() { - woops.super.test(); - super(); - super.test(); - foob(super); + woops.super.test(); + super(); + super.test(); + foob(super); - super(...arguments); - super("test", ...arguments); + super(...arguments); + super("test", ...arguments); - super.test(...arguments); - super.test("test", ...arguments); + super.test(...arguments); + super.test("test", ...arguments); } test() { - super(); - super(...arguments); - super("test", ...arguments); + super(); + super(...arguments); + super("test", ...arguments); } static foo() { - super(); - super(...arguments); - super("test", ...arguments); + super(); + super(...arguments); + super("test", ...arguments); } } diff --git a/test/fixtures/syntax/classes/accessing-super-class/expected.js b/test/fixtures/syntax/classes/accessing-super-class/expected.js index 29748aca2e..f9fa968043 100644 --- a/test/fixtures/syntax/classes/accessing-super-class/expected.js +++ b/test/fixtures/syntax/classes/accessing-super-class/expected.js @@ -1,5 +1,17 @@ "use strict"; var _slice = Array.prototype.slice; +var _extends = function (child, parent) { + child.prototype = Object.create(parent.prototype, { + constructor: { + value: child, + enumerable: false, + writable: true, + configurable: true + } + }); + + child.__proto__ = parent; +}; var Test = function(Foo) { var Test = function Test() { @@ -13,16 +25,7 @@ var Test = function(Foo) { Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments))); }; - Test.prototype = Object.create(Foo.prototype, { - constructor: { - value: Test, - enumerable: false, - writable: true, - configurable: true - } - }); - - Test.__proto__ = Foo; + _extends(Test, Foo); Object.defineProperties(Test.prototype, { test: { @@ -49,4 +52,4 @@ var Test = function(Foo) { }); return Test; -}(Foo); \ No newline at end of file +}(Foo); diff --git a/test/fixtures/syntax/classes/accessing-super-properties/actual.js b/test/fixtures/syntax/classes/accessing-super-properties/actual.js index 5ae821c032..524a05eaa4 100644 --- a/test/fixtures/syntax/classes/accessing-super-properties/actual.js +++ b/test/fixtures/syntax/classes/accessing-super-properties/actual.js @@ -1,6 +1,6 @@ class Test extends Foo { constructor() { - super.test; - super.test.whatever; + super.test; + super.test.whatever; } } diff --git a/test/fixtures/syntax/classes/accessing-super-properties/expected.js b/test/fixtures/syntax/classes/accessing-super-properties/expected.js index 5013de099b..87fc08e3fa 100644 --- a/test/fixtures/syntax/classes/accessing-super-properties/expected.js +++ b/test/fixtures/syntax/classes/accessing-super-properties/expected.js @@ -1,20 +1,25 @@ "use strict"; +var _extends = function (child, parent) { + child.prototype = Object.create(parent.prototype, { + constructor: { + value: child, + enumerable: false, + writable: true, + configurable: true + } + }); + + child.__proto__ = parent; +}; + var Test = function(Foo) { var Test = function Test() { Foo.prototype.test; Foo.prototype.test.whatever; }; - Test.prototype = Object.create(Foo.prototype, { - constructor: { - value: Test, - enumerable: false, - writable: true, - configurable: true - } - }); + _extends(Test, Foo); - Test.__proto__ = Foo; return Test; -}(Foo); \ No newline at end of file +}(Foo); diff --git a/test/fixtures/syntax/classes/calling-super-properties/actual.js b/test/fixtures/syntax/classes/calling-super-properties/actual.js index e0aa30ef4e..993109dc24 100644 --- a/test/fixtures/syntax/classes/calling-super-properties/actual.js +++ b/test/fixtures/syntax/classes/calling-super-properties/actual.js @@ -1,10 +1,10 @@ class Test extends Foo { constructor() { - super.test.whatever(); - super.test(); + super.test.whatever(); + super.test(); } static test() { - return super.wow(); + return super.wow(); } } diff --git a/test/fixtures/syntax/classes/calling-super-properties/expected.js b/test/fixtures/syntax/classes/calling-super-properties/expected.js index c5efd91967..5bbc5432d7 100644 --- a/test/fixtures/syntax/classes/calling-super-properties/expected.js +++ b/test/fixtures/syntax/classes/calling-super-properties/expected.js @@ -1,21 +1,26 @@ "use strict"; +var _extends = function (child, parent) { + child.prototype = Object.create(parent.prototype, { + constructor: { + value: child, + enumerable: false, + writable: true, + configurable: true + } + }); + + child.__proto__ = parent; +}; + + var Test = function(Foo) { var Test = function Test() { Foo.prototype.test.whatever(); Foo.prototype.test.call(this); }; - Test.prototype = Object.create(Foo.prototype, { - constructor: { - value: Test, - enumerable: false, - writable: true, - configurable: true - } - }); - - Test.__proto__ = Foo; + _extends(Test, Foo); Object.defineProperties(Test, { test: { @@ -28,4 +33,4 @@ var Test = function(Foo) { }); return Test; -}(Foo); \ No newline at end of file +}(Foo); diff --git a/test/fixtures/syntax/classes/constructor/actual.js b/test/fixtures/syntax/classes/constructor/actual.js index af62512cf9..2d26f87082 100644 --- a/test/fixtures/syntax/classes/constructor/actual.js +++ b/test/fixtures/syntax/classes/constructor/actual.js @@ -1,11 +1,11 @@ class Test { constructor() { - this.state = "test"; + this.state = "test"; } } class Foo extends Bar { constructor() { - this.state = "test"; + this.state = "test"; } } diff --git a/test/fixtures/syntax/classes/constructor/expected.js b/test/fixtures/syntax/classes/constructor/expected.js index b5657cfd8a..57865f997b 100644 --- a/test/fixtures/syntax/classes/constructor/expected.js +++ b/test/fixtures/syntax/classes/constructor/expected.js @@ -1,5 +1,18 @@ "use strict"; +var _extends = function (child, parent) { + child.prototype = Object.create(parent.prototype, { + constructor: { + value: child, + enumerable: false, + writable: true, + configurable: true + } + }); + + child.__proto__ = parent; +}; + var Test = function() { var Test = function Test() { this.state = "test"; @@ -13,15 +26,7 @@ var Foo = function(Bar) { this.state = "test"; }; - Foo.prototype = Object.create(Bar.prototype, { - constructor: { - value: Foo, - enumerable: false, - writable: true, - configurable: true - } - }); + _extends(Foo, Bar); - Foo.__proto__ = Bar; return Foo; -}(Bar); \ No newline at end of file +}(Bar); diff --git a/test/fixtures/syntax/classes/super-class-id-member-expression/expected.js b/test/fixtures/syntax/classes/super-class-id-member-expression/expected.js index 3ff37e333e..c6c1a4081b 100644 --- a/test/fixtures/syntax/classes/super-class-id-member-expression/expected.js +++ b/test/fixtures/syntax/classes/super-class-id-member-expression/expected.js @@ -1,20 +1,25 @@ "use strict"; -var BaseController = function(Chaplin) { - var BaseController = function BaseController() { - Chaplin.Controller.apply(this, arguments); - }; - - BaseController.prototype = Object.create(Chaplin.Controller.prototype, { +var _extends = function (child, parent) { + child.prototype = Object.create(parent.prototype, { constructor: { - value: BaseController, + value: child, enumerable: false, writable: true, configurable: true } }); - BaseController.__proto__ = Chaplin.Controller; + child.__proto__ = parent; +}; + +var BaseController = function(Chaplin) { + var BaseController = function BaseController() { + Chaplin.Controller.apply(this, arguments); + }; + + _extends(BaseController, Chaplin.Controller); + return BaseController; }(Chaplin); @@ -23,15 +28,7 @@ var BaseController2 = function(Chaplin) { Chaplin.Controller.Another.apply(this, arguments); }; - BaseController2.prototype = Object.create(Chaplin.Controller.Another.prototype, { - constructor: { - value: BaseController2, - enumerable: false, - writable: true, - configurable: true - } - }); + _extends(BaseController2, Chaplin.Controller.Another); - BaseController2.__proto__ = Chaplin.Controller.Another; return BaseController2; -}(Chaplin); \ No newline at end of file +}(Chaplin); diff --git a/test/fixtures/syntax/classes/super-class-id-non-identifiers/expected.js b/test/fixtures/syntax/classes/super-class-id-non-identifiers/expected.js index f238d5aa06..61b8ece11d 100644 --- a/test/fixtures/syntax/classes/super-class-id-non-identifiers/expected.js +++ b/test/fixtures/syntax/classes/super-class-id-non-identifiers/expected.js @@ -1,19 +1,24 @@ "use strict"; -var Q = function(_ref) { - var Q = function Q() { - _ref.apply(this, arguments); - }; - - Q.prototype = Object.create(_ref.prototype, { +var _extends = function (child, parent) { + child.prototype = Object.create(parent.prototype, { constructor: { - value: Q, + value: child, enumerable: false, writable: true, configurable: true } }); - Q.__proto__ = _ref; + child.__proto__ = parent; +}; + +var Q = function(_ref) { + var Q = function Q() { + _ref.apply(this, arguments); + }; + + _extends(Q, _ref); + return Q; -}(function() {}); \ No newline at end of file +}(function() {}); diff --git a/test/fixtures/syntax/classes/super-class/expected.js b/test/fixtures/syntax/classes/super-class/expected.js index 129e3a2c6b..38a17bb449 100644 --- a/test/fixtures/syntax/classes/super-class/expected.js +++ b/test/fixtures/syntax/classes/super-class/expected.js @@ -1,19 +1,24 @@ "use strict"; -var Test = function(Foo) { - var Test = function Test() { - Foo.apply(this, arguments); - }; - - Test.prototype = Object.create(Foo.prototype, { +var _extends = function (child, parent) { + child.prototype = Object.create(parent.prototype, { constructor: { - value: Test, + value: child, enumerable: false, writable: true, configurable: true } }); - Test.__proto__ = Foo; + child.__proto__ = parent; +}; + +var Test = function(Foo) { + var Test = function Test() { + Foo.apply(this, arguments); + }; + + _extends(Test, Foo); + return Test; -}(Foo); \ No newline at end of file +}(Foo);