javascript - 如何通过函数调用查找 JavaScript 变量的传递用法?

标签 javascript parsing esprima

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

相关文章:

javascript - 用 JavaScript(Esprima.JS) 开发解析器,以 Mozilla AST 格式解析 Haskell 语言

javascript - 如何在客户端使用estraverse js库?

javascript - 如何识别以下代码模式

javascript - 滚动 "stack"个 DOM 对象

javascript - 使用 jquery 单击每个选项卡时如何更改链接 url

javascript - NgRx - 减少存储的优先级和执行顺序

c++ - 帮助解析S-Expression

php - 使用 PHP 格式化 API JSON 响应

Java - 解析字符串 - String.split() 与 Pattern 和 Matcher

javascript - 防止在 jquery 中进行多次 DOM 搜索