esquery

中文文档
1.7.0发布于 2025/12/31BSD-3-ClauseESM + CJS近 30 天下载 5.7亿

A query library for ECMAScript AST using a CSS selector like query language.

原始使用文档

ESQuery is a library for querying the AST output by Esprima for patterns of syntax using a CSS style selector system. Check out the demo:

demo

The following selectors are supported:

  • AST node type: ForStatement
  • wildcard: *
  • attribute existence: [attr]
  • attribute value: [attr="foo"] or [attr=123]
  • attribute regex: [attr=/foo.*/] or (with flags) [attr=/foo.*/is]
  • attribute conditions: [attr!="foo"], [attr>2], [attr<3], [attr>=2], or [attr<=3]
  • nested attribute: [attr.level2="foo"]
  • field: FunctionDeclaration > Identifier.id
  • First or last child: :first-child or :last-child
  • nth-child (no ax+b support): :nth-child(2)
  • nth-last-child (no ax+b support): :nth-last-child(1)
  • descendant: ancestor descendant
  • child: parent > child
  • following sibling: node ~ sibling
  • adjacent sibling: node + adjacent
  • negation: :not(ForStatement)
  • has: :has(ForStatement), :has(> ForStatement)
  • matches-any: :is([attr] > :first-child, :last-child)
  • subject indicator: !IfStatement > [name="foo"]
  • class of AST node: :statement, :expression, :declaration, :function, or :pattern

Build Status

版本与发布记录

esquery 共发布 18 个版本,首次发布于 2013/07/24;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.7.0latest2025/12/3110 个月前
1.6.02024/07/092 年前
1.5.02023/03/023 年前
1.4.22023/02/163 年前
1.4.12023/02/163 年前
1.4.02021/02/065 年前

关于 esquery 的常见问题

esquery 是什么?

A query library for ECMAScript AST using a CSS selector like query language.

如何安装 esquery?

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

esquery 支持 TypeScript 吗?

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

esquery 支持 ESM 还是 CommonJS?

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

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

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

esquery 可以商用吗?

esquery 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

esquery 的下载量有多少?

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

esquery 还在维护吗?

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