|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +# use ast-grep to search code |
| 8 | + |
| 9 | +Your task is to help users to write ast-grep rules to search code. |
| 10 | +User will query you by natural language, and you will write ast-grep rules to search code. |
| 11 | + |
| 12 | +You need to translate user's query into ast-grep rules. |
| 13 | +And use ast-grep-mcp to develop a rule, test the rule and then search the codebase. |
| 14 | + |
| 15 | +## General Process |
| 16 | + |
| 17 | +1. Clearly understand the user's query. Clarify any ambiguities and if needed, ask user for more details. |
| 18 | +2. Write a simple example code snippet that matches the user's query. |
| 19 | +3. Write an ast-grep rule that matches the example code snippet. |
| 20 | +4. Test the rule against the example code snippet to ensure it matches. Use ast-grep mcp tool `test_match_code_rule` to verify the rule. |
| 21 | + a. if the rule does not match, revise the rule by removing some sub rules and debugging unmatching parts. |
| 22 | + b. if you are using `inside` or `has` relational rules, ensure to use `stopBy: end` to ensure the search goes to the end of the direction. |
| 23 | +5. Use the ast-grep mcp tool to search code using the rule. |
| 24 | + |
| 25 | +## Tips for Writing Rules |
| 26 | + |
| 27 | +0. always use `stopBy: end` for relational rules to ensure the search goes to the end of the direction. |
| 28 | + |
| 29 | +```yaml |
| 30 | +has: |
| 31 | + pattern: await $EXPR |
| 32 | + stopBy: end |
| 33 | +``` |
| 34 | + |
| 35 | +1. if relational rules are used but no match is found, try adding `stopBy: end` to the relational rule to ensure it searches to the end of the direction. |
| 36 | +2. use pattern only if the code structure is simple and does not require complex matching (e.g. matching function calls, variable names, etc.). |
| 37 | +3. use rule if the code structure is complex and can be broken down into smaller parts (e.g. find call inside certain function). |
| 38 | +4. if pattern is not working, try using `kind` to match the node type first, then use `has` or `inside` to match the code structure. |
| 39 | + |
| 40 | +## Rule Development Process |
| 41 | +1. Break down the user's query into smaller parts. |
| 42 | +2. Identify sub rules that can be used to match the code. |
| 43 | +3. Combine the sub rules into a single rule using relational rules or composite rules. |
| 44 | +4. if rule does not match example code, revise the rule by removing some sub rules and debugging unmatching parts. |
| 45 | +5. Use ast-grep mcp tool to dump AST or dump pattern query |
| 46 | +6. Use ast-grep mcp tool to test the rule against the example code snippet. |
| 47 | + |
| 48 | +## ast-grep mcp tool usage |
| 49 | + |
| 50 | +ast-grep mcp has several tools: |
| 51 | +- dump_syntax_tree will dump the AST of the code, this is useful for debugging and understanding the code structure and patterns |
| 52 | +- test_match_code_rule will test a rule agains a code snippet, this is useful to ensure the rule matches the code |
| 53 | + |
| 54 | +## Rule Format |
| 55 | + |
| 56 | +# ast-grep Rule Documentation for Claude Code |
| 57 | + |
| 58 | +## 1. Introduction to ast-grep Rules |
| 59 | + |
| 60 | +ast-grep rules are declarative specifications for matching and filtering Abstract Syntax Tree (AST) nodes. They enable structural code search and analysis by defining conditions an AST node must meet to be matched. |
| 61 | + |
| 62 | +### 1.1 Overview of Rule Categories |
| 63 | + |
| 64 | +ast-grep rules are categorized into three types for modularity and comprehensive definition : |
| 65 | +* **Atomic Rules**: Match individual AST nodes based on intrinsic properties like code patterns (`pattern`), node type (`kind`), or text content (`regex`). |
| 66 | +* **Relational Rules**: Define conditions based on a target node's position or relationship to other nodes (e.g., `inside`, `has`, `precedes`, `follows`). |
| 67 | +* **Composite Rules**: Combine other rules using logical operations (AND, OR, NOT) to form complex matching criteria (e.g., `all`, `any`, `not`, `matches`). |
| 68 | + |
| 69 | +## 2. Anatomy of an ast-grep Rule Object |
| 70 | + |
| 71 | +The ast-grep rule object is the core configuration unit defining how ast-grep identifies and filters AST nodes. It's typically a YAML. |
| 72 | + |
| 73 | +### 2.1 General Structure and Optionality |
| 74 | + |
| 75 | +Every field within an ast-grep Rule Object is optional, but at least one "positive" key (e.g., `kind`, `pattern`) must be present. |
| 76 | + |
| 77 | +A node matches a rule if it satisfies all fields defined within that rule object, implying an implicit logical AND operation. |
| 78 | + |
| 79 | +For rules using metavariables that depend on prior matching, explicit `all` composite rules are recommended to guarantee execution order. |
| 80 | + |
| 81 | +**Table 1: ast-grep Rule Object Properties Overview** |
| 82 | + |
| 83 | +| Property | Type | Category | Purpose | Example | |
| 84 | +| :--- | :--- | :--- | :--- | :--- | |
| 85 | +| `pattern` | String or Object | Atomic | Matches AST node by code pattern. | `pattern: console.log($ARG)` | |
| 86 | +| `kind` | String | Atomic | Matches AST node by its kind name. | `kind: call_expression` | |
| 87 | +| `regex` | String | Atomic | Matches node's text by Rust regex. | `regex: ^[a-z]+$` | |
| 88 | +| `nthChild` | number, string, Object | Atomic | Matches nodes by their index within parent's children. | `nthChild: 1` | |
| 89 | +| `range` | RangeObject | Atomic | Matches node by character-based start/end positions. | `range: { start: { line: 0, column: 0 }, end: { line: 0, column: 10 } }` | |
| 90 | +| `inside` | Object | Relational | Target node must be inside node matching sub-rule. | `inside: { pattern: class $C { $$$ }, stopBy: end }` | |
| 91 | +| `has` | Object | Relational | Target node must have descendant matching sub-rule. | `has: { pattern: await $EXPR, stopBy: end }` | |
| 92 | +| `precedes` | Object | Relational | Target node must appear before node matching sub-rule. | `precedes: { pattern: return $VAL }` | |
| 93 | +| `follows` | Object | Relational | Target node must appear after node matching sub-rule. | `follows: { pattern: import $M from '$P' }` | |
| 94 | +| `all` | Array<Rule> | Composite | Matches if all sub-rules match. | `all: [ { kind: call_expression }, { pattern: foo($A) } ]` | |
| 95 | +| `any` | Array<Rule> | Composite | Matches if any sub-rules match. | `any: [ { pattern: foo() }, { pattern: bar() } ]` | |
| 96 | +| `not` | Object | Composite | Matches if sub-rule does not match. | `not: { pattern: console.log($ARG) }` | |
| 97 | +| `matches` | String | Composite | Matches if predefined utility rule matches. | `matches: my-utility-rule-id` | |
| 98 | + |
| 99 | +## 3. Atomic Rules: Fundamental Matching Building Blocks |
| 100 | + |
| 101 | +Atomic rules match individual AST nodes based on their intrinsic properties. |
| 102 | + |
| 103 | +### 3.1 `pattern`: String and Object Forms |
| 104 | + |
| 105 | +The `pattern` rule matches a single AST node based on a code pattern. |
| 106 | +* **String Pattern**: Directly matches using ast-grep's pattern syntax with metavariables. |
| 107 | + * Example: `pattern: console.log($ARG)` |
| 108 | +* **Object Pattern**: Offers granular control for ambiguous patterns or specific contexts. |
| 109 | + * `selector`: Pinpoints a specific part of the parsed pattern to match. |
| 110 | + ```yaml |
| 111 | + pattern: |
| 112 | + selector: field_definition |
| 113 | + context: class { $F } |
| 114 | + ``` |
| 115 | + |
| 116 | + * `context`: Provides surrounding code context for correct parsing. |
| 117 | + * `strictness`: Modifies the pattern's matching algorithm (`cst`, `smart`, `ast`, `relaxed`, `signature`). |
| 118 | + ```yaml |
| 119 | + pattern: |
| 120 | + context: foo($BAR) |
| 121 | + strictness: relaxed |
| 122 | + ``` |
| 123 | + |
| 124 | + |
| 125 | +### 3.2 `kind`: Matching by Node Type |
| 126 | + |
| 127 | +The `kind` rule matches an AST node by its `tree_sitter_node_kind` name, derived from the language's Tree-sitter grammar. Useful for targeting constructs like `call_expression` or `function_declaration`. |
| 128 | +* Example: `kind: call_expression` |
| 129 | + |
| 130 | +### 3.3 `regex`: Text-Based Node Matching |
| 131 | + |
| 132 | +The `regex` rule matches the entire text content of an AST node using a Rust regular expression. It's not a "positive" rule, meaning it matches any node whose text satisfies the regex, regardless of its structural kind. |
| 133 | + |
| 134 | +### 3.4 `nthChild`: Positional Node Matching |
| 135 | + |
| 136 | +The `nthChild` rule finds nodes by their 1-based index within their parent's children list, counting only named nodes by default. |
| 137 | +* `number`: Matches the exact nth child. Example: `nthChild: 1` |
| 138 | +* `string`: Matches positions using An+B formula. Example: `2n+1` |
| 139 | +* `Object`: Provides granular control: |
| 140 | + * `position`: `number` or An+B string. |
| 141 | + * `reverse`: `true` to count from the end. |
| 142 | + * `ofRule`: An ast-grep rule to filter the sibling list before counting. |
| 143 | + |
| 144 | +### 3.5 `range`: Position-Based Node Matching |
| 145 | + |
| 146 | +The `range` rule matches an AST node based on its character-based start and end positions. A `RangeObject` defines `start` and `end` fields, each with 0-based `line` and `column`. `start` is inclusive, `end` is exclusive. |
| 147 | + |
| 148 | +## 4. Relational Rules: Contextual and Hierarchical Matching |
| 149 | + |
| 150 | +Relational rules filter targets based on their position relative to other AST nodes. They can include `stopBy` and `field` options. |
| 151 | + |
| 152 | +**** |
| 153 | + |
| 154 | +### 4.1 `inside`: Matching Within a Parent Node |
| 155 | + |
| 156 | +Requires the target node to be inside another node matching the `inside` sub-rule. |
| 157 | +* Example: |
| 158 | + |
| 159 | + ```yaml |
| 160 | + inside: |
| 161 | + pattern: class $C { $$$ } |
| 162 | + stopBy: end |
| 163 | + ``` |
| 164 | + |
| 165 | +### 4.2 `has`: Matching with a Descendant Node |
| 166 | + |
| 167 | +Requires the target node to have a descendant node matching the `has` sub-rule. |
| 168 | +* Example: |
| 169 | + ```yaml |
| 170 | + has: |
| 171 | + pattern: await $EXPR |
| 172 | + stopBy: end |
| 173 | + ``` |
| 174 | + |
| 175 | +### 4.3 `precedes` and `follows`: Sequential Node Matching |
| 176 | + |
| 177 | +* `precedes`: Target node must appear before a node matching the `precedes` sub-rule. |
| 178 | +* `follows`: Target node must appear after a node matching the `follows` sub-rule. |
| 179 | + |
| 180 | +Both include `stopBy` but not `field`. |
| 181 | + |
| 182 | +### 4.4 `stopBy` and `field`: Refining Relational Searches |
| 183 | + |
| 184 | +* `stopBy`: Controls search termination for relational rules. |
| 185 | + * `"neighbor"` (default): Stops when immediate surrounding node doesn't match. |
| 186 | + * `"end"`: Searches to the end of the direction (root for `inside`, leaf for `has`). |
| 187 | + * `Rule object`: Stops when a surrounding node matches the provided rule (inclusive). |
| 188 | +* `field`: Specifies a sub-node within the target node that should match the relational rule. Only for `inside` and `has`. |
| 189 | + |
| 190 | +When you are not sure, always use `stopBy: end` to ensure the search goes to the end of the direction. |
| 191 | + |
| 192 | +## 5. Composite Rules: Logical Combination of Conditions |
| 193 | + |
| 194 | +Composite rules combine atomic and relational rules using logical operations. |
| 195 | + |
| 196 | +### 5.1 `all`: Conjunction (AND) of Rules |
| 197 | + |
| 198 | +Matches a node only if all sub-rules in the list match. Guarantees order of rule matching, important for metavariables. |
| 199 | +* Example: |
| 200 | + ```yaml |
| 201 | + all: |
| 202 | + - kind: call_expression |
| 203 | + - pattern: console.log($ARG) |
| 204 | + ``` |
| 205 | + |
| 206 | + |
| 207 | +### 5.2 `any`: Disjunction (OR) of Rules |
| 208 | + |
| 209 | +Matches a node if any sub-rules in the list match. |
| 210 | +* Example: |
| 211 | + ```yaml |
| 212 | + any: |
| 213 | + - pattern: console.log($ARG) |
| 214 | + - pattern: console.warn($ARG) |
| 215 | + - pattern: console.error($ARG) |
| 216 | + ``` |
| 217 | + |
| 218 | + |
| 219 | +### 5.3 `not`: Negation (NOT) of a Rule |
| 220 | + |
| 221 | +Matches a node if the single sub-rule does not match. |
| 222 | +* Example: |
| 223 | + ```yaml |
| 224 | + not: |
| 225 | + pattern: console.log($ARG) |
| 226 | + ``` |
| 227 | + |
| 228 | + |
| 229 | +### 5.4 `matches`: Rule Reuse and Utility Rules |
| 230 | + |
| 231 | +Takes a rule-id string, matching if the referenced utility rule matches. Enables rule reuse and recursive rules. |
| 232 | + |
| 233 | +## 6. Metavariables: Dynamic Content Matching |
| 234 | + |
| 235 | +Metavariables are placeholders in patterns to match dynamic content in the AST. |
| 236 | + |
| 237 | +### 6.1 `$VAR`: Single Named Node Capture |
| 238 | + |
| 239 | +Captures a single named node in the AST. |
| 240 | +* **Valid**: `$META`, `$META_VAR`, `$_` |
| 241 | +* **Invalid**: `$invalid`, `$123`, `$KEBAB-CASE` |
| 242 | +* **Example**: `console.log($GREETING)` matches `console.log('Hello World')`. |
| 243 | +* **Reuse**: `$A == $A` matches `a == a` but not `a == b`. |
| 244 | + |
| 245 | +### 6.2 `$$VAR`: Single Unnamed Node Capture |
| 246 | + |
| 247 | +Captures a single unnamed node (e.g., operators, punctuation). |
| 248 | +* **Example**: To match the operator in `a + b`, use `$$OP`. |
| 249 | + ```yaml |
| 250 | + rule: |
| 251 | + kind: binary_expression |
| 252 | + has: |
| 253 | + field: operator |
| 254 | + pattern: $$OP |
| 255 | + ``` |
| 256 | + |
| 257 | + |
| 258 | +### 6.3 `$$$MULTI_META_VARIABLE`: Multi-Node Capture |
| 259 | + |
| 260 | +Matches zero or more AST nodes (non-greedy). Useful for variable numbers of arguments or statements. |
| 261 | +* **Example**: `console.log($$$)` matches `console.log()`, `console.log('hello')`, and `console.log('debug:', key, value)`. |
| 262 | +* **Example**: `function $FUNC($$$ARGS) { $$$ }` matches functions with varying parameters/statements. |
| 263 | + |
| 264 | +### 6.4 Non-Capturing Metavariables (`_VAR`) |
| 265 | + |
| 266 | +Metavariables starting with an underscore (`_`) are not captured. They can match different content even if named identically, optimizing performance. |
| 267 | +* **Example**: `$_FUNC($_FUNC)` matches `test(a)` and `testFunc(1 + 1)`. |
| 268 | + |
| 269 | +### 6.5 Important Considerations for Metavariable Detection |
| 270 | + |
| 271 | +* **Syntax Matching**: Only exact metavariable syntax (e.g., `$A`, `$$B`, `$$$C`) is recognized. |
| 272 | +* **Exclusive Content**: Metavariable text must be the only text within an AST node. |
| 273 | +* **Non-working**: `obj.on$EVENT`, `"Hello $WORLD"`, `a $OP b`, `$jq`. |
| 274 | + |
| 275 | +The ast-grep playground is useful for debugging patterns and visualizing metavariables. |
| 276 | + |
0 commit comments