Skip to content

Instantly share code, notes, and snippets.

@meowmeowxw
Last active November 28, 2022 20:57
Show Gist options
  • Save meowmeowxw/b9e0251626f88120d3e5027f4edcf7d4 to your computer and use it in GitHub Desktop.
Save meowmeowxw/b9e0251626f88120d3e5027f4edcf7d4 to your computer and use it in GitHub Desktop.
Nodejs simple ast parser
const {Parser} = require("acorn")
const recast = require("recast")
const fs = require("fs");
const ast = Parser.parse(fs.readFileSync("./request.js").toString(), Parser.Options = {
ecmaVersion: "latest"
});
const functionNames = [];
const dependencies = [];
let libraryVariable = "";
recast.visit(
ast,
{
visitCallExpression(path) {
const {callee, arguments: args} = path.node;
if (callee.name === "require" && args[0]) {
if (args[0].value === "http-signature") {
console.log(path.node);
//console.log(path.parentPath.value.id.name);
libraryVariable = path.parentPath.value.id.name;
return false;
}
}
this.traverse(path);
}
}
)
console.log(libraryVariable);
recast.visit(
ast,
{
visitCallExpression(path) {
let callee_object = path.node.callee.object;
if (callee_object != undefined && 'name' in callee_object) {
if (callee_object.name === libraryVariable) {
console.log(callee_object);
console.log(path.node.callee.property);
}
}
this.traverse(path);
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment