From 3cf7b2b761ad01464155d163bd9b04d8f1f21493 Mon Sep 17 00:00:00 2001 From: Ondrej Kraus Date: Mon, 23 Feb 2015 17:19:41 +0100 Subject: [PATCH 1/4] add name to constructor of extended anonymous class --- lib/babel/transformation/transformers/es6/classes.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/babel/transformation/transformers/es6/classes.js b/lib/babel/transformation/transformers/es6/classes.js index 6d1705bffa..20df75660c 100644 --- a/lib/babel/transformation/transformers/es6/classes.js +++ b/lib/babel/transformation/transformers/es6/classes.js @@ -83,7 +83,14 @@ ClassTransformer.prototype.run = function () { constructor = t.functionDeclaration(className, [], constructorBody); body.push(constructor); } else { - constructor = t.functionExpression(null, [], constructorBody); + var constructorName = null; + // when there is only constructor or nothing, constructor is not + // wrapped in closure and has to be named + if (this.node.body.body.length <= 1) { + constructorName = className; + } + + constructor = t.functionExpression(constructorName, [], constructorBody); body.push(t.variableDeclaration("var", [ t.variableDeclarator(className, constructor) ])); From 9621d1bbeb8932290654e1d94cd4077739caa771 Mon Sep 17 00:00:00 2001 From: neVERberleRfellerER Date: Mon, 23 Feb 2015 17:39:42 +0100 Subject: [PATCH 2/4] remove unnecessary derived class constructor name --- lib/babel/transformation/transformers/es6/classes.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/babel/transformation/transformers/es6/classes.js b/lib/babel/transformation/transformers/es6/classes.js index 20df75660c..8ca8786924 100644 --- a/lib/babel/transformation/transformers/es6/classes.js +++ b/lib/babel/transformation/transformers/es6/classes.js @@ -84,9 +84,9 @@ ClassTransformer.prototype.run = function () { body.push(constructor); } else { var constructorName = null; - // when there is only constructor or nothing, constructor is not - // wrapped in closure and has to be named - if (this.node.body.body.length <= 1) { + // when class has no parent and there is only constructor or nothing then + // constructor is not wrapped in closure and has to be named + if (!this.hasSuper && this.node.body.body.length <= 1) { constructorName = className; } From b7c297bb89c255ef055a502b79c1df4946415b29 Mon Sep 17 00:00:00 2001 From: Ondrej Kraus Date: Tue, 24 Feb 2015 00:20:45 +0100 Subject: [PATCH 3/4] remove anonymous class constructor name in specific case Name of anonymous class constructor is not needed when it contains exactly one method that is not construtor, because constructor will be assigned to variable in closure. --- lib/babel/transformation/transformers/es6/classes.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/babel/transformation/transformers/es6/classes.js b/lib/babel/transformation/transformers/es6/classes.js index 8ca8786924..c82d8566a0 100644 --- a/lib/babel/transformation/transformers/es6/classes.js +++ b/lib/babel/transformation/transformers/es6/classes.js @@ -65,6 +65,7 @@ function ClassTransformer(node, file, scope, isStatement) { ClassTransformer.prototype.run = function () { var superName = this.superName; var className = this.className; + var classBody = this.node.body.body; var file = this.file; // @@ -86,7 +87,10 @@ ClassTransformer.prototype.run = function () { var constructorName = null; // when class has no parent and there is only constructor or nothing then // constructor is not wrapped in closure and has to be named - if (!this.hasSuper && this.node.body.body.length <= 1) { + var containsOnlyConstructor = classBody.length === 1 && + classBody[0].key.name === "constructor"; + if (!this.hasSuper && (classBody.length === 0 || + containsOnlyConstructor)) { constructorName = className; } From 811a843be9bbe311879bc0d2ab234be38d5c5de1 Mon Sep 17 00:00:00 2001 From: Ondrej Kraus Date: Tue, 24 Feb 2015 00:42:08 +0100 Subject: [PATCH 4/4] add tests for extends of anonymous classes --- .../super-class-anonymous/actual.js | 20 +++ .../super-class-anonymous/expected.js | 129 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 test/fixtures/transformation/es6-classes/super-class-anonymous/actual.js create mode 100644 test/fixtures/transformation/es6-classes/super-class-anonymous/expected.js diff --git a/test/fixtures/transformation/es6-classes/super-class-anonymous/actual.js b/test/fixtures/transformation/es6-classes/super-class-anonymous/actual.js new file mode 100644 index 0000000000..bacf04fbc4 --- /dev/null +++ b/test/fixtures/transformation/es6-classes/super-class-anonymous/actual.js @@ -0,0 +1,20 @@ +class TestEmpty extends (class {}) { +} + +class TestConstructorOnly extends (class { constructor() {} }) { +} + +class TestMethodOnly extends (class { method() {} }) { +} + +class TestConstructorAndMethod extends (class { + constructor() {} + method() {} +}) { +} + +class TestMultipleMethods extends (class { + m1() {} + m2() {} +}) { +} diff --git a/test/fixtures/transformation/es6-classes/super-class-anonymous/expected.js b/test/fixtures/transformation/es6-classes/super-class-anonymous/expected.js new file mode 100644 index 0000000000..91faeccc4b --- /dev/null +++ b/test/fixtures/transformation/es6-classes/super-class-anonymous/expected.js @@ -0,0 +1,129 @@ +"use strict"; + +var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; + +var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; + +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + +var TestEmpty = (function (_ref) { + function TestEmpty() { + _classCallCheck(this, TestEmpty); + + if (_ref != null) { + _ref.apply(this, arguments); + } + } + + _inherits(TestEmpty, _ref); + + return TestEmpty; +})(function _class() { + _classCallCheck(this, _class); +}); + +var TestConstructorOnly = (function (_ref2) { + function TestConstructorOnly() { + _classCallCheck(this, TestConstructorOnly); + + if (_ref2 != null) { + _ref2.apply(this, arguments); + } + } + + _inherits(TestConstructorOnly, _ref2); + + return TestConstructorOnly; +})(function _class2() { + _classCallCheck(this, _class2); +}); + +var TestMethodOnly = (function (_ref3) { + function TestMethodOnly() { + _classCallCheck(this, TestMethodOnly); + + if (_ref3 != null) { + _ref3.apply(this, arguments); + } + } + + _inherits(TestMethodOnly, _ref3); + + return TestMethodOnly; +})((function () { + var _class3 = function () { + _classCallCheck(this, _class3); + }; + + _prototypeProperties(_class3, null, { + method: { + value: function method() {}, + writable: true, + configurable: true + } + }); + + return _class3; +})()); + +var TestConstructorAndMethod = (function (_ref4) { + function TestConstructorAndMethod() { + _classCallCheck(this, TestConstructorAndMethod); + + if (_ref4 != null) { + _ref4.apply(this, arguments); + } + } + + _inherits(TestConstructorAndMethod, _ref4); + + return TestConstructorAndMethod; +})((function () { + var _class4 = function () { + _classCallCheck(this, _class4); + }; + + _prototypeProperties(_class4, null, { + method: { + value: function method() {}, + writable: true, + configurable: true + } + }); + + return _class4; +})()); + +var TestMultipleMethods = (function (_ref5) { + function TestMultipleMethods() { + _classCallCheck(this, TestMultipleMethods); + + if (_ref5 != null) { + _ref5.apply(this, arguments); + } + } + + _inherits(TestMultipleMethods, _ref5); + + return TestMultipleMethods; +})((function () { + var _class5 = function () { + _classCallCheck(this, _class5); + }; + + _prototypeProperties(_class5, null, { + m1: { + value: function m1() {}, + writable: true, + configurable: true + }, + m2: { + value: function m2() {}, + writable: true, + configurable: true + } + }); + + return _class5; +})()); +