add classProps declaration to simplify/nicen up class property defining - closes #88
This commit is contained in:
4
lib/6to5/templates/class-props.js
Normal file
4
lib/6to5/templates/class-props.js
Normal file
@@ -0,0 +1,4 @@
|
||||
(function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
})
|
||||
@@ -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])
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var _extends = function (child, parent) {
|
||||
child.prototype = Object.create(parent.prototype, {
|
||||
constructor: {
|
||||
@@ -27,7 +34,17 @@ var Test = function(Foo) {
|
||||
|
||||
_extends(Test, Foo);
|
||||
|
||||
Object.defineProperties(Test.prototype, {
|
||||
_classProps(Test, {
|
||||
foo: {
|
||||
writable: true,
|
||||
|
||||
value: function() {
|
||||
Foo.foo.call(this);
|
||||
Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
|
||||
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
|
||||
}
|
||||
}
|
||||
}, {
|
||||
test: {
|
||||
writable: true,
|
||||
|
||||
@@ -39,17 +56,5 @@ var Test = function(Foo) {
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperties(Test, {
|
||||
foo: {
|
||||
writable: true,
|
||||
|
||||
value: function() {
|
||||
Foo.foo.call(this);
|
||||
Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
|
||||
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var _extends = function (child, parent) {
|
||||
child.prototype = Object.create(parent.prototype, {
|
||||
constructor: {
|
||||
@@ -13,7 +18,6 @@ var _extends = function (child, parent) {
|
||||
child.__proto__ = parent;
|
||||
};
|
||||
|
||||
|
||||
var Test = function(Foo) {
|
||||
var Test = function Test() {
|
||||
Foo.prototype.test.whatever();
|
||||
@@ -22,7 +26,7 @@ var Test = function(Foo) {
|
||||
|
||||
_extends(Test, Foo);
|
||||
|
||||
Object.defineProperties(Test, {
|
||||
_classProps(Test, {
|
||||
test: {
|
||||
writable: true,
|
||||
|
||||
@@ -30,7 +34,7 @@ var Test = function(Foo) {
|
||||
return Foo.wow.call(this);
|
||||
}
|
||||
}
|
||||
});
|
||||
}, null);
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var Test = function() {
|
||||
var Test = function Test() {};
|
||||
|
||||
Object.defineProperties(Test.prototype, {
|
||||
_classProps(Test, null, {
|
||||
test: {
|
||||
get: function() {
|
||||
return 5 + 5;
|
||||
@@ -16,4 +21,4 @@ var Test = function() {
|
||||
});
|
||||
|
||||
return Test;
|
||||
}();
|
||||
}();
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var Test = function() {
|
||||
var Test = function Test() {};
|
||||
|
||||
Object.defineProperties(Test.prototype, {
|
||||
_classProps(Test, null, {
|
||||
test: {
|
||||
get: function() {
|
||||
return 5 + 5;
|
||||
@@ -12,4 +17,4 @@ var Test = function() {
|
||||
});
|
||||
|
||||
return Test;
|
||||
}();
|
||||
}();
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var Test = function() {
|
||||
var Test = function Test() {};
|
||||
|
||||
Object.defineProperties(Test.prototype, {
|
||||
_classProps(Test, null, {
|
||||
test: {
|
||||
writable: true,
|
||||
|
||||
@@ -14,4 +19,4 @@ var Test = function() {
|
||||
});
|
||||
|
||||
return Test;
|
||||
}();
|
||||
}();
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var Test = function() {
|
||||
var Test = function Test() {};
|
||||
|
||||
Object.defineProperties(Test.prototype, {
|
||||
_classProps(Test, null, {
|
||||
test: {
|
||||
set: function(val) {
|
||||
this._test = val;
|
||||
@@ -12,4 +17,4 @@ var Test = function() {
|
||||
});
|
||||
|
||||
return Test;
|
||||
}();
|
||||
}();
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var A = function() {
|
||||
var A = function A() {};
|
||||
|
||||
Object.defineProperties(A, {
|
||||
_classProps(A, {
|
||||
a: {
|
||||
writable: true,
|
||||
value: function() {}
|
||||
@@ -13,7 +18,7 @@ var A = function() {
|
||||
get: function() {},
|
||||
set: function(b) {}
|
||||
}
|
||||
});
|
||||
}, null);
|
||||
|
||||
return A;
|
||||
}();
|
||||
}();
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
};
|
||||
|
||||
var Test = function() {
|
||||
var Test = function Test() {};
|
||||
|
||||
Object.defineProperties(Test.prototype, {
|
||||
_classProps(Test, null, {
|
||||
bar: {
|
||||
get: function() {
|
||||
throw new Error("wow");
|
||||
@@ -15,4 +20,4 @@ var Test = function() {
|
||||
}();
|
||||
|
||||
var test = new Test();
|
||||
test.bar;
|
||||
test.bar;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"column": 11
|
||||
},
|
||||
"generated": {
|
||||
"line": 9,
|
||||
"line": 17,
|
||||
"column": 15
|
||||
}
|
||||
}]
|
||||
|
||||
Reference in New Issue
Block a user