estree-walker

中文文档
3.0.3发布于 2023/01/20MITTypeScriptES Module近 30 天下载 8.5亿

Traverse an ESTree-compliant AST

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Why not use estraverse?
  4. License

estree-walker

Simple utility for walking an ESTree-compliant AST, such as one generated by acorn.

Installation

npm i estree-walker

Usage

var walk = require('estree-walker').walk;
var acorn = require('acorn');

ast = acorn.parse(sourceCode, options); // https://github.com/acornjs/acorn

walk(ast, {
  enter(node, parent, prop, index) {
    // some code happens
  },
  leave(node, parent, prop, index) {
    // some code happens
  }
});

Inside the enter function, calling this.skip() will prevent the node's children being walked, or the leave function (which is optional) being called.

Call this.replace(new_node) in either enter or leave to replace the current node with a new one.

Call this.remove() in either enter or leave to remove the current node.

Why not use estraverse?

The ESTree spec is evolving to accommodate ES6/7. I've had a couple of experiences where estraverse was unable to handle an AST generated by recent versions of acorn, because it hard-codes visitor keys.

estree-walker, by contrast, simply enumerates a node's properties to find child nodes (and child lists of nodes), and is therefore resistant to spec changes. It's also much smaller. (The performance, if you're wondering, is basically identical.)

None of which should be taken as criticism of estraverse, which has more features and has been battle-tested in many more situations, and for which I'm very grateful.

License

MIT

版本与发布记录

estree-walker 共发布 27 个版本,首次发布于 2015/04/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.3latest2023/01/203 年前
3.0.22023/01/043 年前
3.0.12021/12/204 年前
3.0.02021/01/305 年前
2.0.22020/12/095 年前
2.0.12020/02/046 年前

关于 estree-walker 的常见问题

estree-walker 是什么?

Traverse an ESTree-compliant AST

如何安装 estree-walker?

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

estree-walker 支持 TypeScript 吗?

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

estree-walker 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

estree-walker 可以商用吗?

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

estree-walker 的下载量有多少?

根据 npm 官方下载统计,estree-walker 在 2026/10/05 – 2026/11/03 这 30 天内被下载 849,894,364 次,日均约 28,329,812 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

estree-walker 还在维护吗?

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