support static property on ClassMethods - fixes #28

This commit is contained in:
Sebastian McKenzie
2014-10-10 13:57:08 +11:00
parent 3cb8866fcc
commit 3e34bbe722
5 changed files with 54 additions and 5 deletions

View File

@@ -0,0 +1 @@
CLASS_NAME.METHOD_NAME = FUNCTION;

View File

@@ -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,

View File

@@ -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 <sebmck@gmail.com>",
"homepage": "https://github.com/sebmck/6to5",
"repository": {

13
test/fixtures/classes/static/actual.js vendored Normal file
View File

@@ -0,0 +1,13 @@
class A {
static a() {
}
static get b(){
}
static set b(b){
}
}

View File

@@ -0,0 +1,14 @@
var A = function() {
function A() {}
A.a = function() {};
Object.defineProperties(A, {
b: {
get: function() {},
set: function(b) {}
}
});
return A;
}();