假设我可以访问可能使用 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.
关于javascript - 如何通过函数调用查找 JavaScript 变量的传递用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35761210/