add classProps declaration to simplify/nicen up class property defining - closes #88

This commit is contained in:
Sebastian McKenzie
2014-11-03 13:36:23 +11:00
parent c8139317ee
commit c17878913b
13 changed files with 95 additions and 40 deletions

View File

@@ -0,0 +1,4 @@
(function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
})

View File

@@ -56,14 +56,14 @@ var buildClass = function (node, file) {
container.callee.params.push(superClassCallee);
}
buildClassBody(body, className, superName, node);
buildClassBody(file, body, className, superName, node);
body.push(returnStatement);
return container;
};
var buildClassBody = function (body, className, superName, node) {
var buildClassBody = function (file, body, className, superName, node) {
var instanceMutatorMap = {};
var staticMutatorMap = {};
var hasConstructor = false;
@@ -105,16 +105,28 @@ var buildClassBody = function (body, className, superName, node) {
}, true));
}
var instanceProps;
var staticProps;
if (!_.isEmpty(instanceMutatorMap)) {
var protoId = util.template("prototype-identifier", {
CLASS_NAME: className
});
body.push(util.buildDefineProperties(instanceMutatorMap, protoId));
instanceProps = util.buildDefineProperties(instanceMutatorMap, protoId);
}
if (!_.isEmpty(staticMutatorMap)) {
body.push(util.buildDefineProperties(staticMutatorMap, className));
staticProps = util.buildDefineProperties(staticMutatorMap, className);
}
if (instanceProps || staticProps) {
instanceProps = instanceProps || b.literal(null);
staticProps = staticProps || b.literal(null);
body.push(b.expressionStatement(
b.callExpression(file.addDeclaration("class-props"), [className, staticProps, instanceProps])
));
}
};

View File

@@ -24,6 +24,9 @@ exports.ObjectExpression = function (node, parent, file) {
return util.template("object-define-properties-closure", {
KEY: objId,
OBJECT: node,
CONTENT: util.buildDefineProperties(mutatorMap, objId).expression
CONTENT: util.template("object-define-properties", {
OBJECT: objId,
PROPS: util.buildDefineProperties(mutatorMap)
})
});
};

View File

@@ -147,7 +147,7 @@ exports.pushMutatorMap = function (mutatorMap, key, kind, method) {
}
};
exports.buildDefineProperties = function (mutatorMap, keyNode) {
exports.buildDefineProperties = function (mutatorMap) {
var objExpr = b.objectExpression([]);
_.each(mutatorMap, function (map, key) {
@@ -164,10 +164,7 @@ exports.buildDefineProperties = function (mutatorMap, keyNode) {
objExpr.properties.push(propNode);
});
return exports.template("object-define-properties", {
OBJECT: keyNode,
PROPS: objExpr
}, true);
return objExpr;
};
exports.template = function (name, nodes, keepExpression) {