merge pretzel maps and method binding

This commit is contained in:
Sebastian McKenzie
2014-11-26 17:23:33 +11:00
parent a29505f75d
commit 3c808fcef2
15 changed files with 70 additions and 76 deletions

View File

@@ -1,6 +1,6 @@
var _ = require("lodash");
_.each(["BindMemberExpression", "PretzelMapExpression"], function (type) {
_.each(["BindMemberExpression", "BindFunctionExpression"], function (type) {
exports[type] = function () {
throw new ReferenceError("Trying to render non-standard playground node " + JSON.stringify(type));
};

View File

@@ -38,7 +38,7 @@ def("BindMemberExpression")
.field("property", or(def("Identifier"), def("Expression")))
.field("arguments", [def("Expression")]);
def("PretzelMapExpression")
def("BindFunctionExpression")
.bases("Expression")
.build("callee", "arguments")
.field("callee", def("Expression"))

View File

@@ -59,7 +59,6 @@ _.each({
// plyground
methodBinding: require("./transformers/playground-method-binding"),
memoizationOperator: require("./transformers/playground-memoization-operator"),
pretzelMap: require("./transformers/playground-pretzel-map"),
_blockHoist: require("./transformers/_block-hoist"),
_declarations: require("./transformers/_declarations"),

View File

@@ -1,4 +1,5 @@
var t = require("../../types");
var _ = require("lodash");
exports.BindMemberExpression = function (node, parent, file, scope) {
var object = node.object;
@@ -28,3 +29,30 @@ exports.BindMemberExpression = function (node, parent, file, scope) {
return call;
}
};
exports.BindFunctionExpression = function (node, parent, file, scope) {
var buildCall = function (args) {
var param = file.generateUidIdentifier("val", scope);
return t.functionExpression(null, [param], t.blockStatement([
t.returnStatement(t.callExpression(t.memberExpression(param, node.callee), args))
]));
};
if (_.find(node.arguments, t.isDynamic)) {
var argsIdName = file.generateUid("args", scope);
var argsId = t.identifier(argsIdName);
scope.push({
key: argsIdName,
id: argsId
});
return t.sequenceExpression([
t.assignmentExpression("=", argsId, t.arrayExpression(node.arguments)),
buildCall(node.arguments.map(function (node, i) {
return t.memberExpression(argsId, t.literal(i), true);
}))
]);
} else {
return buildCall(node.arguments);
}
};

View File

@@ -1,29 +0,0 @@
var t = require("../../types");
var _ = require("lodash");
exports.PretzelMapExpression = function (node, parent, file, scope) {
var buildCall = function (args) {
var param = file.generateUidIdentifier("val", scope);
return t.functionExpression(null, [param], t.blockStatement([
t.returnStatement(t.callExpression(t.memberExpression(param, node.callee), args))
]));
};
if (_.find(node.arguments, t.isDynamic)) {
var argsIdName = file.generateUid("args", scope);
var argsId = t.identifier(argsIdName);
scope.push({
key: argsIdName,
id: argsId
});
return t.sequenceExpression([
t.assignmentExpression("=", argsId, t.arrayExpression(node.arguments)),
buildCall(node.arguments.map(function (node, i) {
return t.memberExpression(argsId, t.literal(i), true);
}))
]);
} else {
return buildCall(node.arguments);
}
};

View File

@@ -44,7 +44,7 @@
"ObjectExpression": ["properties"],
"ObjectPattern": ["properties"],
"ParenthesizedExpression": ["expression"],
"PretzelMapExpression": ["callee", "arguments"],
"BindFunctionExpression": ["callee", "arguments"],
"Program": ["body"],
"Property": ["key", "value"],
"ReturnStatement": ["argument"],