Created
June 8, 2015 17:59
-
-
Save program247365/fba52fb5017a2d602b77 to your computer and use it in GitHub Desktop.
Console Logging with Esprima
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: http://kevinridgway.com/discovering-ast-in-javascript/