ESQuery是一个库,用于使用CSS样式选择器系统查询Esprima的AST输出中的语法模式。看看这个演示:
支持以下选择器:
- AST节点类型:
ForStatement - 通配符:
* - 属性的存在:
[attr] - 属性值:
[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 - 第一个 或 最后 孩子:
:first-child或:last-child - nth-child (不支持ax+b):
:nth-child(2) - nth-last-child (不支持ax+b):
:nth-last-child(1) - 后代:
ancestor descendant - 孩子:
parent > child - 后兄弟姐妹:
node ~ sibling - 相邻的兄弟姐妹:
node + adjacent - 否定:
:not(ForStatement) - 有:
:has(ForStatement),:has(> ForStatement) - 匹配任何:
:is([attr] > :first-child, :last-child) - 主题指标:
!IfStatement > [name="foo"] - AST节点类别:
:statement,:expression,:declaration,:function,或:pattern
