This package is adapted from acorn-globals
.
When I found improvable things in use, I submitted some PR for to improve it together.
But there's one thing I think it's not a defect, but a requirement for most people, but not for me, so I can only publish a modified package.
The major difference is that acorn-globals
automatically ignores references to global variable undefined
, while @ltd/acorn-globals
reserves it to decide in user land.
At the same time, if the input is not a Program
node whose sourceType
is not module
, the variable names declared at top-level will also be included in the output.
-
License: From
MIT
toLGPL-3.0
, almost same unlimited for using, while the difference is that if the package itself is modified, it must be released as open source with the same license. -
Host Requirement: Because it is a back-end package, it uses ES 6+ feature.
-
Reduce the API: The the main export function only accepts the parsed
AST
object as argument, not thecode
string andoptions
arguments, and noparse
method exported. Because users may use different versions and grammar plug-ins with differentoptions
in different cases, I don't want too much coupling. -
Parameter Read-Only: No modification of
AST
argument (acorn-globals
modified thelocals
andparents
properties of nodes). -
Return Value: The return value is changed from
{ name :string, nodes :Node[] }[]
toMap<string, Node[]> & { names (): string[], nodes () :Node[] }
.
const find = require('@ltd/acorn-globals');
const AST = require('acorn')/*.Parser.extend(plugin)*/.parse(code/*, options*/);
const globals = find(AST);
console.log(globals.names());
这个包改编自 acorn-globals
。
我在使用中发现可以改进的地方时,都第一时间提交了 PR 以共同完善。
但是其中有一处我认为是它的功能而非缺陷,也是多数人的需求,但不是我的需求,因而只能单独发布一个修改后的包。
这个最重要的差异就是,acorn-globals
会自动忽略对于全局变量 undefined
的引用,而 @ltd/acorn-globals
会保留,供用户自行取舍。
同时,如果传入的不是 sourceType
属性为 "module"
的 Program
节点,那么顶层声明的变量名也会列入全局名录。
-
许可协议:从
MIT
变为LGPL-3.0
,使用上一样,几乎就是没限制,主要区别是如果对包本身进行了改造,则必须以同样许可协议开源发布。 -
宿主要求:由于是后端用包,使用了 ES 6 以上的特性。
-
缩小接口:模块主导出函数只接受解析后的
AST
对象作为参数,而不接受code
字符串和options
,同时取消导出parse
方法函数,以尽可能解除耦合,满足用户对不同acorn
版本、语法插件、options
的需求。 -
参数只读:不对
AST
参数作修改(acorn-globals
中修改了节点的locals
和parents
属性)。 -
返回形式:返回值从
{ name :string, nodes :Node[] }[]
改为Map<string, Node[]> & { names () :string[], nodes () :Node[] }
。
const find = require('@ltd/acorn-globals');
const AST = require('acorn')/*.Parser.extend(plugin)*/.parse(code/*, options*/);
const globals = find(AST);
console.log(globals.names());