簡體   English   中英

如何通過函數調用找到JavaScript變量的可傳遞用法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM