From 3e34bbe722408e5d1f63e400f4e9c4bfb0623390 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 10 Oct 2014 13:57:08 +1100 Subject: [PATCH] support static property on ClassMethods - fixes #28 --- lib/6to5/templates/class-static-method.js | 1 + lib/6to5/transformers/classes.js | 29 +++++++++++++++++++---- package.json | 2 +- test/fixtures/classes/static/actual.js | 13 ++++++++++ test/fixtures/classes/static/expected.js | 14 +++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 lib/6to5/templates/class-static-method.js create mode 100644 test/fixtures/classes/static/actual.js create mode 100644 test/fixtures/classes/static/expected.js diff --git a/lib/6to5/templates/class-static-method.js b/lib/6to5/templates/class-static-method.js new file mode 100644 index 0000000000..3edb25589b --- /dev/null +++ b/lib/6to5/templates/class-static-method.js @@ -0,0 +1 @@ +CLASS_NAME.METHOD_NAME = FUNCTION; diff --git a/lib/6to5/transformers/classes.js b/lib/6to5/transformers/classes.js index 142d8002be..6deae86066 100644 --- a/lib/6to5/transformers/classes.js +++ b/lib/6to5/transformers/classes.js @@ -63,7 +63,8 @@ var buildClass = function (node, generateUid) { }; var buildClassBody = function (body, className, superName, node) { - var mutatorMap = {}; + var instanceMutatorMap = {}; + var staticMutatorMap = {}; var classBody = node.body.body; _.each(classBody, function (node) { @@ -79,20 +80,32 @@ 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) { + add = addStaticMethod; + mutatorMap = staticMutatorMap + } + if (node.kind === "") { - addInstanceMethod(body, className, methodName, method); + add(body, className, methodName, method); } else { util.pushMutatorMap(mutatorMap, methodName, node.kind, node); } } }); - if (!_.isEmpty(mutatorMap)) { + if (!_.isEmpty(instanceMutatorMap)) { var protoId = util.template("prototype-identifier", { CLASS_NAME: className }); - body.push(util.buildDefineProperties(mutatorMap, protoId)); + body.push(util.buildDefineProperties(instanceMutatorMap, protoId)); + } + + if (!_.isEmpty(staticMutatorMap)) { + body.push(util.buildDefineProperties(staticMutatorMap, className)); } }; @@ -148,6 +161,14 @@ var addConstructor = function (construct, method) { 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, diff --git a/package.json b/package.json index e5ecaa1edd..7ba7769928 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "6to5", "description": "Turn ES6 code into vanilla ES5 with source maps and no runtime", - "version": "1.7.2", + "version": "1.7.3", "author": "Sebastian McKenzie ", "homepage": "https://github.com/sebmck/6to5", "repository": { diff --git a/test/fixtures/classes/static/actual.js b/test/fixtures/classes/static/actual.js new file mode 100644 index 0000000000..e74b71aaa8 --- /dev/null +++ b/test/fixtures/classes/static/actual.js @@ -0,0 +1,13 @@ +class A { + static a() { + + } + + static get b(){ + + } + + static set b(b){ + + } +} diff --git a/test/fixtures/classes/static/expected.js b/test/fixtures/classes/static/expected.js new file mode 100644 index 0000000000..7af68d8a18 --- /dev/null +++ b/test/fixtures/classes/static/expected.js @@ -0,0 +1,14 @@ +var A = function() { + function A() {} + + A.a = function() {}; + + Object.defineProperties(A, { + b: { + get: function() {}, + set: function(b) {} + } + }); + + return A; +}();