From 545c8c3adb2ffafa584fb338953ea3054258ba66 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 12 Jan 2015 11:44:23 +1100 Subject: [PATCH] define class methods instead of assigning them - fixes #454 --- .../transformers/es6-classes.js | 29 +++++-------- .../accessing-super-class/expected.js | 43 +++++++++++++------ .../calling-super-properties/expected.js | 18 ++++++-- .../es6-classes/instance-method/expected.js | 18 ++++++-- .../es6-classes/statement/expected.js | 18 ++++++-- .../es6-classes/static/expected.js | 8 +++- .../accessing-super-class/expected.js | 43 +++++++++++++------ .../calling-super-properties/expected.js | 18 ++++++-- 8 files changed, 136 insertions(+), 59 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index fd824f86c7..2595952d5f 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -170,28 +170,19 @@ Class.prototype.pushMethod = function (node) { var methodName = node.key; var kind = node.kind; + var mutatorMap = this.instanceMutatorMap; + if (node.static) { + this.hasStaticMutators = true; + mutatorMap = this.staticMutatorMap; + } else { + this.hasInstanceMutators = true; + } if (kind === "") { - // method - - var className = this.className; - if (!node.static) className = t.memberExpression(className, t.identifier("prototype")); - methodName = t.memberExpression(className, methodName, node.computed); - - var expr = t.expressionStatement(t.assignmentExpression("=", methodName, node.value)); - t.inheritsComments(expr, node); - this.body.push(expr); - } else { - // mutator - var mutatorMap = this.instanceMutatorMap; - if (node.static) { - this.hasStaticMutators = true; - mutatorMap = this.staticMutatorMap; - } else { - this.hasInstanceMutators = true; - } - util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node); + kind = "value"; } + + util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node); }; /** 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 c556d7c015..ba500fb940 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -1,6 +1,11 @@ "use strict"; var _slice = Array.prototype.slice; +var _prototypeProperties = function (child, staticProps, instanceProps) { + if (staticProps) Object.defineProperties(child, staticProps); + if (instanceProps) Object.defineProperties(child.prototype, instanceProps); +}; + var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); @@ -54,19 +59,31 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.prototype.test = function () { - var _get6, _get7; - _get(Object.getPrototypeOf(Test.prototype), "test", this).call(this); - (_get6 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get6, [this].concat(_slice.call(arguments))); - (_get7 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get7, [this, "test"].concat(_slice.call(arguments))); - }; - - Test.foo = function () { - var _get8, _get9; - _get(Object.getPrototypeOf(Test), "foo", this).call(this); - (_get8 = _get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_get8, [this].concat(_slice.call(arguments))); - (_get9 = _get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_get9, [this, "test"].concat(_slice.call(arguments))); - }; + _prototypeProperties(Test, { + foo: { + value: function () { + var _get6, _get7; + _get(Object.getPrototypeOf(Test), "foo", this).call(this); + (_get6 = _get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_get6, [this].concat(_slice.call(arguments))); + (_get7 = _get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_get7, [this, "test"].concat(_slice.call(arguments))); + }, + writable: true, + enumerable: true, + configurable: true + } + }, { + test: { + value: function () { + var _get8, _get9; + _get(Object.getPrototypeOf(Test.prototype), "test", this).call(this); + (_get8 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get8, [this].concat(_slice.call(arguments))); + (_get9 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get9, [this, "test"].concat(_slice.call(arguments))); + }, + writable: true, + enumerable: true, + configurable: true + } + }); return 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 ce7a061321..b388800218 100644 --- a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js @@ -1,5 +1,10 @@ "use strict"; +var _prototypeProperties = function (child, staticProps, instanceProps) { + if (staticProps) Object.defineProperties(child, staticProps); + if (instanceProps) Object.defineProperties(child.prototype, instanceProps); +}; + var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); @@ -45,9 +50,16 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.test = function () { - return _get(Object.getPrototypeOf(Test), "wow", this).call(this); - }; + _prototypeProperties(Test, { + test: { + value: function () { + return _get(Object.getPrototypeOf(Test), "wow", this).call(this); + }, + writable: true, + enumerable: true, + configurable: true + } + }); return Test; })(Foo); diff --git a/test/fixtures/transformation/es6-classes/instance-method/expected.js b/test/fixtures/transformation/es6-classes/instance-method/expected.js index bedd164fe5..a65c93b1a6 100644 --- a/test/fixtures/transformation/es6-classes/instance-method/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-method/expected.js @@ -1,11 +1,23 @@ "use strict"; +var _prototypeProperties = function (child, staticProps, instanceProps) { + if (staticProps) Object.defineProperties(child, staticProps); + if (instanceProps) Object.defineProperties(child.prototype, instanceProps); +}; + var Test = (function () { var Test = function Test() {}; - Test.prototype.test = function () { - return 5 + 5; - }; + _prototypeProperties(Test, null, { + test: { + value: function () { + return 5 + 5; + }, + writable: true, + enumerable: true, + configurable: true + } + }); return Test; })(); diff --git a/test/fixtures/transformation/es6-classes/statement/expected.js b/test/fixtures/transformation/es6-classes/statement/expected.js index 7b2a8a13d1..1a10318ee2 100644 --- a/test/fixtures/transformation/es6-classes/statement/expected.js +++ b/test/fixtures/transformation/es6-classes/statement/expected.js @@ -1,5 +1,10 @@ "use strict"; +var _prototypeProperties = function (child, staticProps, instanceProps) { + if (staticProps) Object.defineProperties(child, staticProps); + if (instanceProps) Object.defineProperties(child.prototype, instanceProps); +}; + var BaseView = function BaseView() { this.autoRender = true; }; @@ -11,9 +16,16 @@ var BaseView = function () { var BaseView = (function () { var _class2 = function () {}; - _class2.prototype.foo = function () { - this.autoRender = true; - }; + _prototypeProperties(_class2, null, { + foo: { + value: function () { + this.autoRender = true; + }, + writable: true, + enumerable: true, + configurable: true + } + }); return _class2; })(); diff --git a/test/fixtures/transformation/es6-classes/static/expected.js b/test/fixtures/transformation/es6-classes/static/expected.js index 6f1deec853..897d8cbd3b 100644 --- a/test/fixtures/transformation/es6-classes/static/expected.js +++ b/test/fixtures/transformation/es6-classes/static/expected.js @@ -8,9 +8,13 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { var A = (function () { var A = function A() {}; - A.a = function () {}; - _prototypeProperties(A, { + a: { + value: function () {}, + writable: true, + enumerable: true, + configurable: true + }, b: { get: function () {}, set: function (b) {}, diff --git a/test/fixtures/transformation/optional-classes-fast-super/accessing-super-class/expected.js b/test/fixtures/transformation/optional-classes-fast-super/accessing-super-class/expected.js index 63b24b422e..821e2b4f02 100644 --- a/test/fixtures/transformation/optional-classes-fast-super/accessing-super-class/expected.js +++ b/test/fixtures/transformation/optional-classes-fast-super/accessing-super-class/expected.js @@ -1,6 +1,11 @@ "use strict"; var _slice = Array.prototype.slice; +var _prototypeProperties = function (child, staticProps, instanceProps) { + if (staticProps) Object.defineProperties(child, staticProps); + if (instanceProps) Object.defineProperties(child.prototype, instanceProps); +}; + var _inherits = function (child, parent) { if (typeof parent !== "function" && parent !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof parent); @@ -32,19 +37,31 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.prototype.test = function () { - var _Foo$prototype$test3, _Foo$prototype$test4; - Foo.prototype.test.call(this); - (_Foo$prototype$test3 = Foo.prototype.test).call.apply(_Foo$prototype$test3, [this].concat(_slice.call(arguments))); - (_Foo$prototype$test4 = Foo.prototype.test).call.apply(_Foo$prototype$test4, [this, "test"].concat(_slice.call(arguments))); - }; - - Test.foo = function () { - var _Foo$foo, _Foo$foo2; - Foo.foo.call(this); - (_Foo$foo = Foo.foo).call.apply(_Foo$foo, [this].concat(_slice.call(arguments))); - (_Foo$foo2 = Foo.foo).call.apply(_Foo$foo2, [this, "test"].concat(_slice.call(arguments))); - }; + _prototypeProperties(Test, { + foo: { + value: function () { + var _Foo$foo, _Foo$foo2; + Foo.foo.call(this); + (_Foo$foo = Foo.foo).call.apply(_Foo$foo, [this].concat(_slice.call(arguments))); + (_Foo$foo2 = Foo.foo).call.apply(_Foo$foo2, [this, "test"].concat(_slice.call(arguments))); + }, + writable: true, + enumerable: true, + configurable: true + } + }, { + test: { + value: function () { + var _Foo$prototype$test3, _Foo$prototype$test4; + Foo.prototype.test.call(this); + (_Foo$prototype$test3 = Foo.prototype.test).call.apply(_Foo$prototype$test3, [this].concat(_slice.call(arguments))); + (_Foo$prototype$test4 = Foo.prototype.test).call.apply(_Foo$prototype$test4, [this, "test"].concat(_slice.call(arguments))); + }, + writable: true, + enumerable: true, + configurable: true + } + }); return Test; })(Foo); diff --git a/test/fixtures/transformation/optional-classes-fast-super/calling-super-properties/expected.js b/test/fixtures/transformation/optional-classes-fast-super/calling-super-properties/expected.js index f107cf4bc4..8eebc5204e 100644 --- a/test/fixtures/transformation/optional-classes-fast-super/calling-super-properties/expected.js +++ b/test/fixtures/transformation/optional-classes-fast-super/calling-super-properties/expected.js @@ -1,5 +1,10 @@ "use strict"; +var _prototypeProperties = function (child, staticProps, instanceProps) { + if (staticProps) Object.defineProperties(child, staticProps); + if (instanceProps) Object.defineProperties(child.prototype, instanceProps); +}; + var _inherits = function (child, parent) { if (typeof parent !== "function" && parent !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof parent); @@ -23,9 +28,16 @@ var Test = (function (Foo) { _inherits(Test, Foo); - Test.test = function () { - return Foo.wow.call(this); - }; + _prototypeProperties(Test, { + test: { + value: function () { + return Foo.wow.call(this); + }, + writable: true, + enumerable: true, + configurable: true + } + }); return Test; })(Foo);