Skip to content

Instantly share code, notes, and snippets.

@alex-seville
Created October 18, 2012 02:17
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 alex-seville/3909508 to your computer and use it in GitHub Desktop.
Save alex-seville/3909508 to your computer and use it in GitHub Desktop.
instrumenting code with falafel
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