move transformers over to using ast-types when constructing new nodes

This commit is contained in:
Sebastian McKenzie
2014-10-01 11:01:49 +10:00
parent 91737fc84b
commit 05c037d2d4
12 changed files with 95 additions and 138 deletions

View File

@@ -1,29 +1,21 @@
var util = require("../util");
var _ = require("lodash");
exports.ComprehensionExpression = function (node, parent, opts, generateUid) {
var blocks = node.blocks;
var single = function (node) {
var block = node.blocks[0];
_.each(blocks, function (block) {
if (!block.of) {
throw util.errorWithNode(block, "for-in array comprehension is not supported");
}
var templateName = "array-comprehension-map";
if (node.filter) templateName = "array-comprehension-filter";
return util.template(templateName, {
STATEMENT: node.body,
FILTER: node.filter,
ARRAY: block.right,
KEY: block.left
});
};
if (blocks.length === 1) {
var block = blocks[0];
var templateName = "array-comprehension-map";
if (node.filter) templateName += "-filter";
return util.template(templateName, {
ARRAY: block.right,
KEY: block.left,
FILTER: node.filter,
STATEMENT: node.body
});
}
var multiple = function (node, generateUid) {
var uid = generateUid("arr");
var container = util.template("array-comprehension-container", {
@@ -36,7 +28,7 @@ exports.ComprehensionExpression = function (node, parent, opts, generateUid) {
var returnStatement = body.pop();
var build = function () {
var self = blocks.shift();
var self = node.blocks.shift();
if (!self) return;
var child = build();
@@ -44,8 +36,8 @@ exports.ComprehensionExpression = function (node, parent, opts, generateUid) {
// last item
child = util.template("array-push", {
KEY: uid,
STATEMENT: node.body
STATEMENT: node.body,
KEY: uid
}, true);
// add a filter as this is our final stop
@@ -60,7 +52,7 @@ exports.ComprehensionExpression = function (node, parent, opts, generateUid) {
var container2 = util.template("array-comprehension-for-each", {
ARRAY: self.right,
KEY: self.left
KEY: self.left
}, true);
container2.expression.arguments[0].body.body = [child];
return container2;
@@ -71,3 +63,17 @@ exports.ComprehensionExpression = function (node, parent, opts, generateUid) {
return container;
};
exports.ComprehensionExpression = function (node, parent, opts, generateUid) {
_.each(node.blocks, function (block) {
if (!block.of) {
throw util.errorWithNode(block, "for-in array comprehension is not supported");
}
});
if (node.blocks.length === 1) {
return single(node);
} else {
return multiple(node, generateUid);
}
};