From 13a52dd300b7b0f5267f40636fb0368bf2842242 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 10 Jan 2015 22:18:30 +1100 Subject: [PATCH] always use an IIFE for classes - fixes #435 --- .../transformers/es6-classes.js | 71 +++++++------------ .../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 +++--- .../object-getter-memoization/expected.js | 26 ++++--- .../source-maps/class/expected.js | 22 +++--- .../source-maps/class/source-mappings.json | 4 +- 9 files changed, 118 insertions(+), 111 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 0d3ef1660b..ffa8f0e47f 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -3,36 +3,11 @@ var util = require("../../util"); var t = require("../../types"); exports.ClassDeclaration = function (node, parent, file, scope) { - var closure = true; - if (t.isProgram(parent) || t.isBlockStatement(parent)) { - closure = false; - } - - var factory = new Class(node, file, scope, closure); - var newNode = factory.run(); - if (factory.closure) { - if (closure) { - // declaration in an expression context... - // export default class Foo {} - scope.push({ - kind: "var", - key: node.id.key, - id: node.id - }); - return t.assignmentExpression("=", node.id, newNode); - } else { - // has a super class or PrivateDeclaration etc - return t.variableDeclaration("let", [ - t.variableDeclarator(node.id, newNode) - ]); - } - } else { - return newNode; - } + return new Class(node, file, scope, true).run(); }; exports.ClassExpression = function (node, parent, file, scope) { - return new Class(node, file, scope, true).run(); + return new Class(node, file, scope, false).run(); }; /** @@ -44,11 +19,11 @@ exports.ClassExpression = function (node, parent, file, scope) { * @param {Boolean} closure */ -function Class(node, file, scope, closure) { - this.closure = closure; - 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.hasInstanceMutators = false; this.hasStaticMutators = false; @@ -86,8 +61,6 @@ Class.prototype.run = function () { // if (superName) { - this.closure = true; - // so we're only evaluating it once var superRef = this.scope.generateUidBasedOnNode(superName, this.file); body.unshift(t.variableDeclaration("var", [ @@ -103,19 +76,25 @@ Class.prototype.run = function () { t.inheritsComments(body[0], this.node); - if (this.closure) { - 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)), - [] - ); - } + var init; + + if (body.length === 1) { + // only a constructor so no need for a closure container + init = constructor; } else { - return body; + body.push(t.returnStatement(className)); + init = t.callExpression( + t.functionExpression(null, [], t.blockStatement(body)), + [] + ); + } + + if (this.isStatement) { + return t.variableDeclaration("let", [ + t.variableDeclarator(className, init) + ]); + } else { + return init; } }; 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 391423e078..6d90b4abd4 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,16 +5,20 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = function Test() {}; +var Test = (function () { + var Test = function Test() {}; -_prototypeProperties(Test, null, { - test: { - get: function () { - return 5 + 5; - }, - set: function (val) { - this._test = val; - }, - enumerable: true - } -}); + _prototypeProperties(Test, null, { + test: { + get: function () { + return 5 + 5; + }, + set: function (val) { + this._test = val; + }, + enumerable: true + } + }); + + return Test; +})(); diff --git a/test/fixtures/transformation/es6-classes/instance-getter/expected.js b/test/fixtures/transformation/es6-classes/instance-getter/expected.js index 6fa5d59400..10922e7805 100644 --- a/test/fixtures/transformation/es6-classes/instance-getter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-getter/expected.js @@ -5,13 +5,17 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = function Test() {}; +var Test = (function () { + var Test = function Test() {}; -_prototypeProperties(Test, null, { - test: { - get: function () { - return 5 + 5; - }, - enumerable: true - } -}); + _prototypeProperties(Test, null, { + test: { + get: function () { + return 5 + 5; + }, + enumerable: true + } + }); + + return Test; +})(); diff --git a/test/fixtures/transformation/es6-classes/instance-method/expected.js b/test/fixtures/transformation/es6-classes/instance-method/expected.js index 8e4305baca..bedd164fe5 100644 --- a/test/fixtures/transformation/es6-classes/instance-method/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-method/expected.js @@ -1,7 +1,11 @@ "use strict"; -var Test = function Test() {}; +var Test = (function () { + var Test = function Test() {}; -Test.prototype.test = function () { - return 5 + 5; -}; + Test.prototype.test = function () { + return 5 + 5; + }; + + return Test; +})(); diff --git a/test/fixtures/transformation/es6-classes/instance-setter/expected.js b/test/fixtures/transformation/es6-classes/instance-setter/expected.js index 2ffe02b551..0fb2a379ef 100644 --- a/test/fixtures/transformation/es6-classes/instance-setter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-setter/expected.js @@ -5,13 +5,17 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = function Test() {}; +var Test = (function () { + var Test = function Test() {}; -_prototypeProperties(Test, null, { - test: { - set: function (val) { - this._test = val; - }, - enumerable: true - } -}); + _prototypeProperties(Test, null, { + test: { + set: function (val) { + this._test = val; + }, + enumerable: true + } + }); + + return Test; +})(); diff --git a/test/fixtures/transformation/es6-classes/static/expected.js b/test/fixtures/transformation/es6-classes/static/expected.js index e7d7df4451..59c3cfd171 100644 --- a/test/fixtures/transformation/es6-classes/static/expected.js +++ b/test/fixtures/transformation/es6-classes/static/expected.js @@ -5,14 +5,18 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var A = function A() {}; +var A = (function () { + var A = function A() {}; -A.a = function () {}; + A.a = function () {}; -_prototypeProperties(A, { - b: { - get: function () {}, - set: function (b) {}, - enumerable: true - } -}); + _prototypeProperties(A, { + b: { + get: function () {}, + set: function (b) {}, + enumerable: true + } + }); + + return A; +})(); diff --git a/test/fixtures/transformation/playground/object-getter-memoization/expected.js b/test/fixtures/transformation/playground/object-getter-memoization/expected.js index c94e260493..ac43f9b05d 100644 --- a/test/fixtures/transformation/playground/object-getter-memoization/expected.js +++ b/test/fixtures/transformation/playground/object-getter-memoization/expected.js @@ -14,21 +14,25 @@ var _defineProperty = function (obj, key, value) { }); }; -var Foo = function Foo() {}; +var Foo = (function () { + var Foo = function Foo() {}; -_prototypeProperties(Foo, null, _defineProperty({ - bar: { + _prototypeProperties(Foo, null, _defineProperty({ + bar: { + get: function () { + return _defineProperty(this, "bar", complex()).bar; + }, + enumerable: true + } + }, bar, { get: function () { - return _defineProperty(this, "bar", complex()).bar; + return _defineProperty(this, bar, complex())[bar]; }, enumerable: true - } -}, bar, { - get: function () { - return _defineProperty(this, bar, complex())[bar]; - }, - enumerable: true -})); + })); + + return Foo; +})(); var foo = Object.defineProperties({}, _defineProperty({ bar: { diff --git a/test/fixtures/transformation/source-maps/class/expected.js b/test/fixtures/transformation/source-maps/class/expected.js index af92fdbd5c..43d698e530 100644 --- a/test/fixtures/transformation/source-maps/class/expected.js +++ b/test/fixtures/transformation/source-maps/class/expected.js @@ -5,16 +5,20 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; -var Test = function Test() {}; +var Test = (function () { + var Test = function Test() {}; -_prototypeProperties(Test, null, { - bar: { - get: function () { - throw new Error("wow"); - }, - enumerable: true - } -}); + _prototypeProperties(Test, null, { + bar: { + get: function () { + throw new Error("wow"); + }, + enumerable: true + } + }); + + return Test; +})(); 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 1fe92711b8..8b95905776 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": 13, - "column": 13 + "line": 14, + "column": 15 } }]