estraverse

中文文档
5.3.0发布于 2021/10/25BSD-2-ClauseCommonJS零依赖近 30 天下载 8.7亿

ECMAScript JS AST遍历函数

中文使用文档
文档目录4 节
  1. Estraverse
  2. 文档
  3. 示例使用
  4. 许可证

Estraverse Build Status

Estraverse (estraverse)是 ECMAScript 遍历函数 esmangle项目。

文档

你可以在这里找到使用文档 wiki页面。

示例使用

下面的代码将输出在文件根目录下声明的所有变量。

estraverse.traverse(ast, {
    enter: function (node, parent) {
        if (node.type == 'FunctionExpression' || node.type == 'FunctionDeclaration')
            return estraverse.VisitorOption.Skip;
    },
    leave: function (node, parent) {
        if (node.type == 'VariableDeclarator')
          console.log(node.id.name);
    }
});

我们可以用 this.skip, this.remove 和 this.break 函数,而不是使用Skip, Remove和Break。

estraverse.traverse(ast, {
    enter: function (node) {
        this.break();
    }
});

而estraverse提供 estraverse.replace 函数。返回节点时 enter/leave,将当前节点替换为当前节点。

result = estraverse.replace(tree, {
    enter: function (node) {
        // Replace it with replaced.
        if (node.type === 'Literal')
            return replaced;
    }
});

通过 visitor.keys 通过映射,我们可以扩展遍历功能。

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'Literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
estraverse.traverse(tree, {
    enter: function (node) { },

    // Extending the existing traversing rules.
    keys: {
        // TargetNodeName: [ 'keys', 'containing', 'the', 'other', '**node**' ]
        TestExpression: ['argument']
    }
});

通过 visitor.fallback 选项,我们可以控制遇到未知节点时的行为。

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'Literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
estraverse.traverse(tree, {
    enter: function (node) { },

    // Iterating the child **nodes** of unknown nodes.
    fallback: 'iteration'
});

当 visitor.fallback 是一个函数,我们可以确定在每个节点上访问哪个键。

// This tree contains a user-defined `TestExpression` node.
var tree = {
    type: 'TestExpression',

    // This 'argument' is the property containing the other **node**.
    argument: {
        type: 'Literal',
        value: 20
    },

    // This 'extended' is the property not containing the other **node**.
    extended: true
};
estraverse.traverse(tree, {
    enter: function (node) { },

    // Skip the `argument` property of each node
    fallback: function(node) {
        return Object.keys(node).filter(function(key) {
            return key !== 'argument';
        });
    }
});

许可证

版权所有2012-2016 Yusuke Suzuki (推特: @Constellation)和其他贡献者。

重新分发和使用的源代码和二进制形式,有或没有 在满足下列条件的情况下,允许进行修改:

  • 重新发布源代码必须保留上述版权 注意,此条件列表和以下免责声明。

  • 以二进制形式重新发布必须复制上述版权 注意,本条件列表和以下免责声明中的 随发行版提供的文件和/或其他材料。

本软件由版权所有者和贡献者“按原样”提供。 以及任何明示或暗示的保证,包括但不限于 对适销性和适合某一特定目的的默示保证 是否认。在任何情况下,我都不承担任何责任 直接的、间接的、偶然的、特殊的、惩戒性的或后果性的损害 (包括但不限于,替代商品或服务的采购; 损失:失去使用、数据或利润;或业务中断),无论如何引起和 在任何责任理论中,无论是合同责任、严格责任还是侵权责任 (包括疏忽或其他)因使用 本软件即使被告知有这种损害的可能性。

版本与发布记录

estraverse 共发布 35 个版本,首次发布于 2012/10/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.3.0latest2021/10/255 年前
5.2.02020/08/056 年前
5.1.02020/04/166 年前
5.0.02020/03/236 年前
4.3.02019/08/147 年前
4.2.02016/03/1110 年前

关于 estraverse 的常见问题

estraverse 是什么?

ECMAScript JS AST遍历函数

如何安装 estraverse?

在项目目录运行 npm install estraverse,或使用 pnpm add / yarn add / bun add。

estraverse 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/estraverse 是否存在。

estraverse 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

estraverse 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=4.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

estraverse 可以商用吗?

estraverse 使用 BSD-2-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

estraverse 的下载量有多少?

根据 npm 官方下载统计,estraverse 在 2026/10/04 – 2026/11/02 这 30 天内被下载 865,832,385 次,日均约 28,861,080 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

estraverse 还在维护吗?

最新版本 5.3.0 发布于 2021/10/25(5 年前);至今共发布 35 个版本,其中过去 12 个月发布了 0 个;首次发布于 2012/10/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。