move transformers over to using ast-types when constructing new nodes
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
|
||||
exports.ArrowFunctionExpression = function (node) {
|
||||
var body = node.body;
|
||||
|
||||
// expression body
|
||||
if (body.type !== "BlockStatement") {
|
||||
body = {
|
||||
type: "BlockStatement",
|
||||
body: [{
|
||||
type: "ReturnStatement",
|
||||
argument: body
|
||||
}]
|
||||
};
|
||||
body = b.blockStatement([
|
||||
b.returnStatement(body)
|
||||
]);
|
||||
}
|
||||
|
||||
node.expression = false;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var isLet = function (node) {
|
||||
if (node.type === "VariableDeclaration" && node.kind === "let") {
|
||||
if (node && node.type === "VariableDeclaration" && node.kind === "let") {
|
||||
node.kind = "var";
|
||||
node._ignoreBlockBindingHoist = true;
|
||||
return true;
|
||||
@@ -54,16 +55,14 @@ var buildNode = function (node) {
|
||||
if (node._ignoreBlockBindingHoist) return node;
|
||||
|
||||
if (node.type === "VariableDeclaration" && node.kind === "var") {
|
||||
_.each(node.declarations, function (declar) {
|
||||
nodes.push(util.template("variable-declare", {
|
||||
KEY: declar.id
|
||||
}));
|
||||
});
|
||||
nodes.push(b.variableDeclaration("var", node.declarations.map(function (declar) {
|
||||
return b.variableDeclarator(declar.id, null);
|
||||
})));
|
||||
|
||||
return node.declarations.map(function (declar) {
|
||||
return util.template("assign", {
|
||||
KEY: declar.id,
|
||||
VALUE: declar.init
|
||||
VALUE: declar.init,
|
||||
KEY: declar.id
|
||||
}, true);
|
||||
});
|
||||
} else if (node.type === "ForInStatement" && !node.left._ignoreBlockBindingHoist) {
|
||||
@@ -79,15 +78,9 @@ var buildNode = function (node) {
|
||||
|
||||
//
|
||||
|
||||
var func = {
|
||||
type: "FunctionExpression",
|
||||
params: [],
|
||||
defaults: [],
|
||||
body: {
|
||||
type: "BlockStatement",
|
||||
body: node
|
||||
}
|
||||
};
|
||||
var block = b.blockStatement([]);
|
||||
block.body = node;
|
||||
var func = b.functionExpression(null, [], block, false)
|
||||
|
||||
var templateName = "function-call";
|
||||
if (traverse.hasType(node, "ThisExpression")) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ClassDeclaration = function (node) {
|
||||
@@ -47,7 +48,7 @@ var buildClassBody = function (body, className, superName, node) {
|
||||
if (node.kind === "") {
|
||||
addConstructor(body[0], method);
|
||||
} else {
|
||||
throw new Error("unknown kind for constructor method");
|
||||
throw util.errorWithNode(node, "unknown kind for constructor method");
|
||||
}
|
||||
} else {
|
||||
if (node.kind === "") {
|
||||
@@ -69,29 +70,27 @@ var buildClassBody = function (body, className, superName, node) {
|
||||
|
||||
var superIdentifier = function (superName, node, parent) {
|
||||
if (parent.property === node) return;
|
||||
if (!superName) return;
|
||||
|
||||
node.name = superName.name;
|
||||
node.name = superName.name || superName.value;
|
||||
|
||||
// super(); -> ClassName.call(this);
|
||||
if (parent.type === "CallExpression" && parent.callee === node) {
|
||||
node.name += ".call";
|
||||
parent.arguments.unshift({
|
||||
type: "ThisExpression"
|
||||
});
|
||||
parent.arguments.unshift(b.thisExpression());
|
||||
}
|
||||
};
|
||||
|
||||
var replaceInstanceSuperReferences = function (superName, method) {
|
||||
superName = superName || b.literal("Function");
|
||||
|
||||
traverse(method, function (node, parent) {
|
||||
if (node.type === "Identifier" && node.name === "super") {
|
||||
superIdentifier(superName, node, parent);
|
||||
} else if (node.type === "MemberExpression") {
|
||||
// no accessing of super properties
|
||||
|
||||
|
||||
if (isAccessingSuperProperties(parent, node)) {
|
||||
throw new Error("cannot access super properties");
|
||||
throw util.errorWithNode(node, "cannot access super properties");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@@ -101,16 +100,10 @@ var replaceInstanceSuperReferences = function (superName, method) {
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
callee.property.name = "prototype." + callee.property.name + ".call";
|
||||
node.arguments.unshift({
|
||||
type: "ThisExpression"
|
||||
});
|
||||
node.arguments.unshift(b.thisExpression());
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!superName) {
|
||||
throw new Error("cannot access super as this class has none");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
var util = require("../util");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function (node) {
|
||||
_.each(node.defaults, function (def, i) {
|
||||
if (!def) return;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var isPattern = function (id) {
|
||||
@@ -20,15 +21,9 @@ exports.VariableDeclaration = function (node, parent, opts, generateUid) {
|
||||
//
|
||||
|
||||
var buildVariableAssign = function (id, init) {
|
||||
return {
|
||||
type: "VariableDeclaration",
|
||||
kind: node.kind,
|
||||
declarations: [{
|
||||
type: "VariableDeclarator",
|
||||
id: id,
|
||||
init: init
|
||||
}]
|
||||
};
|
||||
return b.variableDeclaration(node.kind, [
|
||||
b.variableDeclarator(id, init)
|
||||
]);
|
||||
};
|
||||
|
||||
var push = function (pattern, parentId) {
|
||||
@@ -43,11 +38,7 @@ exports.VariableDeclaration = function (node, parent, opts, generateUid) {
|
||||
_.each(pattern.properties, function (prop) {
|
||||
var id = prop.value;
|
||||
|
||||
var init = {
|
||||
type: "MemberExpression",
|
||||
object: parentId,
|
||||
property: prop.key
|
||||
};
|
||||
var init = b.memberExpression(parentId, prop.key, false);
|
||||
|
||||
if (isPattern(id)) {
|
||||
push(id, init);
|
||||
@@ -59,15 +50,7 @@ exports.VariableDeclaration = function (node, parent, opts, generateUid) {
|
||||
|
||||
var pushArrayPattern = function (pattern, parentId) {
|
||||
_.each(pattern.elements, function (id, i) {
|
||||
var init = {
|
||||
type: "MemberExpression",
|
||||
computed: true,
|
||||
object: parentId,
|
||||
property: {
|
||||
type: "Literal",
|
||||
value: i
|
||||
}
|
||||
};
|
||||
var init = b.memberExpression(parentId, b.literal(i), true);
|
||||
|
||||
if (id.type === "Identifier") {
|
||||
nodes.push(buildVariableAssign(id, init));
|
||||
@@ -86,10 +69,7 @@ exports.VariableDeclaration = function (node, parent, opts, generateUid) {
|
||||
VALUE: init
|
||||
}, true));
|
||||
|
||||
init = {
|
||||
type: "Identifier",
|
||||
name: key
|
||||
};
|
||||
init = b.identifier(key);
|
||||
}
|
||||
|
||||
push(id, init);
|
||||
|
||||
@@ -3,18 +3,16 @@ var util = require("../util");
|
||||
exports.ForOfStatement = function (node, parent, opts, generateUid) {
|
||||
var node2 = util.template("for-of", {
|
||||
ITERATOR_KEY: generateUid("iterator"),
|
||||
STEP_KEY: generateUid("step"),
|
||||
|
||||
KEY: node.left.declarations[0].id,
|
||||
OBJECT: node.right
|
||||
STEP_KEY: generateUid("step"),
|
||||
OBJECT: node.right,
|
||||
KEY: node.left.declarations[0].id
|
||||
});
|
||||
|
||||
var block = node2.body;
|
||||
|
||||
var declar = block.body[0];
|
||||
declar.kind = node.left.kind;
|
||||
|
||||
var block = node2.body;
|
||||
block.body = block.body.concat(node.body.body);
|
||||
|
||||
var declar = block.body[0];
|
||||
declar.kind = node.left.kind;
|
||||
|
||||
return node2;
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ImportDeclaration = function (node) {
|
||||
@@ -35,15 +36,22 @@ exports.ExportDeclaration = function (node) {
|
||||
_.each(node.specifiers, function (specifier) {
|
||||
var variableName = specifier.name || specifier.id;
|
||||
|
||||
if (specifier.type === "ExportBatchSpecifier") {
|
||||
nodes.push(util.template("exports-wildcard", {
|
||||
MODULE_NAME: node.source
|
||||
}, true));
|
||||
if (node.source) {
|
||||
if (specifier.type === "ExportBatchSpecifier") {
|
||||
nodes.push(util.template("exports-wildcard", {
|
||||
MODULE_NAME: node.source
|
||||
}, true));
|
||||
} else {
|
||||
nodes.push(util.template("exports-require-assign-key", {
|
||||
VARIABLE_NAME: variableName.name,
|
||||
MODULE_NAME: node.source,
|
||||
KEY: specifier.id
|
||||
}, true));
|
||||
}
|
||||
} else {
|
||||
nodes.push(util.template("exports-require-assign-key", {
|
||||
VARIABLE_NAME: variableName.name,
|
||||
MODULE_NAME: node.source,
|
||||
KEY: specifier.id.name
|
||||
nodes.push(util.template("exports-assign", {
|
||||
VALUE: specifier.id,
|
||||
KEY: specifier.id
|
||||
}, true));
|
||||
}
|
||||
});
|
||||
@@ -61,10 +69,7 @@ exports.ExportDeclaration = function (node) {
|
||||
|
||||
_.each(declar.declarations, function (declar) {
|
||||
nodes.push(util.template("exports-alias-var", {
|
||||
STRING_KEY: {
|
||||
type: "Literal",
|
||||
value: declar.id.name
|
||||
},
|
||||
STRING_KEY: b.literal(declar.id.name),
|
||||
KEY: declar.id
|
||||
}, true));
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.Property = function (node) {
|
||||
@@ -6,9 +7,6 @@ exports.Property = function (node) {
|
||||
};
|
||||
|
||||
exports.ObjectExpression = function (node) {
|
||||
//if (node.ignorePropertyMethods) return;
|
||||
//node.ignorePropertyMethods = true;
|
||||
|
||||
var mutatorMap = {};
|
||||
|
||||
node.properties = node.properties.filter(function (prop) {
|
||||
@@ -24,9 +22,6 @@ exports.ObjectExpression = function (node) {
|
||||
|
||||
return util.template("object-define-properties-closure", {
|
||||
OBJECT: node,
|
||||
CONTENT: util.buildDefineProperties(mutatorMap, {
|
||||
type: "Identifier",
|
||||
name: "obj"
|
||||
}).expression
|
||||
CONTENT: util.buildDefineProperties(mutatorMap, b.identifier("obj")).expression
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function (node) {
|
||||
if (!node.rest) return;
|
||||
|
||||
@@ -11,9 +13,6 @@ exports.FunctionExpression = function (node) {
|
||||
|
||||
node.body.body.unshift(util.template(templateName, {
|
||||
VARIABLE_NAME: rest,
|
||||
SLICE_ARG: {
|
||||
type: "Literal",
|
||||
value: node.params.length
|
||||
}
|
||||
SLICE_ARG: b.literal(node.params.length)
|
||||
}));
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ArrayExpression = function (node) {
|
||||
@@ -28,12 +29,8 @@ exports.CallExpression = function (node) {
|
||||
if (args.length && _.last(args).type === "SpreadElement") {
|
||||
var spread = args.pop();
|
||||
|
||||
var spreadLiteral = spread.argument;
|
||||
|
||||
var contextLiteral = {
|
||||
type: "Literal",
|
||||
value: null
|
||||
};
|
||||
var spreadLiteral = spread.argument;
|
||||
var contextLiteral = b.literal(null);
|
||||
|
||||
node.arguments = [];
|
||||
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var buildBinaryExpression = function (left, right) {
|
||||
return {
|
||||
type: "BinaryExpression",
|
||||
operator: "+",
|
||||
left: left,
|
||||
right: right
|
||||
};
|
||||
return b.binaryExpression("+", left, right);
|
||||
};
|
||||
|
||||
exports.TemplateLiteral = function (node) {
|
||||
var nodes = [];
|
||||
|
||||
_.each(node.quasis, function (elem) {
|
||||
nodes.push({
|
||||
type: "Literal",
|
||||
value: elem.value.raw
|
||||
});
|
||||
nodes.push(b.literal(elem.value.raw));
|
||||
|
||||
var expr = node.expressions.shift();
|
||||
if (expr) nodes.push(expr);
|
||||
|
||||
Reference in New Issue
Block a user