Skip to content

Instantly share code, notes, and snippets.

@program247365
Created June 8, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save program247365/fba52fb5017a2d602b77 to your computer and use it in GitHub Desktop.
Save program247365/fba52fb5017a2d602b77 to your computer and use it in GitHub Desktop.
Console Logging with Esprima
function addLogging(code) {
var ast = esprima.parse(code);
estraverse.traverse(ast, {
enter: function(node, parent) {
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
addBeforeCode(node);
}
}
});
return escodegen.generate(ast);
}
function addBeforeCode(node) {
var name = node.id ? node.id.name : '<anonymous function>';
var beforeCode = "console.log('Entering " + name + "()');";
var beforeNodes = esprima.parse(beforeCode).body;
node.body.body = beforeNodes.concat(node.body.body);
}
@program247365
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment