From a40cbc2cee0ae37aa44b90c513cab65da7b343f1 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 11 Oct 2014 11:49:03 +1100 Subject: [PATCH] make class methods unenumerable - fixes #41 --- lib/6to5/transformers/classes.js | 32 ++----- lib/6to5/util.js | 8 +- .../classes/accessing-super-class/expected.js | 91 +++++++++++-------- .../classes/instance-method/expected.js | 11 ++- test/fixtures/classes/static/expected.js | 7 +- 5 files changed, 79 insertions(+), 70 deletions(-) diff --git a/lib/6to5/transformers/classes.js b/lib/6to5/transformers/classes.js index fe1fac5bf7..1585ec2f06 100644 --- a/lib/6to5/transformers/classes.js +++ b/lib/6to5/transformers/classes.js @@ -91,19 +91,17 @@ var buildClassBody = function (body, className, superName, node) { throw util.errorWithNode(node, "unknown kind for constructor method"); } } else { - var add = addInstanceMethod; var mutatorMap = instanceMutatorMap; + if (node.static) mutatorMap = staticMutatorMap; - if (node.static) { - add = addStaticMethod; - mutatorMap = staticMutatorMap + var kind = node.kind; + + if (kind === "") { + kind = "value"; + util.pushMutatorMap(mutatorMap, methodName, "writeable", b.identifier("true")); } - if (node.kind === "") { - add(body, className, methodName, method); - } else { - util.pushMutatorMap(mutatorMap, methodName, node.kind, node); - } + util.pushMutatorMap(mutatorMap, methodName, kind, node); } }); @@ -177,19 +175,3 @@ var addConstructor = function (construct, method) { construct.body = method.body; construct.rest = method.rest; }; - -var addStaticMethod = function (body, className, methodName, method) { - body.push(util.template("class-static-method", { - METHOD_NAME: methodName, - CLASS_NAME: className, - FUNCTION: method - }, true)); -}; - -var addInstanceMethod = function (body, className, methodName, method) { - body.push(util.template("class-method", { - METHOD_NAME: methodName, - CLASS_NAME: className, - FUNCTION: method - }, true)); -}; diff --git a/lib/6to5/util.js b/lib/6to5/util.js index b4c0193b54..66f7f8580e 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -97,10 +97,10 @@ exports.buildDefineProperties = function (mutatorMap, keyNode) { var propNode = b.property("init", b.identifier(key), mapNode); - _.each(map, function (methodNode, type) { - methodNode = _.clone(methodNode); - if (methodNode.type === "MethodDefinition") methodNode = methodNode.value; - mapNode.properties.push(b.property("init", b.identifier(type), methodNode)); + _.each(map, function (node, key) { + node = _.clone(node); + if (node.type === "MethodDefinition") node = node.value; + mapNode.properties.push(b.property("init", b.identifier(key), node)); }); objExpr.properties.push(propNode); diff --git a/test/fixtures/classes/accessing-super-class/expected.js b/test/fixtures/classes/accessing-super-class/expected.js index 013063b60a..2d16b90ebb 100644 --- a/test/fixtures/classes/accessing-super-class/expected.js +++ b/test/fixtures/classes/accessing-super-class/expected.js @@ -1,38 +1,57 @@ -var Test = function (Foo) { - function Test() { - woops.super.test(); - Foo.call(this); - Foo.prototype.test.call(this); - foob(Foo); - Foo.call.apply(Foo, [this].concat(Array.prototype.slice.call(arguments))); - Foo.call.apply(Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this].concat(Array.prototype.slice.call(arguments))); - Foo.prototype.test.call.apply( - Foo.prototype, - [this, "test"].concat(Array.prototype.slice.call(arguments)) - ); - } - Test.prototype = Object.create(Foo.prototype, { - constructor: { - value: Test, - enumerable: false, - writable: true, - configurable: true +var Test = function(Foo) { + function Test() { + woops.super.test(); + Foo.call(this); + Foo.prototype.test.call(this); + foob(Foo); + Foo.call.apply(Foo, [this].concat(Array.prototype.slice.call(arguments))); + Foo.call.apply(Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype, [this].concat(Array.prototype.slice.call(arguments))); + + Foo.prototype.test.call.apply( + Foo.prototype, + [this, "test"].concat(Array.prototype.slice.call(arguments)) + ); } - }); - Test.__proto__ = Foo; - Test.prototype.test = function () { - Foo.prototype.test.call(this); - Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(Array.prototype.slice.call(arguments))); - Foo.prototype.test.call.apply( - Foo.prototype.test, - [this, "test"].concat(Array.prototype.slice.call(arguments)) - ); - }; - Test.foo = function () { - Foo.foo.call(this); - Foo.foo.call.apply(Foo.foo, [this].concat(Array.prototype.slice.call(arguments))); - Foo.foo.call.apply(Foo.foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); - }; - return Test; + + Test.prototype = Object.create(Foo.prototype, { + constructor: { + value: Test, + enumerable: false, + writable: true, + configurable: true + } + }); + + Test.__proto__ = Foo; + + Object.defineProperties(Test.prototype, { + test: { + writeable: true, + + value: function() { + Foo.prototype.test.call(this); + Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(Array.prototype.slice.call(arguments))); + + Foo.prototype.test.call.apply( + Foo.prototype.test, + [this, "test"].concat(Array.prototype.slice.call(arguments)) + ); + } + } + }); + + Object.defineProperties(Test, { + foo: { + writeable: true, + + value: function() { + Foo.foo.call(this); + Foo.foo.call.apply(Foo.foo, [this].concat(Array.prototype.slice.call(arguments))); + Foo.foo.call.apply(Foo.foo, [this, "test"].concat(Array.prototype.slice.call(arguments))); + } + } + }); + + return Test; }(Foo); diff --git a/test/fixtures/classes/instance-method/expected.js b/test/fixtures/classes/instance-method/expected.js index 724b0683fb..3945f41673 100644 --- a/test/fixtures/classes/instance-method/expected.js +++ b/test/fixtures/classes/instance-method/expected.js @@ -1,8 +1,13 @@ var Test = function () { function Test() { } - Test.prototype.test = function () { - return 5 + 5; - }; + Object.defineProperties(Test.prototype, { + test: { + writeable: true, + value: function () { + return 5 + 5; + } + } + }); return Test; }(); diff --git a/test/fixtures/classes/static/expected.js b/test/fixtures/classes/static/expected.js index 7af68d8a18..8892c37fb6 100644 --- a/test/fixtures/classes/static/expected.js +++ b/test/fixtures/classes/static/expected.js @@ -1,9 +1,12 @@ var A = function() { function A() {} - A.a = function() {}; - Object.defineProperties(A, { + a: { + writeable: true, + value: function() {} + }, + b: { get: function() {}, set: function(b) {}