# esquery 中文文档

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

版本：1\.7\.0
npm 元数据更新时间：2025-12-31T15:44:57.906Z
中文译文更新时间：2026-11-04T23:42:07.147Z
软件包许可：BSD-3-Clause

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

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

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

- 包体积（解压后）：1.1 MB，13 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- Node.js 要求：\>=0\.10
- 运行依赖：1 个
- 首次发布：2013-07-24T00:20:02.985Z
- 最新版本发布：2025-12-31T15:44:57.664Z
- 近 30 天下载量：566077381（2026-10-04 至 2026-11-02，npm 官方下载统计）
- 已发布版本数：18（近 12 个月 1 个）

## 中文简介

ECMAScript AST的查询库，使用类似CSS选择器的查询语言。

## 安装

```sh
npm install esquery
```

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

## README

ESQuery是一个库，用于使用CSS样式选择器系统查询Esprima的AST输出中的语法模式。看看这个演示：

[演示](https://estools.github.io/esquery/)

支持以下选择器：

-   AST节点类型： `ForStatement`
-   [通配符](http://dev.w3.org/csswg/selectors4/#universal-selector)： `*`
-   [属性的存在](http://dev.w3.org/csswg/selectors4/#attribute-selectors)： `[attr]`
-   [属性值](http://dev.w3.org/csswg/selectors4/#attribute-selectors)： `[attr="foo"]` 或 `[attr=123]`
-   属性正则表达式: `[attr=/foo.*/]` 或者（带旗帜） `[attr=/foo.*/is]`
-   属性条件: `[attr!="foo"]`， `[attr>2]`， `[attr<3]`， `[attr>=2]`,或 `[attr<=3]`
-   嵌套的属性: `[attr.level2="foo"]`
-   字段: `FunctionDeclaration > Identifier.id`
-   [第一个](http://dev.w3.org/csswg/selectors4/#the-first-child-pseudo) 或 [最后](http://dev.w3.org/csswg/selectors4/#the-last-child-pseudo) 孩子: `:first-child` 或 `:last-child`
-   [nth-child](http://dev.w3.org/csswg/selectors4/#the-nth-child-pseudo) （不支持ax+b）： `:nth-child(2)`
-   [nth-last-child](http://dev.w3.org/csswg/selectors4/#the-nth-last-child-pseudo) （不支持ax+b）： `:nth-last-child(1)`
-   [后代](http://dev.w3.org/csswg/selectors4/#descendant-combinators)： `ancestor descendant`
-   [孩子](http://dev.w3.org/csswg/selectors4/#child-combinators)： `parent > child`
-   [后兄弟姐妹](http://dev.w3.org/csswg/selectors4/#general-sibling-combinators)： `node ~ sibling`
-   [相邻的兄弟姐妹](http://dev.w3.org/csswg/selectors4/#adjacent-sibling-combinators)： `node + adjacent`
-   [否定](http://dev.w3.org/csswg/selectors4/#negation-pseudo)： `:not(ForStatement)`
-   [有](https://drafts.csswg.org/selectors-4/#has-pseudo)： `:has(ForStatement)`， `:has(> ForStatement)`
-   [匹配任何](http://dev.w3.org/csswg/selectors4/#matches)： `:is([attr] > :first-child, :last-child)`
-   [主题指标](http://dev.w3.org/csswg/selectors4/#subject)： `!IfStatement > [name="foo"]`
-   AST节点类别： `:statement`， `:expression`， `:declaration`， `:function`,或 `:pattern`

[![Build Status](https://travis-ci.org/estools/esquery.png?branch=master)](https://travis-ci.org/estools/esquery)

## 常见问题

### esquery 是什么？

ECMAScript AST的查询库，使用类似CSS选择器的查询语言。

### 如何安装 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 和提交记录判断。

