Skip to content

Commit 3da5fe3

Browse files
fix: add esquery style doc
1 parent 9e7afa9 commit 3da5fe3

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ fn get_message(rule: &RuleConfig<WasmLang>, node: &NodeMatch) -> String {
4545
}
4646

4747
impl WasmMatch {
48+
pub fn from_pattern(nm: NodeMatch) -> Self {
49+
let node = nm.get_node().clone();
50+
let kind = node.kind().to_string();
51+
let node = WasmNode::from(node);
52+
let env = nm.get_env().clone();
53+
let env = env_to_map(env);
54+
let message = String::new();
55+
Self { node, env, message, kind }
56+
}
4857
pub fn from_match(nm: NodeMatch, rule: &RuleConfig<WasmLang>) -> Self {
4958
let node = nm.get_node().clone();
5059
let kind = node.kind().to_string();

website/guide/rule-config/atomic-rule.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ To change the parsing behavior of `pattern`, you should use pattern object with
125125
See [this FAQ](/advanced/faq.html#kind-and-pattern-rules-are-not-working-together-why).
126126
:::
127127

128+
### ESQuery style `kind` <Badge type="warning" text="Experimental" />
129+
130+
From ast-grep v0.39.1, you can also use ESQuery style selector in `kind` to match AST nodes. This is an experimental feature and may change in the future.
131+
132+
```yaml
133+
rule:
134+
kind: call_expression > identifier
135+
```
136+
This will match the `identifier` node that is a child of `call_expression` node. Internally, it will be converted to a [relational rule](/guide/rule-config/relational-rule.html) `has`.
137+
138+
Currently, the ESQuery style `kind` only supports the following selectors:
139+
* node kind: `identifier`
140+
* `>`: direct child selectors
141+
* `+`: next sibling selector
142+
* `~`: following sibling selector
143+
* ` `: descendant selector
144+
145+
If you want more selectors, please respond to [this issue on GitHub](https://github.com/ast-grep/ast-grep/issues/2127).
146+
147+
128148
## `regex`
129149

130150
The `regex` atomic rule will match the AST node by its text against a Rust regular expression.

website/reference/rule.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ A rule is called **positive** if it only matches nodes with specific kinds. For
2121

2222
A `String` pattern will match one single AST node according to [pattern syntax](/guide/pattern-syntax).
2323

24-
Example:
24+
**Example:**
2525

2626
```yml
2727
pattern: console.log($ARG)
@@ -61,12 +61,19 @@ pattern:
6161

6262
The kind name of the node to match. You can look up code's kind names in [playground](/playground).
6363

64-
Example:
64+
**Example:**
6565

6666
```yml
6767
kind: call_expression
6868
```
6969

70+
ast-grep 0.39+ also supports limited ESQuery syntax for `kind`:
71+
72+
**Example:**
73+
```yml
74+
kind: call_expression > identifier
75+
```
76+
7077
### `regex`
7178
* type: `String`
7279

0 commit comments

Comments
 (0)