@es-joy/estraverse

中文文档
7.1.1发布于 2025/09/26BSD-2-ClauseTypeScriptESM + CJS零依赖近 30 天下载 211.1万

ECMAScript JS AST traversal functions

原始使用文档
文档目录4 节
  1. Estraverse
  2. Documentation
  3. Example Usage
  4. License

Estraverse Build Status

@es-joy/estraverse (estraverse) is a fork of estraverse.

ECMAScript traversal functions from esmangle project.

Documentation

You can find usage docs at wiki page.

Example Usage

The following code will output all variables declared at the root of a file.

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

We can use this.skip, this.remove and this.break functions instead of using Skip, Remove and Break.

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

And estraverse provides estraverse.replace function. When returning node from enter/leave, current node is replaced with it.

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

By passing visitor.keys mapping, we can extend estraverse traversing functionality.

// This tree contains a user-defined `TestExpression` node.
const 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 (node) { },

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

By passing visitor.fallback option, we can control the behavior when encountering unknown nodes.

// This tree contains a user-defined `TestExpression` node.
const 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 (node) { },

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

When visitor.fallback is a function, we can determine which keys to visit on each node.

// This tree contains a user-defined `TestExpression` node.
const 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 (node) { },

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

License

Copyright (C) 2012-2016 Yusuke Suzuki (twitter: @Constellation) and other contributors.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

版本与发布记录

@es-joy/estraverse 共发布 4 个版本,首次发布于 2022/03/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
7.1.1latest2025/09/261.1 年前
7.1.02025/09/261.1 年前
7.0.02025/09/261.1 年前
6.0.02022/03/104 年前

关于 @es-joy/estraverse 的常见问题

@es-joy/estraverse 是什么?

ECMAScript JS AST traversal functions

如何安装 @es-joy/estraverse?

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

@es-joy/estraverse 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@es-joy/estraverse 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@es-joy/estraverse 需要什么版本的 Node.js?

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

@es-joy/estraverse 可以商用吗?

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

@es-joy/estraverse 的下载量有多少?

根据 npm 官方下载统计,@es-joy/estraverse 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,111,374 次,日均约 70,379 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@es-joy/estraverse 还在维护吗?

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