@ltd/acorn-globals v3.0.0
@ltd/acorn-globals
Important Notes
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.
Other differences
License: From
MITtoLGPL-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
ASTobject as argument, not thecodestring andoptionsarguments, and noparsemethod exported. Because users may use different versions and grammar plug-ins with differentoptionsin different cases, I don't want too much coupling.Parameter Read-Only: No modification of
ASTargument (acorn-globalsmodified thelocalsandparentsproperties 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());