use t.is* methods to nicen up code

This commit is contained in:
Sebastian McKenzie 2014-11-03 18:10:52 +11:00
parent 47ee2cc99f
commit 620e5791af
18 changed files with 64 additions and 58 deletions

View File

@ -1,10 +1,11 @@
var t = require("../types");
var _ = require("lodash"); var _ = require("lodash");
exports.UnaryExpression = function (node, print) { exports.UnaryExpression = function (node, print) {
this.push(node.operator); this.push(node.operator);
var arg = node.argument; var arg = node.argument;
if (/[a-z]$/.test(node.operator) || arg.type === "UpdateExpression" || arg.type === "UnaryExpression") { if (/[a-z]$/.test(node.operator) || t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) {
this.push(" "); this.push(" ");
} }

View File

@ -1,3 +1,4 @@
var t = require("../types");
var _ = require("lodash"); var _ = require("lodash");
exports.XJSAttribute = function (node, print) { exports.XJSAttribute = function (node, print) {
@ -45,7 +46,7 @@ exports.XJSElement = function (node, print) {
this.indent(); this.indent();
_.each(node.children, function (child) { _.each(node.children, function (child) {
if (child.type === "Literal" && typeof child.value === "string") { if (t.isLiteral(child) && typeof child.value === "string") {
if (/\S/.test(child.value)) { if (/\S/.test(child.value)) {
return self.push(child.value.replace(/^\s+|\s+$/g, "")); return self.push(child.value.replace(/^\s+|\s+$/g, ""));
} else if (/\n/.test(child.value)) { } else if (/\n/.test(child.value)) {

View File

@ -1,3 +1,5 @@
var t = require("../types");
exports._params = function (node, print) { exports._params = function (node, print) {
var self = this; var self = this;
@ -79,7 +81,7 @@ exports.FunctionExpression = function (node, print) {
}; };
exports.ArrowFunctionExpression = function (node, print) { exports.ArrowFunctionExpression = function (node, print) {
if (node.params.length === 1 && !node.defaults.length && !node.rest && node.params[0].type === "Identifier") { if (node.params.length === 1 && !node.defaults.length && !node.rest && t.isIdentifier(node.params[0])) {
print(node.params[0]); print(node.params[0]);
} else { } else {
this._params(node, print); this._params(node, print);

View File

@ -1,3 +1,4 @@
var t = require("../types");
var _ = require("lodash"); var _ = require("lodash");
exports.ImportSpecifier = exports.ImportSpecifier =
@ -21,7 +22,7 @@ exports.ExportDeclaration = function (node, print) {
if (node.default) { if (node.default) {
this.push(" default"); this.push(" default");
} else if (specifiers && specifiers.length > 0) { } else if (specifiers && specifiers.length > 0) {
if (specifiers.length === 1 && specifiers[0].type === "ExportBatchSpecifier") { if (specifiers.length === 1 && t.isExportBatchSpecifier(specifiers[0])) {
this.push(" *"); this.push(" *");
} else { } else {
this.push(" { "); this.push(" { ");

View File

@ -62,7 +62,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
var ref; var ref;
if (specifier.type === "ImportBatchSpecifier") { if (t.isImportBatchSpecifier(specifier)) {
// import * as bar from "foo"; // import * as bar from "foo";
ref = this._push(node); ref = this._push(node);
} else { } else {

View File

@ -53,7 +53,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
}, true)); }, true));
} else { } else {
var id = declar.id; var id = declar.id;
if (declar.type === "VariableDeclaration") { if (t.isVariableDeclaration(declar)) {
id = declar.declarations[0].id; id = declar.declarations[0].id;
} }
@ -64,7 +64,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
nodes.push(declar); nodes.push(declar);
if (declar.type === "FunctionDeclaration") { if (t.isFunctionDeclaration(declar)) {
assign._blockHoist = true; assign._blockHoist = true;
} }
@ -76,7 +76,7 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node
var variableName = t.getSpecifierName(specifier); var variableName = t.getSpecifierName(specifier);
if (node.source) { if (node.source) {
if (specifier.type === "ExportBatchSpecifier") { if (t.isExportBatchSpecifier(specifier)) {
// export * from "foo"; // export * from "foo";
nodes.push(util.template("exports-wildcard", { nodes.push(util.template("exports-wildcard", {
OBJECT: getRef() OBJECT: getRef()

View File

@ -39,9 +39,9 @@ var go = function (getBody, node, file) {
var getId; var getId;
if (node.type === "Identifier" && node.name === "arguments") { if (t.isIdentifier(node) && node.name === "arguments") {
getId = getArgumentsId; getId = getArgumentsId;
} else if (node.type === "ThisExpression") { } else if (t.isThisExpression(node)) {
getId = getThisId; getId = getThisId;
} else { } else {
return; return;

View File

@ -15,7 +15,7 @@ exports.ClassExpression = function (node, parent, file) {
}; };
var getMemberExpressionObject = function (node) { var getMemberExpressionObject = function (node) {
while (node.type === "MemberExpression") { while (t.isMemberExpression(node)) {
node = node.object; node = node.object;
} }
return node; return node;
@ -29,9 +29,9 @@ var buildClass = function (node, file) {
var superClassCallee = node.superClass; var superClassCallee = node.superClass;
if (superName) { if (superName) {
if (superName.type === "MemberExpression") { if (t.isMemberExpression(superName)) {
superClassArgument = superClassCallee = getMemberExpressionObject(superName); superClassArgument = superClassCallee = getMemberExpressionObject(superName);
} else if (superName.type !== "Identifier") { } else if (t.isIdentifier(superName)) {
superClassArgument = superName; superClassArgument = superName;
superClassCallee = superName = b.identifier(file.generateUid("ref")); superClassCallee = superName = b.identifier(file.generateUid("ref"));
} }
@ -138,7 +138,7 @@ var buildClassBody = function (file, construct, body, className, superName, node
var superIdentifier = function (superName, methodNode, methodName, node, parent) { var superIdentifier = function (superName, methodNode, methodName, node, parent) {
if (parent.property === node) { if (parent.property === node) {
return; return;
} else if (parent.type === "CallExpression" && parent.callee === node) { } else if (t.isCallExpression(parent) && parent.callee === node) {
// super(); -> ClassName.prototype.MethodName.call(this); // super(); -> ClassName.prototype.MethodName.call(this);
parent.arguments.unshift(b.thisExpression()); parent.arguments.unshift(b.thisExpression());
@ -156,7 +156,7 @@ var superIdentifier = function (superName, methodNode, methodName, node, parent)
node = b.memberExpression(node, b.identifier(methodName)); node = b.memberExpression(node, b.identifier(methodName));
return b.memberExpression(node, b.identifier("call")); return b.memberExpression(node, b.identifier("call"));
} }
} else if (parent.type === "MemberExpression" && !methodNode.static) { } else if (t.isMemberExpression(parent) && !methodNode.static) {
// super.test -> ClassName.prototype.test // super.test -> ClassName.prototype.test
return b.memberExpression(superName, b.identifier("prototype")); return b.memberExpression(superName, b.identifier("prototype"));
} else { } else {
@ -168,11 +168,11 @@ var replaceInstanceSuperReferences = function (superName, method, methodNode, me
superName = superName || b.identifier("Function"); superName = superName || b.identifier("Function");
traverse(method, function (node, parent) { traverse(method, function (node, parent) {
if (node.type === "Identifier" && node.name === "super") { if (t.isIdentifier(node) && node.name === "super") {
return superIdentifier(superName, methodNode, methodName, node, parent); return superIdentifier(superName, methodNode, methodName, node, parent);
} else if (node.type === "CallExpression") { } else if (t.isCallExpression(node)) {
var callee = node.callee; var callee = node.callee;
if (callee.type !== "MemberExpression") return; if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return; if (callee.object.name !== "super") return;
// super.test(); -> ClassName.prototype.MethodName.call(this); // super.test(); -> ClassName.prototype.MethodName.call(this);

View File

@ -1,5 +1,6 @@
var traverse = require("../traverse"); var traverse = require("../traverse");
var util = require("../util"); var util = require("../util");
var t = require("../types");
var _ = require("lodash"); var _ = require("lodash");
exports.Program = exports.Program =
@ -16,7 +17,7 @@ exports.ForStatement = function (node, parent, file) {
}; };
_.each(node.body, function (child) { _.each(node.body, function (child) {
if (child && child.type === "VariableDeclaration" && child.kind === "const") { if (child && t.isVariableDeclaration(child) && child.kind === "const") {
_.each(child.declarations, function (declar) { _.each(child.declarations, function (declar) {
_.each(util.getIds(declar.id), function (name) { _.each(util.getIds(declar.id), function (name) {
check(declar, name); check(declar, name);
@ -34,11 +35,9 @@ exports.ForStatement = function (node, parent, file) {
traverse(node, function (child) { traverse(node, function (child) {
if (child._ignoreConstant) return; if (child._ignoreConstant) return;
if (child.type === "VariableDeclarator" || if (t.isVariableDeclarator(child) || t.isFunctionDeclaration(child) || t.isClassDeclaration(child)) {
child.type === "FunctionDeclaration" ||
child.type === "ClassDeclaration") {
check(child, child.id.name); check(child, child.id.name);
} else if (child.type === "AssignmentExpression") { } else if (t.isAssignmentExpression(child)) {
check(child, child.left.name); check(child, child.left.name);
} }
}); });

View File

@ -13,11 +13,11 @@ var buildVariableAssign = function (kind, id, init) {
}; };
var push = function (kind, nodes, elem, parentId) { var push = function (kind, nodes, elem, parentId) {
if (elem.type === "ObjectPattern") { if (t.isObjectPattern(elem)) {
pushObjectPattern(kind, nodes, elem, parentId); pushObjectPattern(kind, nodes, elem, parentId);
} else if (elem.type === "ArrayPattern") { } else if (t.isArrayPattern(elem)) {
pushArrayPattern(kind, nodes, elem, parentId); pushArrayPattern(kind, nodes, elem, parentId);
} else if (elem.type === "MemberExpression") { } else if (t.isMemberExpression(elem)) {
nodes.push(buildVariableAssign(false, elem, parentId)); nodes.push(buildVariableAssign(false, elem, parentId));
} else { } else {
nodes.push(buildVariableAssign(kind, elem, parentId)); nodes.push(buildVariableAssign(kind, elem, parentId));
@ -124,7 +124,7 @@ exports.ExpressionStatement = function (node, parent, file) {
}; };
exports.VariableDeclaration = function (node, parent, file) { exports.VariableDeclaration = function (node, parent, file) {
if (parent.type === "ForInStatement") return; if (t.isForInStatement(parent)) return;
var nodes = []; var nodes = [];

View File

@ -9,9 +9,9 @@ exports.ForOfStatement = function (node, parent, file) {
var stepKey = b.identifier(file.generateUid("step")); var stepKey = b.identifier(file.generateUid("step"));
var stepValueId = b.memberExpression(stepKey, b.identifier("value")); var stepValueId = b.memberExpression(stepKey, b.identifier("value"));
if (left.type === "Identifier") { if (t.isIdentifier(left)) {
declar = b.expressionStatement(b.assignmentExpression("=", left, stepValueId)); declar = b.expressionStatement(b.assignmentExpression("=", left, stepValueId));
} else if (left.type === "VariableDeclaration") { } else if (t.isVariableDeclaration(left)) {
declar = b.variableDeclaration(left.kind, [ declar = b.variableDeclaration(left.kind, [
b.variableDeclarator(left.declarations[0].id, stepValueId) b.variableDeclarator(left.declarations[0].id, stepValueId)
]); ]);

View File

@ -37,7 +37,7 @@ exports.XJSNamespacedName = function () {
exports.XJSMemberExpression = { exports.XJSMemberExpression = {
exit: function (node) { exit: function (node) {
node.computed = node.property.type === "Literal"; node.computed = t.isLiteral(node.property);
node.type = "MemberExpression"; node.type = "MemberExpression";
} }
}; };
@ -64,7 +64,7 @@ exports.XJSOpeningElement = {
exit: function (node, parent, file) { exit: function (node, parent, file) {
var tagExpr = node.name; var tagExpr = node.name;
if (tagExpr.type === "Identifier") { if (t.isIdentifier(tagExpr)) {
var tagName = tagExpr.name; var tagName = tagExpr.name;
if (/[a-z]/.exec(tagName[0]) || _.contains(tagName, "-")) { if (/[a-z]/.exec(tagName[0]) || _.contains(tagName, "-")) {

View File

@ -27,7 +27,7 @@ exports.VariableDeclaration = function (node, parent, file) {
if (t.isReferenced(node, parent)) return id; if (t.isReferenced(node, parent)) return id;
}; };
var isProgram = parent.type === "Program"; var isProgram = t.isProgram(parent);
var replace = function (node, parent) { var replace = function (node, parent) {
if (!isProgram && _.contains(t.FUNCTION_TYPES, node.type)) { if (!isProgram && _.contains(t.FUNCTION_TYPES, node.type)) {
@ -40,7 +40,7 @@ exports.VariableDeclaration = function (node, parent, file) {
}); });
if (letReferences.length) { if (letReferences.length) {
if (node.type === "FunctionDeclaration") { if (t.isFunctionDeclaration(node)) {
throw new Error("`FunctionDeclaration`s that use `let` and `constant` references aren't allowed outside of the root scope"); throw new Error("`FunctionDeclaration`s that use `let` and `constant` references aren't allowed outside of the root scope");
} else { } else {
var func = b.functionExpression(null, letReferences, b.blockStatement([ var func = b.functionExpression(null, letReferences, b.blockStatement([

View File

@ -1,4 +1,5 @@
var b = require("../builders"); var b = require("../builders");
var t = require("../types");
var _ = require("lodash"); var _ = require("lodash");
var addDisplayName = function (id, call) { var addDisplayName = function (id, call) {
@ -42,22 +43,22 @@ exports.Property =
exports.VariableDeclarator = function (node) { exports.VariableDeclarator = function (node) {
var left, right; var left, right;
if (node.type === "AssignmentExpression") { if (t.isAssignmentExpression(node)) {
left = node.left; left = node.left;
right = node.right; right = node.right;
} else if (node.type === "Property") { } else if (t.isProperty(node)) {
left = node.key; left = node.key;
right = node.value; right = node.value;
} else if (node.type === "VariableDeclarator") { } else if (t.isVariableDeclarator(node)) {
left = node.id; left = node.id;
right = node.init; right = node.init;
} }
if (left && left.type === "MemberExpression") { if (t.isMemberExpression(left)) {
left = left.property; left = left.property;
} }
if (left && left.type === "Identifier") { if (t.isIdentifier(left)) {
addDisplayName(left.name, right); addDisplayName(left.name, right);
} }
}; };

View File

@ -1,5 +1,6 @@
var util = require("../util"); var util = require("../util");
var b = require("../builders"); var b = require("../builders");
var t = require("../types");
var _ = require("lodash"); var _ = require("lodash");
var getSpreadLiteral = function (spread, file) { var getSpreadLiteral = function (spread, file) {
@ -16,7 +17,7 @@ var getSpreadLiteral = function (spread, file) {
var hasSpread = function (nodes) { var hasSpread = function (nodes) {
var has = false; var has = false;
_.each(nodes, function (node) { _.each(nodes, function (node) {
if (node.type === "SpreadElement") { if (t.isSpreadElement(node)) {
has = true; has = true;
return false; return false;
} }
@ -36,7 +37,7 @@ var build = function (props, file) {
}; };
_.each(props, function (prop) { _.each(props, function (prop) {
if (prop.type === "SpreadElement") { if (t.isSpreadElement(prop)) {
push(); push();
nodes.push(getSpreadLiteral(prop, file)); nodes.push(getSpreadLiteral(prop, file));
} else { } else {
@ -80,7 +81,7 @@ exports.CallExpression = function (node, parent, file) {
var callee = node.callee; var callee = node.callee;
if (callee.type === "MemberExpression") { if (t.isMemberExpression(callee)) {
contextLiteral = callee.object; contextLiteral = callee.object;
if (callee.computed) { if (callee.computed) {

View File

@ -1,3 +1,4 @@
var t = require("../types");
var b = require("../builders"); var b = require("../builders");
var _ = require("lodash"); var _ = require("lodash");
@ -34,7 +35,7 @@ exports.TemplateLiteral = function (node) {
if (nodes.length > 1) { if (nodes.length > 1) {
// remove redundant '' at the end of the expression // remove redundant '' at the end of the expression
var last = _.last(nodes); var last = _.last(nodes);
if (last.type === "Literal" && last.value === "") nodes.pop(); if (t.isLiteral(last) && last.value === "") nodes.pop();
var root = buildBinaryExpression(nodes.shift(), nodes.shift()); var root = buildBinaryExpression(nodes.shift(), nodes.shift());

View File

@ -196,11 +196,11 @@ t.needsParans = function (node, parent) {
if (t.isYieldExpression(node)) { if (t.isYieldExpression(node)) {
return t.isBinary(parent) return t.isBinary(parent)
|| t.isUnaryLike(parent) || t.isUnaryLike(parent)
|| parent.type === "CallExpression" || t.isCallExpression(parent)
|| parent.type === "MemberExpression" || t.isMemberExpression(parent)
|| parent.type === "NewExpression" || t.isNewExpression(parent)
|| parent.type === "ConditionalExpression" || t.isConditionalExpression(parent)
|| parent.type === "YieldExpression"; || t.isYieldExpression(parent);
} }
if (t.isNewExpression(parent) && parent.callee === node) { if (t.isNewExpression(parent) && parent.callee === node) {

View File

@ -4,6 +4,7 @@ var acorn = require("acorn-6to5");
var path = require("path"); var path = require("path");
var util = require("util"); var util = require("util");
var fs = require("fs"); var fs = require("fs");
var t = require("./types");
var b = require("./builders"); var b = require("./builders");
var _ = require("lodash"); var _ = require("lodash");
@ -26,17 +27,15 @@ exports.list = function (val) {
exports.getUid = function (parent, file) { exports.getUid = function (parent, file) {
var node; var node;
if (parent.type === "AssignmentExpression") { if (t.isAssignmentExpression(parent)) {
node = parent.left; node = parent.left;
} else if (parent.type === "VariableDeclarator") { } else if (t.isVariableDeclarator(parent)) {
node = parent.id; node = parent.id;
} }
var id = "ref"; var id = "ref";
if (node && node.type === "Identifier") { if (t.isIdentifier(node)) id = node.name;
id = node.name;
}
return b.identifier(file.generateUid(id)); return b.identifier(file.generateUid(id));
}; };
@ -55,13 +54,13 @@ exports.getIds = function (node) {
while (search.length) { while (search.length) {
var id = search.shift(); var id = search.shift();
if (id.type === "Identifier") { if (t.isIdentifier(id)) {
ids.push(id.name); ids.push(id.name);
} else if (id.type === "ArrayPattern") { } else if (t.isArrayPattern(id)) {
_.each(id.elements, function (elem) { _.each(id.elements, function (elem) {
search.push(elem); search.push(elem);
}); });
} else if (id.type === "ObjectPattern") { } else if (t.isObjectPattern(id)) {
_.each(id.properties, function (prop) { _.each(id.properties, function (prop) {
search.push(prop.value); search.push(prop.value);
}); });
@ -110,7 +109,7 @@ exports.buildDefineProperties = function (mutatorMap) {
_.each(map, function (node, key) { _.each(map, function (node, key) {
node = _.clone(node); node = _.clone(node);
if (node.type === "MethodDefinition") node = node.value; if (t.isMethodDefinition(node)) node = node.value;
mapNode.properties.push(b.property("init", b.identifier(key), node)); mapNode.properties.push(b.property("init", b.identifier(key), node));
}); });
@ -128,7 +127,7 @@ exports.template = function (name, nodes, keepExpression) {
if (!_.isEmpty(nodes)) { if (!_.isEmpty(nodes)) {
traverse(template, function (node) { traverse(template, function (node) {
if (node.type === "Identifier" && _.has(nodes, node.name)) { if (t.isIdentifier(node) && _.has(nodes, node.name)) {
var newNode = nodes[node.name]; var newNode = nodes[node.name];
if (_.isString(newNode)) { if (_.isString(newNode)) {
node.name = newNode; node.name = newNode;
@ -141,7 +140,7 @@ exports.template = function (name, nodes, keepExpression) {
var node = template.body[0]; var node = template.body[0];
if (!keepExpression && node.type === "ExpressionStatement") { if (!keepExpression && t.isExpressionStatement(node)) {
return node.expression; return node.expression;
} else { } else {
return node; return node;