Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
"use strict";
var _ = require("lodash"),
esprima = require("esprima-fb"),
fs = require("fs"),
glob = require("glob");
function collectModuleNameFromArrayExpression(file, arrayExpression, modules) {
if (!arrayExpression.elements) {
return;
}
arrayExpression.elements
.filter(function (arrayExpressionItem) {
return "Literal" === arrayExpressionItem.type;
})
.forEach(function (literal) {
modules.push({
"file": file,
"name": literal.value
});
});
}
function collectModuleNameFromDefineCallExpression(file, callExpression, modules) {
if (3 === callExpression.arguments.length) {
return collectModuleNameFromArrayExpression(file, callExpression.arguments[1], modules);
} else if (2 === callExpression.arguments.length && "ArrayExpression" === callExpression.arguments[1].type) {
return collectModuleNameFromArrayExpression(file, callExpression.arguments[1], modules);
}
return collectModuleNameFromArrayExpression(file, callExpression.arguments[0], modules);
}
function collectModuleNameFromRequireCallExpression(file, callExpression, modules) {
if (2 === callExpression.arguments.length) {
return collectModuleNameFromArrayExpression(file, callExpression.arguments[0], modules);
}
return collectModuleNameFromArrayExpression(file, callExpression.arguments[1], modules);
}
function traverseAssignmentExpression(file, assignmentExpression, modules) {
traverse(file, assignmentExpression.left, modules);
traverse(file, assignmentExpression.right, modules);
}
function traverseArrayExpression(file, arrayExpression, modules) {
if (arrayExpression.elements) {
arrayExpression.elements.forEach(function (node) {
traverse(file, node, modules);
});
}
}
function traverseArrowFunctionExpression(file, arrowFunctionExpression, modules) {
traverse(file, arrowFunctionExpression.body, modules);
}
function traverseBinaryExpression(file, binaryExpression, modules) {
traverse(file, binaryExpression.right, modules);
}
function traverseBlockStatement(file, blockStatement, modules) {
blockStatement.body.forEach(function (node) {
traverse(file, node, modules);
});
}
function traverseBreakStatement(file, breakStatement, modules) {
}
function traverseCallExpression(file, callExpression, modules) {
if ("define" === callExpression.callee.name) {
collectModuleNameFromDefineCallExpression(file, callExpression, modules);
} else if ("require" === callExpression.callee.name) {
collectModuleNameFromRequireCallExpression(file, callExpression, modules);
}
callExpression.arguments.forEach(function (node) {
traverse(file, node, modules);
});
traverse(file, callExpression.callee, modules);
}
function traverseConditionalExpression(file, conditionalExpression, modules) {
traverse(file, conditionalExpression.test, modules);
traverse(file, conditionalExpression.alternate, modules);
traverse(file, conditionalExpression.consequent, modules);
}
function traverseExpressionStatement(file, statement, modules) {
traverse(file, statement.expression, modules);
}
function traverseForStatement(file, forStatement, modules) {
if (forStatement.init) {
traverse(file, forStatement.init, modules);
}
if (forStatement.test) {
traverse(file, forStatement.test, modules);
}
if (forStatement.update) {
traverse(file, forStatement.update, modules);
}
traverse(file, forStatement.body, modules);
}
function traverseForInStatement(file, forInStatement, modules) {
traverse(file, forInStatement.left, modules);
traverse(file, forInStatement.right, modules);
traverse(file, forInStatement.body, modules);
}
function traverseFunctionDeclaration(file, functionDeclaration, modules) {
traverse(file, functionDeclaration.id, modules);
traverse(file, functionDeclaration.body, modules);
}
function traverseFunctionExpression(file, functionExpression, modules) {
traverse(file, functionExpression.body, modules);
}
function traverseIdentifier(file, identifier, modules) {
}
function traverseIfStatement(file, ifStatement, modules) {
traverse(file, ifStatement.consequent, modules);
if (ifStatement.alternate) {
traverse(file, ifStatement.alternate, modules);
}
}
function traverseLiteral(file, literal, modules) {
}
function traverseLogicalExpression(file, logicalExpresion, modules) {
traverse(file, logicalExpresion.left, modules);
traverse(file, logicalExpresion.right, modules);
}
function traverseMemberExpression(file, memberExpression, modules) {
traverse(file, memberExpression.property, modules);
}
function traverseNewExpression(file, newExpression, modules) {
newExpression.arguments.forEach(function (node) {
traverse(file, node, modules);
});
}
function traverseObjectExpression(file, objectExpression, modules) {
objectExpression.properties.forEach(function (node) {
traverse(file, node, modules);
});
}
function traverseProgram(file, program, modules) {
program.body.forEach(function (node) {
traverse(file, node, modules);
});
}
function traverseProperty(file, property, modules) {
traverse(file, property.value, modules);
}
function traverseReturnStatement(file, returnStatement, modules) {
if (returnStatement.argument) {
traverse(file, returnStatement.argument, modules);
}
}
function traverseThisExpression(file, thisExpression, modules) {
}
function traverseUnaryExpression(file, unaryExpression, modules) {
traverse(file, unaryExpression.argument, modules);
}
function traverseVariableDeclaration(file, variableDeclaration, modules) {
variableDeclaration.declarations.forEach(function (node) {
traverse(file, node, modules);
});
}
function traverseVariableDeclarator(file, variableDeclarator, modules) {
if (variableDeclarator.init) {
traverse(file, variableDeclarator.init, modules);
}
}
function traverseWhileStatement(file, whileStatement, modules) {
traverse(file, whileStatement.test, modules);
traverse(file, whileStatement.body, modules);
}
function traverseXJSAttribute(file, XJSAttribute, modules) {
traverse(file, XJSAttribute.value, modules);
}
function traverseXJSClosingElement(file, XJSClosingElement, modules) {
}
function traverseXJSElement(file, XJSElement, modules) {
traverse(file, XJSElement.openingElement, modules);
XJSElement.children.forEach(function (node) {
traverse(file, node, modules);
});
if (XJSElement.closingElement) {
traverse(file, XJSElement.closingElement, modules);
}
}
function traverseXJSEmptyExpression(file, XJSEmptyExpression, modules) {
}
function traverseXJSExpressionContainer(file, XJSExpressionContainer, modules) {
traverse(file, XJSExpressionContainer.expression, modules);
}
function traverseXJSOpeningElement(file, XJSOpeningElement, modules) {
XJSOpeningElement.attributes.forEach(function (node) {
traverse(file, node, modules);
});
}
function traverseXJSSpreadAttribute(file, XJSSpreadAttribute, modules) {
traverse(file, XJSSpreadAttribute.argument, modules);
}
function traverse(file, node, modules) {
switch (node.type) {
case "AssignmentExpression":
return traverseAssignmentExpression(file, node, modules);
case "ArrayExpression":
return traverseArrayExpression(file, node, modules);
case "ArrowFunctionExpression":
return traverseArrowFunctionExpression(file, node, modules);
case "BinaryExpression":
return traverseBinaryExpression(file, node, modules);
case "BlockStatement":
return traverseBlockStatement(file, node, modules);
case "BreakStatement":
return traverseBreakStatement(file, node, modules);
case "CallExpression":
return traverseCallExpression(file, node, modules);
case "ConditionalExpression":
return traverseConditionalExpression(file, node, modules);
case "ExpressionStatement":
return traverseExpressionStatement(file, node, modules);
case "ForStatement":
return traverseForStatement(file, node, modules);
case "ForInStatement":
return traverseForInStatement(file, node, modules);
case "FunctionDeclaration":
return traverseFunctionDeclaration(file, node, modules);
case "FunctionExpression":
return traverseFunctionExpression(file, node, modules);
case "Identifier":
return traverseIdentifier(file, node, modules);
case "IfStatement":
return traverseIfStatement(file, node, modules);
case "Literal":
return traverseLiteral(file, node, modules);
case "LogicalExpression":
return traverseLogicalExpression(file, node, modules);
case "MemberExpression":
return traverseMemberExpression(file, node, modules);
case "NewExpression":
return traverseNewExpression(file, node, modules);
case "ObjectExpression":
return traverseObjectExpression(file, node, modules);
case "Program":
return traverseProgram(file, node, modules);
case "Property":
return traverseProperty(file, node, modules);
case "ReturnStatement":
return traverseReturnStatement(file, node, modules);
case "ThisExpression":
return traverseThisExpression(file, node, modules);
case "UnaryExpression":
return traverseUnaryExpression(file, node, modules);
case "VariableDeclaration":
return traverseVariableDeclaration(file, node, modules);
case "VariableDeclarator":
return traverseVariableDeclarator(file, node, modules);
case "WhileStatement":
return traverseWhileStatement(file, node, modules);
case "XJSAttribute":
return traverseXJSAttribute(file, node, modules);
case "XJSClosingElement":
return traverseXJSClosingElement(file, node, modules);
case "XJSElement":
return traverseXJSElement(file, node, modules);
case "XJSEmptyExpression":
return traverseXJSEmptyExpression(file, node, modules);
case "XJSExpressionContainer":
return traverseXJSExpressionContainer(file, node, modules);
case "XJSOpeningElement":
return traverseXJSOpeningElement(file, node, modules);
case "XJSSpreadAttribute":
return traverseXJSSpreadAttribute(file, node, modules);
}
}
glob("s/React.jsx/ResponsivePopup.js", function (err, files) {
var allModules = _(files)
.map(function (file) {
var modules = [];
traverse(file, esprima.parse(fs.readFileSync(file)), modules);
return modules;
})
.flatten()
.groupBy(function (module) {
return module.name;
})
.value();
console.log(allModules);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment