esquery

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

BSD-3-Clause 18 个版本
安装
npm install esquery
yarn add esquery
pnpm add esquery
bun add esquery
README

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

版本列表
1.7.0 2025-12-31
1.6.0 2024-07-08
1.5.0 2023-03-02
1.4.2 2023-02-16
1.4.1 2023-02-16
1.4.0 2021-02-05
1.3.1 2020-04-16
1.3.0 2020-04-15
1.2.1 2020-04-13
1.2.0 2020-03-23
1.1.0 2020-02-12
1.0.1 2018-04-02
1.0.0 2017-03-10
0.4.0 2015-05-05
0.3.0 2013-11-04
0.2.0 2013-08-06
0.1.0 2013-08-03
0.0.1 2013-07-24