Rule Cheat Sheet โ
This cheat sheet provides a concise overview of ast-grep's rule object configuration, covering Atomic, Relational, and Composite rules, along with notes on Utility rules. It's designed as a handy reference for common usage.
Atomic Rules Cheat Sheet โ
These are your precision tools, matching individual AST nodes based on their inherent properties.
| โ๏ธ Atomic Rules | ||
yaml | ๐งฉ Match a node by code structure. e.g. | |
yaml | ๐งฉ To parse ambiguous patterns, use | |
yaml | ๐ท๏ธ Match an AST node by its | |
yaml | ๐ Matches node text content against a Rust regular expression | |
yaml | ๐ข Find a node by its 1-based index among its named siblings | |
yaml | ๐ข Advanced positional control: | |
yaml | ๐ฏ Matches a node based on its character span: 0-based, inclusive start, exclusive end | |
Relational Rules Cheat Sheet โ
These powerful rules define how nodes relate to each other structurally. Think of them as your AST GPS!
| ๐ Relational Rules | ||
yaml | ๐ Target node must appear inside its parent/ancestor node matching the sub-rule | |
yaml | ๐ณ Target node must have a child/descendant node matching the sub-rule | |
yaml | ๐ณ | |
yaml | โ๏ธ Target node must appear before another node matching the sub-rule | |
yaml | โถ๏ธ Target node must appear after another node matching the sub-rule. | |
yaml | ๐ | |
Composite Rules Cheat Sheet โ
Combine multiple rules using Boolean logic. Crucially, these operations apply to a single target node!
| ๐ง Composite Rules | ||
yaml | โ Node must satisfy ALL the rules in the list. | |
yaml | ๐งก Node must satisfy AT LEAST ONE of the rules in the list. | |
yaml | ๐ซ Node must NOT satisfy the specified sub-rule. | |
yaml | ๐ Matches the node if that utility rule matches it. Your gateway to modularity! | |
Utility Rules Cheat Sheet โ
Define reusable rule definitions to cut down on duplication and build complex, maintainable rule sets.
| ๐ฆ Utility Rules | ||
yaml | ๐ก Defined within the | |
yaml | ๐ Defined in separate YAML files in global | |