babel/bin/without_eval
2014-09-12 16:54:28 +02:00

46 lines
1.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
var fs = require("fs");
var acornSrc = fs.readFileSync(require.resolve("../acorn"), "utf8");
var acorn = require("../acorn"), walk = require("../util/walk");
var ast = acorn.parse(acornSrc);
var touchups = [], uses = [];
walk.simple(ast, {
FunctionDeclaration: function(node) {
if (node.id.name == "makePredicate")
touchups.push({text: "// Removed to create an eval-free library", from: node.start, to: node.end});
},
VariableDeclaration: function(node) {
node.declarations.forEach(function(decl) {
if (decl.init && decl.init.type == "CallExpression" &&
decl.init.callee.name == "makePredicate")
uses.push(decl);
});
}
});
var results = Object.create(null);
var functions = new Function("predicates", acornSrc.replace(
/\}\);\s*$/, uses.map(function(decl) {
return "predicates[" + JSON.stringify(decl.id.name) + "] = " + decl.id.name + ";";
}).join("") + "});"))(results);
uses.forEach(function(decl) {
touchups.push({text: results[decl.id.name].toString(),
from: decl.init.start, to: decl.init.end});
});
var result = "", pos = 0;
touchups.sort(function(a, b) { return a.from - b.from; });
touchups.forEach(function(touchup) {
result += acornSrc.slice(pos, touchup.from);
result += touchup.text;
pos = touchup.to;
});
result += acornSrc.slice(pos);
process.stdout.write(result);