From 833a4900dab0d4f8b58f51eb4ea29d4d6d74c4bb Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 13 Dec 2014 17:33:35 +1100 Subject: [PATCH] remove IIFE on class declarations #288 --- lib/6to5/templates/class.js | 7 -- .../transformers/es6-classes.js | 98 +++++++++---------- .../accessing-super-class/expected.js | 46 ++++----- .../accessing-super-properties/expected.js | 14 +-- .../calling-super-properties/expected.js | 20 ++-- .../es6-classes/constructor/expected.js | 12 +-- .../instance-getter-and-setter/expected.js | 28 +++--- .../es6-classes/instance-getter/expected.js | 22 ++--- .../es6-classes/instance-method/expected.js | 12 +-- .../es6-classes/instance-setter/expected.js | 22 ++--- .../es6-classes/static/expected.js | 22 ++--- .../expected.js | 24 ++--- .../expected.js | 12 +-- .../es6-classes/super-class/expected.js | 12 +-- .../es6-unicode-regex/basic/expected.js | 2 +- .../ignore-non-unicode/expected.js | 2 +- .../object-getter-memoization/expected.js | 46 ++++----- .../source-maps/class/expected.js | 22 ++--- .../source-maps/class/source-mappings.json | 4 +- 19 files changed, 180 insertions(+), 247 deletions(-) delete mode 100644 lib/6to5/templates/class.js diff --git a/lib/6to5/templates/class.js b/lib/6to5/templates/class.js deleted file mode 100644 index b644463235..0000000000 --- a/lib/6to5/templates/class.js +++ /dev/null @@ -1,7 +0,0 @@ -(function () { - var CLASS_NAME = function () { - - }; - - return CLASS_NAME; -})() diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index d44d6c6f25..98ac4d59d6 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -4,24 +4,11 @@ var t = require("../../types"); var _ = require("lodash"); exports.ClassDeclaration = function (node, parent, file, scope) { - var built = new Class(node, file, scope).run(); - - var declar = t.variableDeclaration("let", [ - t.variableDeclarator(node.id, built) - ]); - t.inheritsComments(declar, node); - return declar; + return new Class(node, file, scope, true).run(); }; exports.ClassExpression = function (node, parent, file, scope) { - return new Class(node, file, scope).run(); -}; - -var getMemberExpressionObject = function (node) { - while (t.isMemberExpression(node)) { - node = node.object; - } - return node; + return new Class(node, file, scope, false).run(); }; /** @@ -30,12 +17,14 @@ var getMemberExpressionObject = function (node) { * @param {Node} node * @param {File} file * @param {Scope} scope + * @param {Boolean} isStatement */ -function Class(node, file, scope) { - this.scope = scope; - this.node = node; - this.file = file; +function Class(node, file, scope, isStatement) { + this.isStatement = isStatement; + this.scope = scope; + this.node = node; + this.file = file; this.instanceMutatorMap = {}; this.staticMutatorMap = {}; @@ -51,50 +40,59 @@ function Class(node, file, scope) { */ Class.prototype.run = function () { - var superClassArgument = this.superName; - var superClassCallee = this.superName; - var superName = this.superName; - var className = this.className; - var file = this.file; + var superName = this.superName; + var className = this.className; + var file = this.file; - if (superName) { - if (t.isMemberExpression(superName)) { - superClassArgument = superClassCallee = getMemberExpressionObject(superName); - } else if (!t.isIdentifier(superName)) { - superClassArgument = superName; - superClassCallee = superName = file.generateUidIdentifier("ref", this.scope); - } + // + + var body = this.body = []; + + var constructor = t.functionExpression(null, [], t.blockStatement([])); + if (this.node.id) constructor.id = className; + this.constructor = constructor; + + body.push(t.variableDeclaration("let", [ + t.variableDeclarator(className, constructor) + ])); + + // + + if (superName && t.isDynamic(superName)) { + // so we're only evaluating it once + var superRefName = "super"; + if (className) superRefName = className.name + "Super"; + + var superRef = file.generateUidIdentifier(superRefName, this.scope); + body.unshift(t.variableDeclaration("var", [ + t.variableDeclarator(superRef, superName) + ])); + superName = superRef; } this.superName = superName; - var container = util.template("class", { - CLASS_NAME: className - }); - - var block = container.callee.expression.body; - var body = this.body = block.body; - var constructor = this.constructor = body[0].declarations[0].init; - - if (this.node.id) constructor.id = className; - - var returnStatement = body.pop(); + // if (superName) { body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName]))); - - container.arguments.push(superClassArgument); - container.callee.expression.params.push(superClassCallee); } this.buildBody(); - if (body.length === 1) { - // only a constructor so no need for a closure container - return constructor; + if (this.isStatement) { + return body; } else { - body.push(returnStatement); - return container; + if (body.length === 1) { + // only a constructor so no need for a closure container + return constructor; + } else { + body.push(t.returnStatement(className)); + return t.callExpression( + t.functionExpression(null, [], t.blockStatement(body)), + [] + ); + } } }; diff --git a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js index 0c54fbc4e6..ad8532015c 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -13,33 +13,29 @@ var _extends = function (child, parent) { child.__proto__ = parent; }; -var Test = (function (Foo) { - var Test = function Test() { - woops["super"].test(); - Foo.call(this); - Foo.prototype.test.call(this); - foob(Foo); +var Test = function Test() { + woops["super"].test(); + Foo.call(this); + Foo.prototype.test.call(this); + foob(Foo); - Foo.call.apply(Foo, [this].concat(_slice.call(arguments))); - Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments))); + Foo.call.apply(Foo, [this].concat(_slice.call(arguments))); + Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments))); - }; + Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments))); +}; - _extends(Test, Foo); +_extends(Test, Foo); - Test.prototype.test = function () { - Foo.prototype.test.call(this); - Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments))); - }; +Test.prototype.test = function () { + Foo.prototype.test.call(this); + Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments))); +}; - Test.foo = function () { - Foo.foo.call(this); - Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments))); - Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments))); - }; - - return Test; -})(Foo); +Test.foo = function () { + Foo.foo.call(this); + Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments))); + Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments))); +}; diff --git a/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js index dec479103b..5ffb26c5c6 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js @@ -12,13 +12,9 @@ var _extends = function (child, parent) { child.__proto__ = parent; }; -var Test = (function (Foo) { - var Test = function Test() { - Foo.prototype.test; - Foo.prototype.test.whatever; - }; +var Test = function Test() { + Foo.prototype.test; + Foo.prototype.test.whatever; +}; - _extends(Test, Foo); - - return Test; -})(Foo); +_extends(Test, Foo); diff --git a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js index 28aa2cb0ea..8387489528 100644 --- a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js @@ -12,17 +12,13 @@ var _extends = function (child, parent) { child.__proto__ = parent; }; -var Test = (function (Foo) { - var Test = function Test() { - Foo.prototype.test.whatever(); - Foo.prototype.test.call(this); - }; +var Test = function Test() { + Foo.prototype.test.whatever(); + Foo.prototype.test.call(this); +}; - _extends(Test, Foo); +_extends(Test, Foo); - Test.test = function () { - return Foo.wow.call(this); - }; - - return Test; -})(Foo); +Test.test = function () { + return Foo.wow.call(this); +}; diff --git a/test/fixtures/transformation/es6-classes/constructor/expected.js b/test/fixtures/transformation/es6-classes/constructor/expected.js index 200e36b4c5..47eb5a927e 100644 --- a/test/fixtures/transformation/es6-classes/constructor/expected.js +++ b/test/fixtures/transformation/es6-classes/constructor/expected.js @@ -16,12 +16,8 @@ var Test = function Test() { this.state = "test"; }; -var Foo = (function (Bar) { - var Foo = function Foo() { - this.state = "test"; - }; +var Foo = function Foo() { + this.state = "test"; +}; - _extends(Foo, Bar); - - return Foo; -})(Bar); +_extends(Foo, Bar); diff --git a/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js b/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js index 1ca4e4bc91..fed43faa63 100644 --- a/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js @@ -5,20 +5,16 @@ var _classProps = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = (function () { - var Test = function Test() {}; +var Test = function Test() {}; - _classProps(Test, null, { - test: { - get: function () { - return 5 + 5; - }, - set: function (val) { - this._test = val; - }, - enumerable: true - } - }); - - return Test; -})(); +_classProps(Test, null, { + test: { + get: function () { + return 5 + 5; + }, + set: function (val) { + this._test = val; + }, + enumerable: true + } +}); diff --git a/test/fixtures/transformation/es6-classes/instance-getter/expected.js b/test/fixtures/transformation/es6-classes/instance-getter/expected.js index b980afd00a..4b52e5a6e2 100644 --- a/test/fixtures/transformation/es6-classes/instance-getter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-getter/expected.js @@ -5,17 +5,13 @@ var _classProps = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = (function () { - var Test = function Test() {}; +var Test = function Test() {}; - _classProps(Test, null, { - test: { - get: function () { - return 5 + 5; - }, - enumerable: true - } - }); - - return Test; -})(); +_classProps(Test, null, { + test: { + get: function () { + return 5 + 5; + }, + enumerable: true + } +}); diff --git a/test/fixtures/transformation/es6-classes/instance-method/expected.js b/test/fixtures/transformation/es6-classes/instance-method/expected.js index bedd164fe5..8e4305baca 100644 --- a/test/fixtures/transformation/es6-classes/instance-method/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-method/expected.js @@ -1,11 +1,7 @@ "use strict"; -var Test = (function () { - var Test = function Test() {}; +var Test = function Test() {}; - Test.prototype.test = function () { - return 5 + 5; - }; - - return Test; -})(); +Test.prototype.test = function () { + return 5 + 5; +}; diff --git a/test/fixtures/transformation/es6-classes/instance-setter/expected.js b/test/fixtures/transformation/es6-classes/instance-setter/expected.js index 5a181cc25c..0f47c5db71 100644 --- a/test/fixtures/transformation/es6-classes/instance-setter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-setter/expected.js @@ -5,17 +5,13 @@ var _classProps = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = (function () { - var Test = function Test() {}; +var Test = function Test() {}; - _classProps(Test, null, { - test: { - set: function (val) { - this._test = val; - }, - enumerable: true - } - }); - - return Test; -})(); +_classProps(Test, null, { + test: { + set: function (val) { + this._test = val; + }, + enumerable: true + } +}); diff --git a/test/fixtures/transformation/es6-classes/static/expected.js b/test/fixtures/transformation/es6-classes/static/expected.js index 26ef9d30f1..5898b4e35d 100644 --- a/test/fixtures/transformation/es6-classes/static/expected.js +++ b/test/fixtures/transformation/es6-classes/static/expected.js @@ -5,18 +5,14 @@ var _classProps = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var A = (function () { - var A = function A() {}; +var A = function A() {}; - A.a = function () {}; +A.a = function () {}; - _classProps(A, { - b: { - get: function () {}, - set: function (b) {}, - enumerable: true - } - }); - - return A; -})(); +_classProps(A, { + b: { + get: function () {}, + set: function (b) {}, + enumerable: true + } +}); diff --git a/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js b/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js index bce35e1528..5108133980 100644 --- a/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js @@ -12,22 +12,14 @@ var _extends = function (child, parent) { child.__proto__ = parent; }; -var BaseController = (function (Chaplin) { - var BaseController = function BaseController() { - Chaplin.Controller.apply(this, arguments); - }; +var BaseController = function BaseController() { + Chaplin.Controller.apply(this, arguments); +}; - _extends(BaseController, Chaplin.Controller); +_extends(BaseController, Chaplin.Controller); - return BaseController; -})(Chaplin); +var BaseController2 = function BaseController2() { + Chaplin.Controller.Another.apply(this, arguments); +}; -var BaseController2 = (function (Chaplin) { - var BaseController2 = function BaseController2() { - Chaplin.Controller.Another.apply(this, arguments); - }; - - _extends(BaseController2, Chaplin.Controller.Another); - - return BaseController2; -})(Chaplin); +_extends(BaseController2, Chaplin.Controller.Another); diff --git a/test/fixtures/transformation/es6-classes/super-class-id-non-identifiers/expected.js b/test/fixtures/transformation/es6-classes/super-class-id-non-identifiers/expected.js index 353a8c289b..aa3f6fdfc2 100644 --- a/test/fixtures/transformation/es6-classes/super-class-id-non-identifiers/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class-id-non-identifiers/expected.js @@ -12,12 +12,10 @@ var _extends = function (child, parent) { child.__proto__ = parent; }; -var Q = (function (_ref) { - var Q = function Q() { - _ref.apply(this, arguments); - }; +var _QSuper = function () {}; - _extends(Q, _ref); +var Q = function Q() { + _QSuper.apply(this, arguments); +}; - return Q; -})(function () {}); +_extends(Q, _QSuper); diff --git a/test/fixtures/transformation/es6-classes/super-class/expected.js b/test/fixtures/transformation/es6-classes/super-class/expected.js index c8c1fb516f..82b40d6a4a 100644 --- a/test/fixtures/transformation/es6-classes/super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class/expected.js @@ -12,12 +12,8 @@ var _extends = function (child, parent) { child.__proto__ = parent; }; -var Test = (function (Foo) { - var Test = function Test() { - Foo.apply(this, arguments); - }; +var Test = function Test() { + Foo.apply(this, arguments); +}; - _extends(Test, Foo); - - return Test; -})(Foo); +_extends(Test, Foo); diff --git a/test/fixtures/transformation/es6-unicode-regex/basic/expected.js b/test/fixtures/transformation/es6-unicode-regex/basic/expected.js index d4f30f3911..eb24a69373 100644 --- a/test/fixtures/transformation/es6-unicode-regex/basic/expected.js +++ b/test/fixtures/transformation/es6-unicode-regex/basic/expected.js @@ -1,4 +1,4 @@ "use strict"; -var string = "foo\ud83d\udca9bar"; +var string = "foo💩bar"; var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF]))bar/); diff --git a/test/fixtures/transformation/es6-unicode-regex/ignore-non-unicode/expected.js b/test/fixtures/transformation/es6-unicode-regex/ignore-non-unicode/expected.js index ab57a7f88e..2a77fc0b65 100644 --- a/test/fixtures/transformation/es6-unicode-regex/ignore-non-unicode/expected.js +++ b/test/fixtures/transformation/es6-unicode-regex/ignore-non-unicode/expected.js @@ -1,4 +1,4 @@ "use strict"; -var string = "foo\ud83d\udca9bar"; +var string = "foo💩bar"; var match = string.match(/foo(.)bar/); diff --git a/test/fixtures/transformation/playground/object-getter-memoization/expected.js b/test/fixtures/transformation/playground/object-getter-memoization/expected.js index c8886b8b37..68a1265fd0 100644 --- a/test/fixtures/transformation/playground/object-getter-memoization/expected.js +++ b/test/fixtures/transformation/playground/object-getter-memoization/expected.js @@ -5,32 +5,28 @@ var _classProps = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Foo = (function () { - var Foo = function Foo() {}; +var Foo = function Foo() {}; - _classProps(Foo, null, (function (_ref) { - _ref[bar] = { - get: function () { - if (this._memoDone) return this._memo; - this._memoDone = true; - return this._memo = complex(); - }, - enumerable: true - }; - return _ref; - })({ - bar: { - get: function () { - if (this._barDone) return this._bar; - this._barDone = true; - return this._bar = complex(); - }, - enumerable: true - } - })); - - return Foo; -})(); +_classProps(Foo, null, (function (_ref) { + _ref[bar] = { + get: function () { + if (this._memoDone) return this._memo; + this._memoDone = true; + return this._memo = complex(); + }, + enumerable: true + }; + return _ref; +})({ + bar: { + get: function () { + if (this._barDone) return this._bar; + this._barDone = true; + return this._bar = complex(); + }, + enumerable: true + } +})); var foo = (function (_foo) { _foo[bar] = function () { diff --git a/test/fixtures/transformation/source-maps/class/expected.js b/test/fixtures/transformation/source-maps/class/expected.js index aa5136a46f..8d4a322398 100644 --- a/test/fixtures/transformation/source-maps/class/expected.js +++ b/test/fixtures/transformation/source-maps/class/expected.js @@ -5,20 +5,16 @@ var _classProps = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = (function () { - var Test = function Test() {}; +var Test = function Test() {}; - _classProps(Test, null, { - bar: { - get: function () { - throw new Error("wow"); - }, - enumerable: true - } - }); - - return Test; -})(); +_classProps(Test, null, { + bar: { + get: function () { + throw new Error("wow"); + }, + enumerable: true + } +}); var test = new Test(); test.bar; diff --git a/test/fixtures/transformation/source-maps/class/source-mappings.json b/test/fixtures/transformation/source-maps/class/source-mappings.json index 8b95905776..1fe92711b8 100644 --- a/test/fixtures/transformation/source-maps/class/source-mappings.json +++ b/test/fixtures/transformation/source-maps/class/source-mappings.json @@ -4,7 +4,7 @@ "column": 10 }, "generated": { - "line": 14, - "column": 15 + "line": 13, + "column": 13 } }]