function createFunctionCallMap(tree) {
const m = new Map();
function traverse(node, currentFunctionName) {
if (
node.type === "function_declaration" ||
node.type === "method_definition"
) {
currentFunctionName = node.childForFieldName("name")?.text || null;
}
if (currentFunctionName && node.type === "call_expression") {
const calleeName = node.childForFieldName("function")?.text;
if (!m.has(currentFunctionName)) {
m.set(currentFunctionName, []);
}
m.get(currentFunctionName).push(calleeName);
}
if (currentFunctionName && node.type === "function_expression") {
const calleeName = node.childForFieldName("name")?.text;
if (!m.has(currentFunctionName)) {
m.set(currentFunctionName, []);
}
m.get(currentFunctionName).push(calleeName);
}
for (let i = 0; i < node.childCount; i++) {
traverse(node.child(i), currentFunctionName);
}
}
traverse(tree.rootNode, null);
return m;
}