![](/img/trans.png)
[英]Javascript code analysis: Count function, variable calls and usage in a javascript file
[英]How to find a JavaScript variable's transitive usage through function calls?
假設我可以訪問可能使用JavaScript變量foo
的全部源。
一些源代碼如下所示:
foo.bar = 'baz';
(function(a, b, c) {
a();
b.bar = 'whee';
c();
}(fn, foo, fn));
是否有可用的工具(ESLint規則,NPM模塊等)可以在此處標識foo
變量的所有用法? 我列出了對變量的所有訪問權限,包括作用域內和嵌套對象。 因此,它必須確定:
foo.bar
用法 foo
用法 b.bar
調用是對foo.bar
的訪問 http://ternjs.net/doc/manual.html#infer似乎是一個很好的線索:
infer.findRefs(ast: AST, scope: Scope, name: string, refScope: Scope, f: fn(AST, Scope))
Will traverse the given syntax tree, using scope as the starting scope, looking for references to variable name that resolve to scope refScope, and call f with the node of the reference and its local scope for each of them.
infer.findPropRefs(ast: AST, scope: Scope, objType: Obj, propName: string, f: fn(AST))
Analogous to findRefs, but used to look for references to a specific property instead. Whereas findRefs is precise, this is dependent on type inference, and thus can not be relied on to be precise.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.