# estraverse 中文文档

规范地址：https://npmcn.com/package/estraverse/

版本：5\.3\.0
npm 元数据更新时间：2023-06-22T16:31:49.531Z
中文译文更新时间：2026-11-04T13:50:37.082Z
软件包许可：BSD-2-Clause

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/estraverse
原文对照：https://npmcn.com/package/estraverse/?lang=original
项目主页：https://github.com/estools/estraverse
源码仓库：https://github.com/estools/estraverse

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：37.1 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=4\.0
- 运行依赖：0 个
- 首次发布：2012-10-09T18:22:10.554Z
- 最新版本发布：2021-10-25T12:18:17.722Z
- 近 30 天下载量：891432456（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：35（近 12 个月 0 个）

## 中文简介

ECMAScript JS AST遍历函数

## 安装

```sh
npm install estraverse
```

也可以使用：`pnpm add estraverse`、`yarn add estraverse`、`bun add estraverse`

## README

### Estraverse [![Build Status](https://secure.travis-ci.org/estools/estraverse.svg)](http://travis-ci.org/estools/estraverse)

Estraverse ([estraverse](http://github.com/estools/estraverse))是 [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm) 遍历函数 [esmangle项目](http://github.com/estools/esmangle)。

### 文档

你可以在这里找到使用文档 [wiki页面](https://github.com/estools/estraverse/wiki/Usage)。

### 示例使用

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

```javascript
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。

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

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

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

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

```javascript
// 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` 选项，我们可以控制遇到未知节点时的行为。

```javascript
// 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` 是一个函数，我们可以确定在每个节点上访问哪个键。

```javascript
// 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](http://github.com/Constellation) (推特: [@Constellation](http://twitter.com/Constellation))和其他贡献者。

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

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

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

## 常见问题

### 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/05 – 2026/11/03 这 30 天内被下载 891,432,456 次，日均约 29,714,415 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### estraverse 还在维护吗？

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

