remove builders and move them to automatially generated types

This commit is contained in:
Sebastian McKenzie 2014-11-03 21:13:35 +11:00
parent 718e342e86
commit 816c1d304b
22 changed files with 188 additions and 193 deletions

View File

@ -1,46 +0,0 @@
var _ = require("lodash");
var build = function (type, keys) {
return function () {
var args = arguments;
var node = { type: type };
_.each(keys, function (key, i) {
node[key] = args[i];
});
return node;
};
};
exports.identifier = build("Identifier", ["name"]);
exports.literal = build("Literal", ["value"]);
exports.functionExpression = build("FunctionExpression", ["id", "params", "body"]);
exports.returnStatement = build("ReturnStatement", ["argument"]);
exports.assignmentExpression = build("AssignmentExpression", ["operator", "left", "right"]);
exports.ifStatement = build("IfStatement", ["test", "consequent", "alternate"]);
exports.callExpression = build("CallExpression", ["callee", "arguments"]);
exports.blockStatement = build("BlockStatement", ["body"]);
exports.memberExpression = build("MemberExpression", ["object", "property", "computed"]);
exports.variableDeclaration = build("VariableDeclaration", ["kind", "declarations"]);
exports.variableDeclarator = build("VariableDeclarator", ["id", "init"]);
exports.arrayExpression = build("ArrayExpression", ["elements"]);
exports.binaryExpression = build("BinaryExpression", ["operator", "left", "right"]);
exports.expressionStatement = build("ExpressionStatement", ["expression"]);
exports.thisExpression = build("ThisExpression");
exports.objectExpression = build("ObjectExpression", ["properties"]);
exports.property = build("Property", ["kind", "key", "value"]);

View File

@ -6,7 +6,7 @@ var transform = require("./transform");
var generate = require("./generator");
var acorn = require("acorn-6to5");
var util = require("./util");
var b = require("./builders");
var t = require("./types");
var _ = require("lodash");
function File(opts) {
@ -72,7 +72,7 @@ File.prototype.addDeclaration = function (name) {
var declar = this.declarations[name];
if (declar) return declar.uid;
var uid = b.identifier(this.generateUid(name));
var uid = t.identifier(this.generateUid(name));
this.declarations[name] = {
uid: uid,
node: util.template(name)

View File

@ -78,5 +78,5 @@ exports.XJSClosingElement = function (node, print) {
};
exports.XJSEmptyExpression = function () {
this.push("null");
};

View File

@ -3,7 +3,6 @@ module.exports = AMDFormatter;
var CommonJSFormatter = require("./common");
var util = require("../util");
var t = require("../types");
var b = require("../builders");
var _ = require("lodash");
function AMDFormatter(file) {
@ -19,21 +18,21 @@ AMDFormatter.prototype.transform = function (ast) {
// build an array of module names
var names = [b.literal("exports")];
var names = [t.literal("exports")];
_.each(this.ids, function (id, name) {
names.push(b.literal(name));
names.push(t.literal(name));
});
names = b.arrayExpression(names);
names = t.arrayExpression(names);
// build up define container
var params = _.values(this.ids);
params.unshift(b.identifier("exports"));
params.unshift(t.identifier("exports"));
var container = b.functionExpression(null, params, b.blockStatement(body));
var call = b.callExpression(b.identifier("define"), [names, container]);
var container = t.functionExpression(null, params, t.blockStatement(body));
var call = t.callExpression(t.identifier("define"), [names, container]);
program.body = [b.expressionStatement(call)];
program.body = [t.expressionStatement(call)];
};
AMDFormatter.prototype._push = function (node) {
@ -43,7 +42,7 @@ AMDFormatter.prototype._push = function (node) {
if (ids[id]) {
return ids[id];
} else {
return this.ids[id] = b.identifier(this.file.generateUid(id));
return this.ids[id] = t.identifier(this.file.generateUid(id));
}
};
@ -57,7 +56,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
// import foo from "foo";
if (specifier.default) {
id = b.identifier("default");
id = t.identifier("default");
}
var ref;
@ -67,11 +66,11 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
ref = this._push(node);
} else {
// import foo from "foo";
ref = b.memberExpression(this._push(node), id, false);
ref = t.memberExpression(this._push(node), id, false);
}
nodes.push(b.variableDeclaration("var", [
b.variableDeclarator(key, ref)
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(key, ref)
]));
};

View File

@ -2,7 +2,6 @@ module.exports = CommonJSFormatter;
var util = require("../util");
var t = require("../types");
var b = require("../builders");
function CommonJSFormatter(file) {
this.file = file;
@ -20,7 +19,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
// import foo from "foo";
if (specifier.default) {
specifier.id = b.identifier("default");
specifier.id = t.identifier("default");
}
var templateName = "require-assign";
@ -100,6 +99,6 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
return this._exportSpecifier(function () {
return b.callExpression(b.identifier("require"), [node.source]);
return t.callExpression(t.identifier("require"), [node.source]);
}, specifier, node, nodes);
};

View File

@ -2,7 +2,7 @@ module.exports = UMDFormatter;
var AMDFormatter = require("./amd");
var util = require("../util");
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
function UMDFormatter(file) {
@ -20,28 +20,28 @@ UMDFormatter.prototype.transform = function (ast) {
var names = [];
_.each(this.ids, function (id, name) {
names.push(b.literal(name));
names.push(t.literal(name));
});
// factory
var ids = _.values(this.ids);
var args = [b.identifier("exports")].concat(ids);
var args = [t.identifier("exports")].concat(ids);
var factory = b.functionExpression(null, args, b.blockStatement(body));
var factory = t.functionExpression(null, args, t.blockStatement(body));
// runner
var runner = util.template("umd-runner-body", {
AMD_ARGUMENTS: b.arrayExpression([b.literal("exports")].concat(names)),
AMD_ARGUMENTS: t.arrayExpression([t.literal("exports")].concat(names)),
COMMON_ARGUMENTS: names.map(function (name) {
return b.callExpression(b.identifier("require"), [name]);
return t.callExpression(t.identifier("require"), [name]);
})
});
//
var call = b.callExpression(runner, [factory]);
program.body = [b.expressionStatement(call)];
var call = t.callExpression(runner, [factory]);
program.body = [t.expressionStatement(call)];
};

View File

@ -1,12 +1,12 @@
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
module.exports = function (ast, file) {
var body = ast.program.body;
_.each(file.declarations, function (declar) {
body.unshift(b.variableDeclaration("var", [
b.variableDeclarator(declar.uid, declar.node)
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(declar.uid, declar.node)
]));
});
};

View File

@ -1,12 +1,11 @@
var traverse = require("../traverse");
var util = require("../util");
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
exports.ClassDeclaration = function (node, parent, file) {
return b.variableDeclaration("var", [
b.variableDeclarator(node.id, buildClass(node, file))
return t.variableDeclaration("var", [
t.variableDeclarator(node.id, buildClass(node, file))
]);
};
@ -23,7 +22,7 @@ var getMemberExpressionObject = function (node) {
var buildClass = function (node, file) {
var superName = node.superClass;
var className = node.id || b.identifier(file.generateUid("class"));
var className = node.id || t.identifier(file.generateUid("class"));
var superClassArgument = node.superClass;
var superClassCallee = node.superClass;
@ -33,7 +32,7 @@ var buildClass = function (node, file) {
superClassArgument = superClassCallee = getMemberExpressionObject(superName);
} else if (!t.isIdentifier(superName)) {
superClassArgument = superName;
superClassCallee = superName = b.identifier(file.generateUid("ref"));
superClassCallee = superName = t.identifier(file.generateUid("ref"));
}
}
@ -50,7 +49,7 @@ var buildClass = function (node, file) {
var returnStatement = body.pop();
if (superName) {
body.push(b.expressionStatement(b.callExpression(file.addDeclaration("extends"), [className, superName])));
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName])));
container.arguments.push(superClassArgument);
container.callee.params.push(superClassCallee);
@ -95,7 +94,7 @@ var buildClassBody = function (file, construct, body, className, superName, node
if (kind === "") {
kind = "value";
util.pushMutatorMap(mutatorMap, methodName, "writable", b.identifier("true"));
util.pushMutatorMap(mutatorMap, methodName, "writable", t.identifier("true"));
}
util.pushMutatorMap(mutatorMap, methodName, kind, node);
@ -124,13 +123,13 @@ var buildClassBody = function (file, construct, body, className, superName, node
}
if (instanceProps || staticProps) {
staticProps = staticProps || b.literal(null);
staticProps = staticProps || t.literal(null);
var args = [className, staticProps];
if (instanceProps) args.push(instanceProps);
body.push(b.expressionStatement(
b.callExpression(file.addDeclaration("class-props"), args)
body.push(t.expressionStatement(
t.callExpression(file.addDeclaration("class-props"), args)
));
}
};
@ -140,32 +139,32 @@ var superIdentifier = function (superName, methodNode, methodName, node, parent)
return;
} else if (t.isCallExpression(parent) && parent.callee === node) {
// super(); -> ClassName.prototype.MethodName.call(this);
parent.arguments.unshift(b.thisExpression());
parent.arguments.unshift(t.thisExpression());
if (methodName === "constructor") {
// constructor() { super(); }
return b.memberExpression(superName, b.identifier("call"));
return t.memberExpression(superName, t.identifier("call"));
} else {
node = superName;
// foo() { super(); }
if (!methodNode.static) {
node = b.memberExpression(node, b.identifier("prototype"));
node = t.memberExpression(node, t.identifier("prototype"));
}
node = b.memberExpression(node, b.identifier(methodName));
return b.memberExpression(node, b.identifier("call"));
node = t.memberExpression(node, t.identifier(methodName));
return t.memberExpression(node, t.identifier("call"));
}
} else if (t.isMemberExpression(parent) && !methodNode.static) {
// super.test -> ClassName.prototype.test
return b.memberExpression(superName, b.identifier("prototype"));
return t.memberExpression(superName, t.identifier("prototype"));
} else {
return superName;
}
};
var replaceInstanceSuperReferences = function (superName, method, methodNode, methodName) {
superName = superName || b.identifier("Function");
superName = superName || t.identifier("Function");
traverse(method, function (node, parent) {
if (t.isIdentifier(node) && node.name === "super") {
@ -177,7 +176,7 @@ var replaceInstanceSuperReferences = function (superName, method, methodNode, me
// super.test(); -> ClassName.prototype.MethodName.call(this);
callee.property.name = callee.property.name + ".call";
node.arguments.unshift(b.thisExpression());
node.arguments.unshift(t.thisExpression());
}
});
};

View File

@ -1,5 +1,5 @@
var util = require("../util");
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
exports.ObjectExpression = function (node, parent, file) {
@ -33,10 +33,10 @@ exports.ObjectExpression = function (node, parent, file) {
_.each(computed, function (prop) {
containerBody.unshift(
b.expressionStatement(
b.assignmentExpression(
t.expressionStatement(
t.assignmentExpression(
"=",
b.memberExpression(objId, prop.key, true),
t.memberExpression(objId, prop.key, true),
prop.value
)
)

View File

@ -1,13 +1,12 @@
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
var buildVariableAssign = function (kind, id, init) {
if (kind === false) {
return b.expressionStatement(b.assignmentExpression("=", id, init));
return t.expressionStatement(t.assignmentExpression("=", id, init));
} else {
return b.variableDeclaration(kind, [
b.variableDeclarator(id, init)
return t.variableDeclaration(kind, [
t.variableDeclarator(id, init)
]);
}
};
@ -27,7 +26,7 @@ var push = function (kind, nodes, elem, parentId) {
var pushObjectPattern = function (kind, nodes, pattern, parentId) {
_.each(pattern.properties, function (prop) {
var pattern2 = prop.value;
var patternId2 = b.memberExpression(parentId, prop.key);
var patternId2 = t.memberExpression(parentId, prop.key);
if (t.isPattern(pattern2)) {
push(kind, nodes, pattern2, patternId2);
@ -41,17 +40,17 @@ var pushArrayPattern = function (kind, nodes, pattern, parentId) {
_.each(pattern.elements, function (elem, i) {
if (!elem) return;
var newPatternId = b.memberExpression(parentId, b.literal(i), true);
var newPatternId = t.memberExpression(parentId, t.literal(i), true);
push(kind, nodes, elem, newPatternId);
});
};
var pushPattern = function (kind, nodes, pattern, parentId, file) {
if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") {
var key = b.identifier(file.generateUid("ref"));
var key = t.identifier(file.generateUid("ref"));
nodes.push(b.variableDeclaration("var", [
b.variableDeclarator(key, parentId)
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(key, parentId)
]));
parentId = key;
@ -68,9 +67,9 @@ exports.ForOfStatement = function (node, parent, file) {
var pattern = declar.declarations[0].id;
if (!t.isPattern(pattern)) return;
var key = b.identifier(file.generateUid("ref"));
node.left = b.variableDeclaration(declar.kind, [
b.variableDeclarator(key, null)
var key = t.identifier(file.generateUid("ref"));
node.left = t.variableDeclaration(declar.kind, [
t.variableDeclarator(key, null)
]);
var nodes = [];
@ -92,7 +91,7 @@ exports.Function = function (node, parent, file) {
if (!t.isPattern(pattern)) return pattern;
hasDestructuring = true;
var parentId = b.identifier(file.generateUid("ref"));
var parentId = t.identifier(file.generateUid("ref"));
pushPattern("var", nodes, pattern, parentId, file);
return parentId;
});
@ -113,9 +112,9 @@ exports.ExpressionStatement = function (node, parent, file) {
var nodes = [];
var ref = b.identifier(file.generateUid("ref"));
nodes.push(b.variableDeclaration("var", [
b.variableDeclarator(ref, expr.right)
var ref = t.identifier(file.generateUid("ref"));
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(ref, expr.right)
]));
push(false, nodes, expr.left, ref);
@ -151,7 +150,7 @@ exports.VariableDeclaration = function (node, parent, file) {
var declar;
_.each(nodes, function (node) {
declar = declar || b.variableDeclaration(node.kind, []);
declar = declar || t.variableDeclaration(node.kind, []);
if (node.type !== "VariableDeclaration" && declar.kind !== node.kind) {
throw file.errorWithNode(node, "Cannot use this node within the current parent");

View File

@ -1,19 +1,18 @@
var util = require("../util");
var t = require("../types");
var b = require("../builders");
exports.ForOfStatement = function (node, parent, file) {
var left = node.left;
var declar;
var stepKey = b.identifier(file.generateUid("step"));
var stepValueId = b.memberExpression(stepKey, b.identifier("value"));
var stepKey = t.identifier(file.generateUid("step"));
var stepValueId = t.memberExpression(stepKey, t.identifier("value"));
if (t.isIdentifier(left)) {
declar = b.expressionStatement(b.assignmentExpression("=", left, stepValueId));
declar = t.expressionStatement(t.assignmentExpression("=", left, stepValueId));
} else if (t.isVariableDeclaration(left)) {
declar = b.variableDeclaration(left.kind, [
b.variableDeclarator(left.declarations[0].id, stepValueId)
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(left.declarations[0].id, stepValueId)
]);
} else {
return;

View File

@ -2,7 +2,6 @@
// https://github.com/RReverser/jsx-transpiler
var esutils = require("esutils");
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
@ -18,8 +17,8 @@ exports.Program = function (node, parent, file) {
});
// prebuilding AST node
file.jsx = jsx.split(".").map(b.identifier).reduce(function (object, property) {
return b.memberExpression(object, property);
file.jsx = jsx.split(".").map(t.identifier).reduce(function (object, property) {
return t.memberExpression(object, property);
});
};
@ -27,7 +26,7 @@ exports.XJSIdentifier = function (node) {
if (esutils.keyword.isIdentifierName(node.name)) {
node.type = "Identifier";
} else {
return b.literal(node.name);
return t.literal(node.name);
}
};
@ -42,19 +41,14 @@ exports.XJSMemberExpression = {
}
};
exports.XJSEmptyExpression = function (node) {
node.value = null;
node.type = "Literal";
};
exports.XJSExpressionContainer = function (node) {
return node.expression;
};
exports.XJSAttribute = {
exit: function (node) {
var value = node.value || b.literal(true);
var propNode = b.property("init", node.name, value);
var value = node.value || t.literal(true);
var propNode = t.property("init", node.name, value);
t.inherits(propNode, node);
return propNode;
}
@ -68,18 +62,18 @@ exports.XJSOpeningElement = {
var tagName = tagExpr.name;
if (/[a-z]/.exec(tagName[0]) || _.contains(tagName, "-")) {
tagExpr = b.memberExpression(file.jsx, tagExpr);
tagExpr = t.memberExpression(file.jsx, tagExpr);
}
}
var props = node.attributes;
if (props.length) {
props = b.objectExpression(props);
props = t.objectExpression(props);
} else {
props = b.literal(null);
props = t.literal(null);
}
return b.callExpression(tagExpr, [props]);
return t.callExpression(tagExpr, [props]);
}
};

View File

@ -1,7 +1,6 @@
var traverse = require("../traverse");
var util = require("../util");
var t = require("../types");
var b = require("../builders");
var _ = require("lodash");
exports.VariableDeclaration = function (node, parent, file) {
@ -12,7 +11,7 @@ exports.VariableDeclaration = function (node, parent, file) {
_.each(node.declarations, function (declar) {
_.each(util.getIds(declar.id), function (id) {
ids[id] = b.identifier(file.generateUid(id));
ids[id] = t.identifier(file.generateUid(id));
});
});
@ -43,11 +42,11 @@ exports.VariableDeclaration = function (node, parent, file) {
if (t.isFunctionDeclaration(node)) {
throw new Error("`FunctionDeclaration`s that use `let` and `constant` references aren't allowed outside of the root scope");
} else {
var func = b.functionExpression(null, letReferences, b.blockStatement([
b.returnStatement(node)
var func = t.functionExpression(null, letReferences, t.blockStatement([
t.returnStatement(node)
]));
func._aliasFunction = true;
return b.callExpression(func, letReferences);
return t.callExpression(func, letReferences);
}
} else {
return false;

View File

@ -1,4 +1,3 @@
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
@ -34,7 +33,7 @@ var addDisplayName = function (id, call) {
});
if (safe) {
props.unshift(b.property("init", b.identifier("displayName"), b.literal(id)));
props.unshift(t.property("init", t.identifier("displayName"), t.literal(id)));
}
};

View File

@ -1,6 +1,5 @@
var util = require("../util");
var t = require("../types");
var b = require("../builders");
exports.Function = function (node, parent, file) {
if (!node.rest) return;
@ -16,7 +15,7 @@ exports.Function = function (node, parent, file) {
var template = util.template(templateName, {
SLICE_KEY: file.addDeclaration("slice"),
VARIABLE_NAME: rest,
SLICE_ARG: b.literal(node.params.length)
SLICE_ARG: t.literal(node.params.length)
});
template.declarations[0].init.arguments[0]._ignoreAliasFunctions = true;

View File

@ -1,5 +1,4 @@
var util = require("../util");
var b = require("../builders");
var t = require("../types");
var _ = require("lodash");
@ -32,7 +31,7 @@ var build = function (props, file) {
var push = function () {
if (!_props.length) return;
nodes.push(b.arrayExpression(_props));
nodes.push(t.arrayExpression(_props));
_props = [];
};
@ -59,14 +58,14 @@ exports.ArrayExpression = function (node, parent, file) {
if (!nodes.length) return first;
return b.callExpression(b.memberExpression(first, b.identifier("concat")), nodes);
return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
};
exports.CallExpression = function (node, parent, file) {
var args = node.arguments;
if (!hasSpread(args)) return;
var contextLiteral = b.literal(null);
var contextLiteral = t.literal(null);
node.arguments = [];
@ -74,7 +73,7 @@ exports.CallExpression = function (node, parent, file) {
var first = nodes.shift();
if (nodes.length) {
node.arguments.push(b.callExpression(b.memberExpression(first, b.identifier("concat")), nodes));
node.arguments.push(t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes));
} else {
node.arguments.push(first);
}
@ -85,14 +84,14 @@ exports.CallExpression = function (node, parent, file) {
contextLiteral = callee.object;
if (callee.computed) {
callee.object = b.memberExpression(callee.object, callee.property, true);
callee.property = b.identifier("apply");
callee.object = t.memberExpression(callee.object, callee.property, true);
callee.property = t.identifier("apply");
callee.computed = false;
} else {
callee.property = b.memberExpression(callee.property, b.identifier("apply"));
callee.property = t.memberExpression(callee.property, t.identifier("apply"));
}
} else {
node.callee = b.memberExpression(node.callee, b.identifier("apply"));
node.callee = t.memberExpression(node.callee, t.identifier("apply"));
}
node.arguments.unshift(contextLiteral);

View File

@ -1,9 +1,8 @@
var t = require("../types");
var b = require("../builders");
var _ = require("lodash");
var buildBinaryExpression = function (left, right) {
return b.binaryExpression("+", left, right);
return t.binaryExpression("+", left, right);
};
exports.TaggedTemplateExpression = function (node) {
@ -11,22 +10,22 @@ exports.TaggedTemplateExpression = function (node) {
var quasi = node.quasi;
var strings = quasi.quasis.map(function (elem) {
return b.literal(elem.value.raw);
return t.literal(elem.value.raw);
});
args.push(b.arrayExpression(strings));
args.push(t.arrayExpression(strings));
_.each(quasi.expressions, function (expr) {
args.push(expr);
});
return b.callExpression(node.tag, args);
return t.callExpression(node.tag, args);
};
exports.TemplateLiteral = function (node) {
var nodes = [];
_.each(node.quasis, function (elem) {
nodes.push(b.literal(elem.value.raw));
nodes.push(t.literal(elem.value.raw));
var expr = node.expressions.shift();
if (expr) nodes.push(expr);

View File

@ -1,4 +1,4 @@
var b = require("../builders");
var t = require("../types");
module.exports = function (ast) {
var body = ast.program.body;
@ -7,6 +7,6 @@ module.exports = function (ast) {
var noStrict = !first || first.type !== "ExpressionStatement" || first.expression.type !== "Literal" || first.expression.value !== "use strict";
if (noStrict) {
body.unshift(b.expressionStatement(b.literal("use strict")));
body.unshift(t.expressionStatement(t.literal("use strict")));
}
};

View File

@ -0,0 +1,22 @@
{
"ArrowFunctionExpression": ["Function"],
"FunctionDeclaration": ["Function"],
"FunctionExpression": ["Function"],
"LogicalExpression": ["Binary"],
"BinaryExpression": ["Binary"],
"UnaryExpression": ["UnaryLike"],
"SpreadProperty": ["UnaryLike"],
"SpreadElement": ["UnaryLike"],
"ClassDeclaration": ["Class"],
"ClassExpression": ["Class"],
"ForOfStatement": ["For"],
"ForInStatement": ["For"],
"ForStatement": ["For"],
"ObjectPattern": ["Pattern"],
"ArrayPattern": ["Pattern"]
}

View File

@ -0,0 +1,18 @@
{
"ArrayExpression": ["elements"],
"AssignmentExpression": ["operator", "left", "right"],
"BinaryExpression": ["operator", "left", "right"],
"BlockStatement": ["body"],
"CallExpression": ["callee", "arguments"],
"ExpressionStatement": ["expression"],
"FunctionExpression": ["id", "params", "body"],
"Identifier": ["name"],
"IfStatement": ["test", "consequent", "alternate"],
"Literal": ["value"],
"MemberExpression": ["object", "property", "computed"],
"ObjectExpression": ["properties"],
"Property": ["kind", "key", "value"],
"ReturnStatement": ["argument"],
"VariableDeclaration": ["kind", "declarations"],
"VariableDeclarator": ["id", "init"]
}

View File

@ -1,5 +1,4 @@
var traverse = require("./traverse");
var b = require("./builders");
var traverse = require("../traverse");
var n = require("acorn-ast-types").namedTypes;
var _ = require("lodash");
@ -13,30 +12,41 @@ _.each(traverse.VISITOR_KEYS, function (keys, type) {
};
});
var buildIs = function (isKey, typeKey, types) {
t[typeKey + "_TYPES"] = types;
//
t["is" + isKey] = function (node) {
return node && _.contains(types, node.type);
t.BUILDER_KEYS = _.defaults(require("./builder-keys"), traverse.VISITOR_KEYS);
_.each(t.BUILDER_KEYS, function (keys, type) {
t[type[0].toLowerCase() + type.slice(1)] = function () {
var args = arguments;
var node = { type: type };
_.each(keys, function (key, i) {
node[key] = args[i];
});
return node;
};
};
buildIs("Function", "FUNCTION", ["ArrowFunctionExpression", "FunctionDeclaration", "FunctionExpression"]);
buildIs("Class", "CLASS", ["ClassDeclaration", "ClassExpression"]);
buildIs("Pattern", "PATTERN", ["ArrayPattern", "ObjectPattern"]);
buildIs("Binary", "BINARY", ["BinaryExpression", "LogicalExpression"]);
buildIs("UnaryLike", "UNARY", ["UnaryExpression", "SpreadElement", "SpreadProperty"]);
});
//
t.aliases = {
ArrowFunctionExpression: ["Function"],
FunctionDeclaration: ["Function"],
FunctionExpression: ["Function"],
t.ALIAS_KEYS = require("./alias-keys");
ClassDeclaration: ["Class"],
ClassExpression: ["Class"]
};
var _aliases = {};
_.each(t.ALIAS_KEYS, function (aliases, type) {
_.each(aliases, function (alias) {
var types = _aliases[alias] = _aliases[alias] || [];
types.push(type);
});
});
_.each(_aliases, function (types, type) {
t[type.toUpperCase() + "_TYPES"] = types;
t["is" + type] = function (node) {
return node && _.contains(types, node.type);
};
});
//
@ -70,16 +80,16 @@ t.toBlock = function (node, parent) {
if (!_.isArray(node)) {
if (!n.Statement.check(node)) {
if (t.isFunction(parent)) {
node = b.returnStatement(node);
node = t.returnStatement(node);
} else {
node = b.expressionStatement(node);
node = t.expressionStatement(node);
}
}
node = [node];
}
return b.blockStatement(node);
return t.blockStatement(node);
};
t.inherits = function (child, parent) {
@ -101,7 +111,11 @@ t.needsWhitespace = function (node, expression) {
//
if (t.isFunction(node) || t.isClass(node)) {
if (t.isFunction(node) || t.isClass(node) || t.isFor(node) || t.isSwitchStatement(node)) {
return true;
}
if (t.isIfStatement(node) && t.isBlockStatement(node.consequent)) {
return true;
}
@ -128,6 +142,10 @@ t.needsWhitespace = function (node, expression) {
exprs = node.elements;
}
if (t.isObjectExpression(node)) {
exprs = node.properties;
}
return exprs.some(function (expr) {
return t.needsWhitespace(expr, true);
});

View File

@ -5,7 +5,6 @@ var path = require("path");
var util = require("util");
var fs = require("fs");
var t = require("./types");
var b = require("./builders");
var _ = require("lodash");
_.extend(estraverse.VisitorKeys, traverse.VISITOR_KEYS);
@ -37,7 +36,7 @@ exports.getUid = function (parent, file) {
if (t.isIdentifier(node)) id = node.name;
return b.identifier(file.generateUid(id));
return t.identifier(file.generateUid(id));
};
exports.isAbsolute = function (loc) {
@ -100,17 +99,17 @@ exports.pushMutatorMap = function (mutatorMap, key, kind, method) {
};
exports.buildDefineProperties = function (mutatorMap) {
var objExpr = b.objectExpression([]);
var objExpr = t.objectExpression([]);
_.each(mutatorMap, function (map, key) {
var mapNode = b.objectExpression([]);
var mapNode = t.objectExpression([]);
var propNode = b.property("init", b.identifier(key), mapNode);
var propNode = t.property("init", t.identifier(key), mapNode);
_.each(map, function (node, key) {
node = _.clone(node);
if (t.isMethodDefinition(node)) node = node.value;
mapNode.properties.push(b.property("init", b.identifier(key), node));
mapNode.properties.push(t.property("init", t.identifier(key), node));
});
objExpr.properties.push(propNode);
@ -250,7 +249,7 @@ try {
if (!fs.existsSync(templatesLoc)) {
throw new Error("no templates directory - this is most likely the result" +
" of a broken `npm publish`. Please report to " +
"https://github.com/sebmck/6to5/issues");
"https://githut.com/sebmck/6to5/issues");
}
_.each(fs.readdirSync(templatesLoc), function (name) {