0

假设我可以访问可能正在使用 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
4

1 回答 1

1

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.
于 2016-03-03T01:17:43.917 回答