Skip to content

Instantly share code, notes, and snippets.

@erossignon
Last active August 29, 2015 14:04
Show Gist options
  • Save erossignon/d00db28dbea5741b6a19 to your computer and use it in GitHub Desktop.
Save erossignon/d00db28dbea5741b6a19 to your computer and use it in GitHub Desktop.
commenting out 'assert' in javascript code
var fs = require("fs")
esprima = require('esprima'),
escodegen = require('escodegen'),
estraverse = require('estraverse');
;
function is_assert_call_node(node) {
if (node.type !== "ExpressionStatement") return false;
if (!node.expression) return false;
if (node.expression.type !== "CallExpression") return false;
if (node.expression.callee.type !== "Identifier") return false;
return node.expression.callee.name === "assert" ;
}
function remove_assert(input_file,output_file) {
console.log(" removing assert on file " , input_file);
var code = fs.readFileSync(input_file);
var ast = esprima.parse(code,{ comment : true });
estraverse.replace(ast, {
enter: function(node, parent) {
if (is_assert_call_node(node)) {
var assert_code = escodegen.generate(node , { format : { compact: true }});
return {
type: "EmptyStatement" ,
"trailingComments" :[
{ type: "Line", value: "@ASSERT@ " + assert_code }
]
};
} else {
return node;
}
},
leave: function(node, parent) {
}
});
var regenerated_code = escodegen.generate(ast, { comment: true });
fs.writeFileSync(output_file,regenerated_code);
}
var dive = require('dive');
var path = require("path");
dive(process.cwd() + "/lib", { directories: false, files: true }, function(err, file) {
if (err) throw err;
if (path.extname(file) !== ".js" ) {
return ;
}
console.log( "processing file : ", file);
remove_assert(file,file);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment