This feature is available exclusively as part of the Pro plan. Please refer to our pricing page for more information about our plans and features.
ast-grep β a Rust-based tool that uses the tree-sitter parser to generate AST rules for popular languages, written by Herrington Darkholme. This feature has a learning curve and is recommended for users already comfortable with YAML configuration.
AST-based context is only available during the automated code review process, it is not available in the chat.
- Abstract Syntax Tree β Wikipedia
ast-greprule configuration guide
Setup
Use the ast-grep Playground to design and test rules on source code snippets before adding them to your project.Add message property
Ensure that all rules contain a
message property that will be used during
the review processUpdate configuration
Add the rulesβ directory to the
.coderabbit.yaml file under
tools.ast-grep configurationYAML
The rule object
The rule object is the core concept of theast-grep rule system β every other feature is built on top of it.
Below is the full list of fields in a rule object. Every field is optional and can be omitted, but at least one field must be present. A node matches a rule if and only if it satisfies all fields in the rule object.
YAML
Rule categories
Atomic Rule
The most basic rule that checks if AST nodes match
Relational Rule
Rules that check if a node is surrounded by another node
Composite Rule
Rules that combine sub-rules together using logical operators
Read the ast-grep documentation for detailed guides.
Atomic rule
An atomic rule defines the most basic matching rule: whether a syntax node matches or not. There are three kinds:pattern, kind, and regex.
Official documentation: Atomic Rule
Relational rule
A relational rule defines the relationship between two syntax nodes. There are four kinds:inside, has, follows, and precedes.
All four relational rules accept a sub-rule object as their value. The sub-rule matches the surrounding node; the relational rule itself matches the target node.
Official documentation: Relational Rule
YAML
Composite rule
A composite rule defines the logical relationship between multiple sub-rules. There are three kinds:all, any, and not.
all β matches if all sub-rules match:
YAML
any β matches if any sub-rule matches:
YAML
not β applies negation; matches if the sub-rule does not match:
YAML
Official documentation: Composite Rule
Reusing rules as utilities
ast-grep uses YAML for rule representation, which means rule objects cannot be directly reused across rule files. Utility rules solve this.
Local utility rule
Local utility rules are defined in theutils field of the config file. utils is a string-keyed dictionary.
YAML
Global utility rule
Global utility rules are defined in a separate file and are available across all rule configurations in the project. To create global utility rules, create arules directory and a utils directory at the root of your project:
YAML
.coderabbit.yaml under tools.ast-grep:
YAML
YAML
Official documentation: Utility Rule
Packages
A package is a collection ofast-grep rules that can be shared across multiple projects.
Built-in packages
CodeRabbit provides packages you can use out of the box
Custom packages
Create your own packages and share them with your organization or community
CodeRabbit packages
ast-grep-essentials
Essential security rules packageBecause we value security, this package gets its own property in
.coderabbit.yaml for easier installation without overwriting existing configurations.YAML
Custom packages
To use a public repository containingast-grep rules as a package, add it to the packages field in .coderabbit.yaml.
Repository requirements
Repository requirements
- Must be a public repository
- Name must follow the format
organization/repository
Content requirements
Content requirements
- Contains rules that follow the
ast-greprule format - Follows the required folder structure shown below
Folder structure
Folder structure
rules and utils are reserved directory names and must be named exactly as shown. Inside each directory, the structure is flexible.YAML
Supported languages
Web Technologies
- JavaScript
- TypeScript
Systems Languages
- C
- Rust
- Golang
Enterprise Languages
- Java
- C#
- Kotlin
- Python
Examples
JavaScript β disallow imports without file extension
YAML
TypeScript β no console.log except console.error in catch blocks
YAML
C β prefer plain function calls over struct method-style calls
In C, simulating OOP via struct function pointers introduces memory and indirection overhead. This rule flags the pattern and suggests a plain function call with the struct pointer as the first argument.YAML