use t.is* methods to nicen up code
This commit is contained in:
parent
47ee2cc99f
commit
620e5791af
@ -1,10 +1,11 @@
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.UnaryExpression = function (node, print) {
|
||||
this.push(node.operator);
|
||||
|
||||
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(" ");
|
||||
}
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.XJSAttribute = function (node, print) {
|
||||
@ -45,7 +46,7 @@ exports.XJSElement = function (node, print) {
|
||||
|
||||
this.indent();
|
||||
_.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)) {
|
||||
return self.push(child.value.replace(/^\s+|\s+$/g, ""));
|
||||
} else if (/\n/.test(child.value)) {
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
var t = require("../types");
|
||||
|
||||
exports._params = function (node, print) {
|
||||
var self = this;
|
||||
|
||||
@ -79,7 +81,7 @@ exports.FunctionExpression = 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]);
|
||||
} else {
|
||||
this._params(node, print);
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ImportSpecifier =
|
||||
@ -21,7 +22,7 @@ exports.ExportDeclaration = function (node, print) {
|
||||
if (node.default) {
|
||||
this.push(" default");
|
||||
} else if (specifiers && specifiers.length > 0) {
|
||||
if (specifiers.length === 1 && specifiers[0].type === "ExportBatchSpecifier") {
|
||||
if (specifiers.length === 1 && t.isExportBatchSpecifier(specifiers[0])) {
|
||||
this.push(" *");
|
||||
} else {
|
||||
this.push(" { ");
|
||||
|
||||
@ -62,7 +62,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
|
||||
var ref;
|
||||
|
||||
if (specifier.type === "ImportBatchSpecifier") {
|
||||
if (t.isImportBatchSpecifier(specifier)) {
|
||||
// import * as bar from "foo";
|
||||
ref = this._push(node);
|
||||
} else {
|
||||
|
||||
@ -53,7 +53,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
|
||||
}, true));
|
||||
} else {
|
||||
var id = declar.id;
|
||||
if (declar.type === "VariableDeclaration") {
|
||||
if (t.isVariableDeclaration(declar)) {
|
||||
id = declar.declarations[0].id;
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
|
||||
|
||||
nodes.push(declar);
|
||||
|
||||
if (declar.type === "FunctionDeclaration") {
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
assign._blockHoist = true;
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
if (node.source) {
|
||||
if (specifier.type === "ExportBatchSpecifier") {
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
nodes.push(util.template("exports-wildcard", {
|
||||
OBJECT: getRef()
|
||||
|
||||
@ -39,9 +39,9 @@ var go = function (getBody, node, file) {
|
||||
|
||||
var getId;
|
||||
|
||||
if (node.type === "Identifier" && node.name === "arguments") {
|
||||
if (t.isIdentifier(node) && node.name === "arguments") {
|
||||
getId = getArgumentsId;
|
||||
} else if (node.type === "ThisExpression") {
|
||||
} else if (t.isThisExpression(node)) {
|
||||
getId = getThisId;
|
||||
} else {
|
||||
return;
|
||||
|
||||
@ -15,7 +15,7 @@ exports.ClassExpression = function (node, parent, file) {
|
||||
};
|
||||
|
||||
var getMemberExpressionObject = function (node) {
|
||||
while (node.type === "MemberExpression") {
|
||||
while (t.isMemberExpression(node)) {
|
||||
node = node.object;
|
||||
}
|
||||
return node;
|
||||
@ -29,9 +29,9 @@ var buildClass = function (node, file) {
|
||||
var superClassCallee = node.superClass;
|
||||
|
||||
if (superName) {
|
||||
if (superName.type === "MemberExpression") {
|
||||
if (t.isMemberExpression(superName)) {
|
||||
superClassArgument = superClassCallee = getMemberExpressionObject(superName);
|
||||
} else if (superName.type !== "Identifier") {
|
||||
} else if (t.isIdentifier(superName)) {
|
||||
superClassArgument = superName;
|
||||
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) {
|
||||
if (parent.property === node) {
|
||||
return;
|
||||
} else if (parent.type === "CallExpression" && parent.callee === node) {
|
||||
} else if (t.isCallExpression(parent) && parent.callee === node) {
|
||||
// super(); -> ClassName.prototype.MethodName.call(this);
|
||||
parent.arguments.unshift(b.thisExpression());
|
||||
|
||||
@ -156,7 +156,7 @@ var superIdentifier = function (superName, methodNode, methodName, node, parent)
|
||||
node = b.memberExpression(node, b.identifier(methodName));
|
||||
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
|
||||
return b.memberExpression(superName, b.identifier("prototype"));
|
||||
} else {
|
||||
@ -168,11 +168,11 @@ var replaceInstanceSuperReferences = function (superName, method, methodNode, me
|
||||
superName = superName || b.identifier("Function");
|
||||
|
||||
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);
|
||||
} else if (node.type === "CallExpression") {
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (callee.type !== "MemberExpression") return;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.Program =
|
||||
@ -16,7 +17,7 @@ exports.ForStatement = function (node, parent, file) {
|
||||
};
|
||||
|
||||
_.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(util.getIds(declar.id), function (name) {
|
||||
check(declar, name);
|
||||
@ -34,11 +35,9 @@ exports.ForStatement = function (node, parent, file) {
|
||||
traverse(node, function (child) {
|
||||
if (child._ignoreConstant) return;
|
||||
|
||||
if (child.type === "VariableDeclarator" ||
|
||||
child.type === "FunctionDeclaration" ||
|
||||
child.type === "ClassDeclaration") {
|
||||
if (t.isVariableDeclarator(child) || t.isFunctionDeclaration(child) || t.isClassDeclaration(child)) {
|
||||
check(child, child.id.name);
|
||||
} else if (child.type === "AssignmentExpression") {
|
||||
} else if (t.isAssignmentExpression(child)) {
|
||||
check(child, child.left.name);
|
||||
}
|
||||
});
|
||||
|
||||
@ -13,11 +13,11 @@ var buildVariableAssign = function (kind, id, init) {
|
||||
};
|
||||
|
||||
var push = function (kind, nodes, elem, parentId) {
|
||||
if (elem.type === "ObjectPattern") {
|
||||
if (t.isObjectPattern(elem)) {
|
||||
pushObjectPattern(kind, nodes, elem, parentId);
|
||||
} else if (elem.type === "ArrayPattern") {
|
||||
} else if (t.isArrayPattern(elem)) {
|
||||
pushArrayPattern(kind, nodes, elem, parentId);
|
||||
} else if (elem.type === "MemberExpression") {
|
||||
} else if (t.isMemberExpression(elem)) {
|
||||
nodes.push(buildVariableAssign(false, elem, parentId));
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(kind, elem, parentId));
|
||||
@ -124,7 +124,7 @@ exports.ExpressionStatement = function (node, parent, file) {
|
||||
};
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, file) {
|
||||
if (parent.type === "ForInStatement") return;
|
||||
if (t.isForInStatement(parent)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
|
||||
@ -9,9 +9,9 @@ exports.ForOfStatement = function (node, parent, file) {
|
||||
var stepKey = b.identifier(file.generateUid("step"));
|
||||
var stepValueId = b.memberExpression(stepKey, b.identifier("value"));
|
||||
|
||||
if (left.type === "Identifier") {
|
||||
if (t.isIdentifier(left)) {
|
||||
declar = b.expressionStatement(b.assignmentExpression("=", left, stepValueId));
|
||||
} else if (left.type === "VariableDeclaration") {
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
declar = b.variableDeclaration(left.kind, [
|
||||
b.variableDeclarator(left.declarations[0].id, stepValueId)
|
||||
]);
|
||||
|
||||
@ -37,7 +37,7 @@ exports.XJSNamespacedName = function () {
|
||||
|
||||
exports.XJSMemberExpression = {
|
||||
exit: function (node) {
|
||||
node.computed = node.property.type === "Literal";
|
||||
node.computed = t.isLiteral(node.property);
|
||||
node.type = "MemberExpression";
|
||||
}
|
||||
};
|
||||
@ -64,7 +64,7 @@ exports.XJSOpeningElement = {
|
||||
exit: function (node, parent, file) {
|
||||
var tagExpr = node.name;
|
||||
|
||||
if (tagExpr.type === "Identifier") {
|
||||
if (t.isIdentifier(tagExpr)) {
|
||||
var tagName = tagExpr.name;
|
||||
|
||||
if (/[a-z]/.exec(tagName[0]) || _.contains(tagName, "-")) {
|
||||
|
||||
@ -27,7 +27,7 @@ exports.VariableDeclaration = function (node, parent, file) {
|
||||
if (t.isReferenced(node, parent)) return id;
|
||||
};
|
||||
|
||||
var isProgram = parent.type === "Program";
|
||||
var isProgram = t.isProgram(parent);
|
||||
|
||||
var replace = function (node, parent) {
|
||||
if (!isProgram && _.contains(t.FUNCTION_TYPES, node.type)) {
|
||||
@ -40,7 +40,7 @@ exports.VariableDeclaration = function (node, parent, file) {
|
||||
});
|
||||
|
||||
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");
|
||||
} else {
|
||||
var func = b.functionExpression(null, letReferences, b.blockStatement([
|
||||
|
||||
11
lib/6to5/transformers/react.js
vendored
11
lib/6to5/transformers/react.js
vendored
@ -1,4 +1,5 @@
|
||||
var b = require("../builders");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var addDisplayName = function (id, call) {
|
||||
@ -42,22 +43,22 @@ exports.Property =
|
||||
exports.VariableDeclarator = function (node) {
|
||||
var left, right;
|
||||
|
||||
if (node.type === "AssignmentExpression") {
|
||||
if (t.isAssignmentExpression(node)) {
|
||||
left = node.left;
|
||||
right = node.right;
|
||||
} else if (node.type === "Property") {
|
||||
} else if (t.isProperty(node)) {
|
||||
left = node.key;
|
||||
right = node.value;
|
||||
} else if (node.type === "VariableDeclarator") {
|
||||
} else if (t.isVariableDeclarator(node)) {
|
||||
left = node.id;
|
||||
right = node.init;
|
||||
}
|
||||
|
||||
if (left && left.type === "MemberExpression") {
|
||||
if (t.isMemberExpression(left)) {
|
||||
left = left.property;
|
||||
}
|
||||
|
||||
if (left && left.type === "Identifier") {
|
||||
if (t.isIdentifier(left)) {
|
||||
addDisplayName(left.name, right);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
var util = require("../util");
|
||||
var b = require("../builders");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var getSpreadLiteral = function (spread, file) {
|
||||
@ -16,7 +17,7 @@ var getSpreadLiteral = function (spread, file) {
|
||||
var hasSpread = function (nodes) {
|
||||
var has = false;
|
||||
_.each(nodes, function (node) {
|
||||
if (node.type === "SpreadElement") {
|
||||
if (t.isSpreadElement(node)) {
|
||||
has = true;
|
||||
return false;
|
||||
}
|
||||
@ -36,7 +37,7 @@ var build = function (props, file) {
|
||||
};
|
||||
|
||||
_.each(props, function (prop) {
|
||||
if (prop.type === "SpreadElement") {
|
||||
if (t.isSpreadElement(prop)) {
|
||||
push();
|
||||
nodes.push(getSpreadLiteral(prop, file));
|
||||
} else {
|
||||
@ -80,7 +81,7 @@ exports.CallExpression = function (node, parent, file) {
|
||||
|
||||
var callee = node.callee;
|
||||
|
||||
if (callee.type === "MemberExpression") {
|
||||
if (t.isMemberExpression(callee)) {
|
||||
contextLiteral = callee.object;
|
||||
|
||||
if (callee.computed) {
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
var t = require("../types");
|
||||
var b = require("../builders");
|
||||
var _ = require("lodash");
|
||||
|
||||
@ -34,7 +35,7 @@ exports.TemplateLiteral = function (node) {
|
||||
if (nodes.length > 1) {
|
||||
// remove redundant '' at the end of the expression
|
||||
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());
|
||||
|
||||
|
||||
@ -196,11 +196,11 @@ t.needsParans = function (node, parent) {
|
||||
if (t.isYieldExpression(node)) {
|
||||
return t.isBinary(parent)
|
||||
|| t.isUnaryLike(parent)
|
||||
|| parent.type === "CallExpression"
|
||||
|| parent.type === "MemberExpression"
|
||||
|| parent.type === "NewExpression"
|
||||
|| parent.type === "ConditionalExpression"
|
||||
|| parent.type === "YieldExpression";
|
||||
|| t.isCallExpression(parent)
|
||||
|| t.isMemberExpression(parent)
|
||||
|| t.isNewExpression(parent)
|
||||
|| t.isConditionalExpression(parent)
|
||||
|| t.isYieldExpression(parent);
|
||||
}
|
||||
|
||||
if (t.isNewExpression(parent) && parent.callee === node) {
|
||||
|
||||
@ -4,6 +4,7 @@ var acorn = require("acorn-6to5");
|
||||
var path = require("path");
|
||||
var util = require("util");
|
||||
var fs = require("fs");
|
||||
var t = require("./types");
|
||||
var b = require("./builders");
|
||||
var _ = require("lodash");
|
||||
|
||||
@ -26,17 +27,15 @@ exports.list = function (val) {
|
||||
exports.getUid = function (parent, file) {
|
||||
var node;
|
||||
|
||||
if (parent.type === "AssignmentExpression") {
|
||||
if (t.isAssignmentExpression(parent)) {
|
||||
node = parent.left;
|
||||
} else if (parent.type === "VariableDeclarator") {
|
||||
} else if (t.isVariableDeclarator(parent)) {
|
||||
node = parent.id;
|
||||
}
|
||||
|
||||
var id = "ref";
|
||||
|
||||
if (node && node.type === "Identifier") {
|
||||
id = node.name;
|
||||
}
|
||||
if (t.isIdentifier(node)) id = node.name;
|
||||
|
||||
return b.identifier(file.generateUid(id));
|
||||
};
|
||||
@ -55,13 +54,13 @@ exports.getIds = function (node) {
|
||||
while (search.length) {
|
||||
var id = search.shift();
|
||||
|
||||
if (id.type === "Identifier") {
|
||||
if (t.isIdentifier(id)) {
|
||||
ids.push(id.name);
|
||||
} else if (id.type === "ArrayPattern") {
|
||||
} else if (t.isArrayPattern(id)) {
|
||||
_.each(id.elements, function (elem) {
|
||||
search.push(elem);
|
||||
});
|
||||
} else if (id.type === "ObjectPattern") {
|
||||
} else if (t.isObjectPattern(id)) {
|
||||
_.each(id.properties, function (prop) {
|
||||
search.push(prop.value);
|
||||
});
|
||||
@ -110,7 +109,7 @@ exports.buildDefineProperties = function (mutatorMap) {
|
||||
|
||||
_.each(map, function (node, key) {
|
||||
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));
|
||||
});
|
||||
|
||||
@ -128,7 +127,7 @@ exports.template = function (name, nodes, keepExpression) {
|
||||
|
||||
if (!_.isEmpty(nodes)) {
|
||||
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];
|
||||
if (_.isString(newNode)) {
|
||||
node.name = newNode;
|
||||
@ -141,7 +140,7 @@ exports.template = function (name, nodes, keepExpression) {
|
||||
|
||||
var node = template.body[0];
|
||||
|
||||
if (!keepExpression && node.type === "ExpressionStatement") {
|
||||
if (!keepExpression && t.isExpressionStatement(node)) {
|
||||
return node.expression;
|
||||
} else {
|
||||
return node;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user