Created
October 18, 2012 02:17
-
-
Save alex-seville/3909508 to your computer and use it in GitHub Desktop.
instrumenting code with falafel
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
var falafel = require('falafel'); | |
var intro = "\"tracking code start\";\n"; | |
var checkForOneLiner = function (node) { | |
if (linesToAddBrackets.indexOf(node.type) > -1){ | |
var bracketsExistObject = node.consequent || node.body; | |
if( bracketsExistObject && bracketsExistObject.type != "BlockStatement") { | |
bracketsExistObject.update("{\n"+bracketsExistObject.source()+"\n}"); | |
} | |
} | |
if (linesToAddTracking.indexOf(node.type) > -1){ | |
if (node.type == "VariableDeclaration" && node.parent.type == "ForStatement"){ | |
return; | |
} | |
node.update("\"tracking code:"+node.type+"\";\n"+node.source()); | |
} | |
}; | |
var linesToAddTracking = ["ExpressionStatement", | |
"LabeledStatement" , | |
"BreakStatement" , | |
"ContinueStatement" , | |
"VariableDeclaration" , | |
"ReturnStatement" , | |
"ThrowStatement" , | |
"TryStatement" , | |
"FunctionDeclaration" , | |
"IfStatement" , | |
"WhileStatement" , | |
"DoWhileStatement" , | |
"ForStatement" , | |
"ForInStatement" , | |
"SwitchStatement" , | |
"WithStatement" ]; | |
var linesToAddBrackets = [ | |
"IfStatement" , | |
"WhileStatement" , | |
"DoWhileStatement" , | |
"ForStatement" , | |
"ForInStatement" , | |
"WithStatement" | |
]; | |
console.log("** source **"); | |
var output = intro+falafel(findIf.toString(), checkForOneLiner); | |
console.log(output); | |
console.log("** end source **"); | |
function findIf(tree){ | |
while(true) | |
if (true) | |
if (tree.body) | |
for(var i=0;i<tree.body.length;i++) | |
findIf(tree.body[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment