简体   繁体   English

如何通过函数调用找到JavaScript变量的可传递用法?

[英]How to find a JavaScript variable's transitive usage through function calls?

Suppose I have access to the full set of source that could potentially be using a JavaScript variable foo . 假设我可以访问可能使用JavaScript变量foo的全部源。

Some of the source code looks like this: 一些源代码如下所示:

foo.bar = 'baz';

(function(a, b, c) {
    a();
    b.bar = 'whee';
    c();
}(fn, foo, fn));

Are there any tools available (ESLint rules, NPM modules, etc.) that could identify all usages of the foo variable here? 是否有可用的工具(ESLint规则,NPM模块等)可以在此处标识foo变量的所有用法? I'm after a list of all access of the variable, including within scopes and including nested objects. 我列出了对变量的所有访问权限,包括作用域内和嵌套对象。 So, it would have to identify: 因此,它必须确定:

  • The foo.bar usage in the global scope 全局范围内的foo.bar用法
  • The foo usage in the function invocation 函数调用中的foo用法
  • The b.bar call is an access of foo.bar b.bar调用是对foo.bar的访问

http://ternjs.net/doc/manual.html#infer seems like a good lead: 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