From 2b95b876e1916d49f143562e8f70bb706ec67176 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 7 Mar 2015 01:50:34 +1100 Subject: [PATCH] return only constructor if class was a named class with only a constructor/no constructor --- src/babel/transformation/transformers/es6/classes.js | 7 +++++-- .../transformation/api/blacklist/expected.js | 12 ++++-------- .../super-function-fallback/expected.js | 12 ++++-------- .../es6-classes/constructor/expected.js | 12 ++++-------- .../es6-classes/plain-class/expected.js | 10 +++------- .../transformation/es6-classes/statement/expected.js | 12 ++++-------- .../es6-classes/super-function-fallback/expected.js | 12 ++++-------- .../es6-modules-amd/exports-default/expected.js | 10 +++------- .../es6-modules-amd/exports-variable/expected.js | 10 +++------- .../es6-modules-common/exports-default/expected.js | 10 +++------- .../es6-modules-common/exports-variable/expected.js | 10 +++------- .../es6-modules-ignore/exports-default/expected.js | 10 +++------- .../es6-modules-ignore/exports-variable/expected.js | 10 +++------- .../es6-modules-system/exports-default/expected.js | 10 +++------- .../es6-modules-system/exports-variable/expected.js | 10 +++------- .../es6-modules-umd/exports-default/expected.js | 10 +++------- .../es6-modules-umd/exports-variable/expected.js | 10 +++------- 17 files changed, 58 insertions(+), 119 deletions(-) diff --git a/src/babel/transformation/transformers/es6/classes.js b/src/babel/transformation/transformers/es6/classes.js index 809cb2919a..3e6b119e73 100644 --- a/src/babel/transformation/transformers/es6/classes.js +++ b/src/babel/transformation/transformers/es6/classes.js @@ -143,9 +143,12 @@ class ClassTransformer { this.buildBody(); - // infer class name if this is a nameless class expression - if (!this.className) { + if (this.className) { + // named class with only a constructor + if (body.length === 1) return t.toExpression(body[0]); + } else { + // infer class name if this is a nameless class expression constructor = nameMethod.bare(constructor, this.parent, this.scope); body.unshift(t.variableDeclaration("var", [ diff --git a/test/fixtures/transformation/api/blacklist/expected.js b/test/fixtures/transformation/api/blacklist/expected.js index a11557b23d..934fea14ff 100644 --- a/test/fixtures/transformation/api/blacklist/expected.js +++ b/test/fixtures/transformation/api/blacklist/expected.js @@ -2,12 +2,8 @@ var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; -var Test = (function () { - function Test() { - _classCallCheck(this, Test); +var Test = function Test() { + _classCallCheck(this, Test); - arr.map(x => x * x); - } - - return Test; -})(); \ No newline at end of file + arr.map(x => x * x); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js b/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js index b0a7176043..4366f3918d 100644 --- a/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js @@ -1,11 +1,7 @@ "use strict"; -var Test = (function () { - function Test() { - babelHelpers.classCallCheck(this, Test); +var Test = function Test() { + babelHelpers.classCallCheck(this, Test); - Function.prototype.hasOwnProperty.call(this, "test"); - } - - return Test; -})(); \ No newline at end of file + Function.prototype.hasOwnProperty.call(this, "test"); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-classes/constructor/expected.js b/test/fixtures/transformation/es6-classes/constructor/expected.js index 8708ba08f5..7169ce082f 100644 --- a/test/fixtures/transformation/es6-classes/constructor/expected.js +++ b/test/fixtures/transformation/es6-classes/constructor/expected.js @@ -1,14 +1,10 @@ "use strict"; -var Test = (function () { - function Test() { - babelHelpers.classCallCheck(this, Test); +var Test = function Test() { + babelHelpers.classCallCheck(this, Test); - this.state = "test"; - } - - return Test; -})(); + this.state = "test"; +}; var Foo = (function (_Bar) { function Foo() { diff --git a/test/fixtures/transformation/es6-classes/plain-class/expected.js b/test/fixtures/transformation/es6-classes/plain-class/expected.js index e4c4a5a0be..73c98b9f71 100644 --- a/test/fixtures/transformation/es6-classes/plain-class/expected.js +++ b/test/fixtures/transformation/es6-classes/plain-class/expected.js @@ -1,9 +1,5 @@ "use strict"; -var Test = (function () { - function Test() { - babelHelpers.classCallCheck(this, Test); - } - - return Test; -})(); \ No newline at end of file +var Test = function Test() { + babelHelpers.classCallCheck(this, Test); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-classes/statement/expected.js b/test/fixtures/transformation/es6-classes/statement/expected.js index 701427f6f2..9c68a820dd 100644 --- a/test/fixtures/transformation/es6-classes/statement/expected.js +++ b/test/fixtures/transformation/es6-classes/statement/expected.js @@ -1,14 +1,10 @@ "use strict"; -var BaseView = (function () { - function BaseView() { - babelHelpers.classCallCheck(this, BaseView); +var BaseView = function BaseView() { + babelHelpers.classCallCheck(this, BaseView); - this.autoRender = true; - } - - return BaseView; -})(); + this.autoRender = true; +}; var BaseView = (function () { var _class = function BaseView() { diff --git a/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js b/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js index 2a9de371bf..9d4d8335e0 100644 --- a/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js +++ b/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js @@ -1,11 +1,7 @@ "use strict"; -var Test = (function () { - function Test() { - babelHelpers.classCallCheck(this, Test); +var Test = function Test() { + babelHelpers.classCallCheck(this, Test); - babelHelpers.get(Object.getPrototypeOf(Test.prototype), "hasOwnProperty", this).call(this, "test"); - } - - return Test; -})(); \ No newline at end of file + babelHelpers.get(Object.getPrototypeOf(Test.prototype), "hasOwnProperty", this).call(this, "test"); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js index 0ab9bf338d..36bb526143 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js @@ -23,13 +23,9 @@ define(["exports", "module"], function (exports, module) { function foo() {} - var Foo = (function () { - function Foo() { - _classCallCheck(this, Foo); - } - - return Foo; - })(); + var Foo = function Foo() { + _classCallCheck(this, Foo); + }; module.exports = Foo; }); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js index 85ae9deed6..758bedb23a 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js @@ -15,13 +15,9 @@ define(["exports"], function (exports) { function foo7() {} - var foo8 = exports.foo8 = (function () { - function foo8() { - _classCallCheck(this, foo8); - } - - return foo8; - })(); + var foo8 = exports.foo8 = function foo8() { + _classCallCheck(this, foo8); + }; Object.defineProperty(exports, "__esModule", { value: true diff --git a/test/fixtures/transformation/es6-modules-common/exports-default/expected.js b/test/fixtures/transformation/es6-modules-common/exports-default/expected.js index 68f637c10f..edb55396c5 100644 --- a/test/fixtures/transformation/es6-modules-common/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-common/exports-default/expected.js @@ -22,12 +22,8 @@ module.exports = _default; function foo() {} -var Foo = (function () { - function Foo() { - _classCallCheck(this, Foo); - } - - return Foo; -})(); +var Foo = function Foo() { + _classCallCheck(this, Foo); +}; module.exports = Foo; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js index c14a100184..d2cb09cbef 100644 --- a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js @@ -14,13 +14,9 @@ var foo6 = exports.foo6 = 3; function foo7() {} -var foo8 = exports.foo8 = (function () { - function foo8() { - _classCallCheck(this, foo8); - } - - return foo8; -})(); +var foo8 = exports.foo8 = function foo8() { + _classCallCheck(this, foo8); +}; Object.defineProperty(exports, "__esModule", { value: true diff --git a/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js b/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js index b5c2cae6a3..2c09a5b8b6 100644 --- a/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js @@ -12,10 +12,6 @@ var _default = (function () { function foo() {} -var Foo = (function () { - function Foo() { - _classCallCheck(this, Foo); - } - - return Foo; -})(); \ No newline at end of file +var Foo = function Foo() { + _classCallCheck(this, Foo); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js index 152e99fbb9..1a08078682 100644 --- a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js @@ -13,10 +13,6 @@ var foo6 = 3; function foo7() {} -var foo8 = (function () { - function foo8() { - _classCallCheck(this, foo8); - } - - return foo8; -})(); \ No newline at end of file +var foo8 = function foo8() { + _classCallCheck(this, foo8); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-system/exports-default/expected.js b/test/fixtures/transformation/es6-modules-system/exports-default/expected.js index 51d4dfb8b3..076677950a 100644 --- a/test/fixtures/transformation/es6-modules-system/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-system/exports-default/expected.js @@ -32,13 +32,9 @@ System.register([], function (_export) { _export("default", _default); - Foo = (function () { - function Foo() { - _classCallCheck(this, Foo); - } - - return Foo; - })(); + Foo = function Foo() { + _classCallCheck(this, Foo); + }; _export("default", Foo); } diff --git a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js index 3e7b734e75..c94cf23409 100644 --- a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js @@ -18,13 +18,9 @@ System.register([], function (_export) { foo4 = _export("foo4", 2); foo5 = _export("foo5", undefined); foo6 = _export("foo6", 3); - foo8 = _export("foo8", (function () { - function foo8() { - _classCallCheck(this, foo8); - } - - return foo8; - })()); + foo8 = _export("foo8", function foo8() { + _classCallCheck(this, foo8); + }); _export("foo3", foo3 = 5); } diff --git a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js index 3cabb62475..72cf92c9b3 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js @@ -29,13 +29,9 @@ function foo() {} - var Foo = (function () { - function Foo() { - _classCallCheck(this, Foo); - } - - return Foo; - })(); + var Foo = function Foo() { + _classCallCheck(this, Foo); + }; module.exports = Foo; }); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js index 4c10702040..f162e35935 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js @@ -21,13 +21,9 @@ function foo7() {} - var foo8 = exports.foo8 = (function () { - function foo8() { - _classCallCheck(this, foo8); - } - - return foo8; - })(); + var foo8 = exports.foo8 = function foo8() { + _classCallCheck(this, foo8); + }; Object.defineProperty(exports, "__esModule", { value: true