without_eval 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env node
  2. var fs = require("fs")
  3. var acornSrc = fs.readFileSync(require.resolve("../dist/acorn"), "utf8")
  4. var acorn = require("../dist/acorn"), walk = require("../dist/walk")
  5. var ast = acorn.parse(acornSrc)
  6. var touchups = [], uses = []
  7. var makePred
  8. walk.simple(ast, {
  9. FunctionDeclaration: function(node) {
  10. if (node.id.name == "makePredicate") {
  11. makePred = node
  12. touchups.push({text: "// Removed to create an eval-free library", from: node.start, to: node.end})
  13. }
  14. },
  15. ObjectExpression: function(node) {
  16. node.properties.forEach(function(prop) {
  17. if (prop.value.type == "CallExpression" &&
  18. prop.value.callee.name == "makePredicate")
  19. uses.push(prop.value)
  20. })
  21. }
  22. })
  23. var results = []
  24. var dryRun = acornSrc.slice(0, makePred.end) + "; makePredicate = (function(mp) {" +
  25. "return function(words) { var r = mp(words); predicates.push(r); return r }})(makePredicate);" +
  26. acornSrc.slice(makePred.end)
  27. ;(new Function("predicates", dryRun))(results)
  28. uses.forEach(function (node, i) {
  29. touchups.push({text: results[i].toString(), from: node.start, to: node.end})
  30. })
  31. var result = "", pos = 0
  32. touchups.sort(function(a, b) { return a.from - b.from })
  33. touchups.forEach(function(touchup) {
  34. result += acornSrc.slice(pos, touchup.from)
  35. result += touchup.text
  36. pos = touchup.to
  37. })
  38. result += acornSrc.slice(pos)
  39. process.stdout.write(result)