我看过“https://stackoverflow.com/questions/1385335/how-to-generate-function-call-graphs-for-javascript”,并试了一下。如果你想得到一个抽象的语法树,它运作良好。
不幸的是,Closure Compiler似乎只提供--print_tree
,--print_ast
和--print_pass_graph
。它们都不适合我。
我想查看哪个函数调用其他函数的图表。
答案 0 :(得分:15)
code2flow就是这样做的。完全披露,我开始了这个项目
运行
$ code2flow source1.js source2.js -o out.gv
然后,用graphviz打开out.gv
修改:目前,此项目尚未维护。我建议在使用code2flow之前尝试不同的解决方案。
答案 1 :(得分:5)
如果您过滤closure --print_tree
的输出,则可以获得所需内容。
例如,请使用以下文件:
var fib = function(n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
};
console.log(fib(fib(5)));
过滤closure --print_tree
NAME fib 1
FUNCTION 1
CALL 5
NAME fib 5
SUB 5
NAME a 5
NUMBER 1.0 5
CALL 5
NAME fib 5
SUB 5
NAME a 5
NUMBER 2.0 5
EXPR_RESULT 9
CALL 9
GETPROP 9
NAME console 9
STRING log 9
CALL 9
CALL 9
NAME fib 9
CALL 9
CALL 9
NAME fib 9
NUMBER 5.0 9
你可以看到所有的电话陈述。
我编写了以下脚本来执行此操作。
./call_tree
#! /usr/bin/env sh
function make_tree() {
closure --print_tree $1 | grep $1
}
function parse_tree() {
gawk -f parse_tree.awk
}
if [[ "$1" = "--tree" ]]; then
make_tree $2
else
make_tree $1 | parse_tree
fi
parse_tree.awk
BEGIN {
lines_c = 0
indent_width = 4
indent_offset = 0
string_offset = ""
calling = 0
call_indent = 0
}
{
sub(/\[source_file.*$/, "")
sub(/\[free_call.*$/, "")
}
/SCRIPT/ {
indent_offset = calculate_indent($0)
root_indent = indent_offset - 1
}
/FUNCTION/ {
pl = get_previous_line()
if (calculate_indent(pl) < calculate_indent($0))
print pl
print
}
{
lines_v[lines_c] = $0
lines_c += 1
}
{
indent = calculate_indent($0)
if (indent <= call_indent) {
calling = 0
}
if (calling) {
print
}
}
/CALL/ {
calling = 1
call_indent = calculate_indent($0)
print
}
/EXPR/{
line_indent = calculate_indent($0)
if (line_indent == root_indent) {
if ($0 !~ /(FUNCTION)/) {
print
}
}
}
function calculate_indent(line) {
match(line, /^ */)
return int(RLENGTH / indent_width) - indent_offset
}
function get_previous_line() {
return lines_v[lines_c - 1]
}
答案 2 :(得分:4)
我最终使用UglifyJS2和Dot/GraphViz进行了此操作,将上述答案与相关问题的答案结合起来。
对我来说,缺少的部分是如何过滤解析的AST。事实证明,UglifyJS具有TreeWalker对象,它基本上将函数应用于AST的每个节点。这是我到目前为止的代码:
//to be run using nodejs
var UglifyJS = require('uglify-js')
var fs = require('fs');
var util = require('util');
var file = 'path/to/file...';
//read in the code
var code = fs.readFileSync(file, "utf8");
//parse it to AST
var toplevel = UglifyJS.parse(code);
//open the output DOT file
var out = fs.openSync('path/to/output/file...', 'w');
//output the start of a directed graph in DOT notation
fs.writeSync(out, 'digraph test{\n');
//use a tree walker to examine each node
var walker = new UglifyJS.TreeWalker(function(node){
//check for function calls
if (node instanceof UglifyJS.AST_Call) {
if(node.expression.name !== undefined)
{
//find where the calling function is defined
var p = walker.find_parent(UglifyJS.AST_Defun);
if(p !== undefined)
{
//filter out unneccessary stuff, eg calls to external libraries or constructors
if(node.expression.name == "$" || node.expression.name == "Number" || node.expression.name =="Date")
{
//NOTE: $ is from jquery, and causes problems if it's in the DOT file.
//It's also very frequent, so even replacing it with a safe string
//results in a very cluttered graph
}
else
{
fs.writeSync(out, p.name.name);
fs.writeSync(out, " -> ");
fs.writeSync(out, node.expression.name);
fs.writeSync(out, "\n");
}
}
else
{
//it's a top level function
fs.writeSync(out, node.expression.name);
fs.writeSync(out, "\n");
}
}
}
if(node instanceof UglifyJS.AST_Defun)
{
//defined but not called
fs.writeSync(out, node.name.name);
fs.writeSync(out, "\n");
}
});
//analyse the AST
toplevel.walk(walker);
//finally, write out the closing bracket
fs.writeSync(out, '}');
我用node运行它,然后通过
输出输出 dot -Tpng -o graph_name.png dot_file_name.dot
注意:
它提供了一个非常基本的图形 - 只有黑色和白色,没有格式。
它根本不会捕获ajax,并且可能不会像eval
或with
那样捕获others have mentioned。
此外,它在图中包含:由其他函数调用的函数(以及调用其他函数的函数),独立调用的函数,定义但未调用的AND函数。
由于这一切,它可能会错过相关的内容,或包含不相关的内容。这是一个开始,似乎完成了我所追求的目标,并且首先引起了我这个问题。
答案 3 :(得分:1)
https://github.com/mishoo/UglifyJS 允许访问javascript中的ast。
ast.coffee
util = require 'util'
jsp = require('uglify-js').parser
orig_code = """
var a = function (x) {
return x * x;
};
function b (x) {
return a(x)
}
console.log(a(5));
console.log(b(5));
"""
ast = jsp.parse(orig_code)
console.log util.inspect ast, true, null, true