v0.51.3.
Configuration
- .coderabbit.yaml
- Web UI
.coderabbit.yaml
rule_dirs lists directories with your custom ast-grep rules, and util_dirs lists directories with utility rules that other rules reuse. Both take paths relative to the repository root. packages takes repositories in owner/repository format.
Paths in rule_dirs and util_dirs must point to directories available in the reviewed commit, with matching case. CodeRabbit rejects entries that use absolute paths or parent-directory traversal, or contain control or shell-sensitive characters.
If configured directories remain unavailable after recovery attempts, CodeRabbit reports incomplete custom-rule coverage and continues with the available rule and package directories. Unavailable or unauthorized rule packages also cause an incomplete-coverage report. CodeRabbit skips scanning when no usable rule directories remain.
CodeRabbit will only run ast-grep if you have configured at least one of the following:
- Custom rule directories (
rule_dirs) - Essential rules enabled (
essential_rules: true) - Custom packages (
packages)
util_dirs can be used to support shared utility configs, but by itself it does not cause ast-grep to run.CodeRabbit also skips ast-grep when the pull request contains no changed files with a supported extension. Individual files that are 8 MiB or larger are skipped.Essentials package
essential_rules: true adds the ast-grep essentials package, a set of security rules that CodeRabbit maintains. It has its own setting, so you can turn it on or off without editing packages.
Rule packages
A package is a repository of ast-grep rules that several repositories can share. List it underpackages:
.coderabbit.yaml
- Name. Write it as
owner/repository. Each part uses letters, digits, and single hyphens or underscores between them. Names with dots arenât accepted. - Platform. CodeRabbit clones the package from the same Git platform as the reviewed repository.
- Visibility. The repository must be public. On GitHub, a private package also works when the reviewed repository is private and both the pull request author and whoever triggered the review can read the package repository.
- Layout. Rules go in a
rulesdirectory at the package root. Utility rules go in an optionalutilsdirectory next to it. These directory names are reserved and must be spelled exactly like this. Inside each directory, you can organize files any way you like.
Write custom rules
Each rule is a YAML file in the ast-grep rule format. Design and test rules on sample code in the ast-grep Playground before you add them to your repository.1
Create a rules directory
Add a directory for your rules to the repository, for example
rules.2
Add one file per rule
Each file needs an
id, a language and a rule. Add a message too: it tells the review why the rule matched.3
Point CodeRabbit at the directory
Add the directory to
reviews.tools.ast-grep.rule_dirs. If your rules use global utility rules, add their directory to util_dirs.4
Test on a pull request
Open a pull request that changes a file the rule should match, and read the review.
rules/no-console-log.yml
.coderabbit.yaml
message, the matched code, the replacement from fix when the rule has one, and the ruleâs note.
The rule object
Therule field holds a rule object. A node matches when it satisfies every field in the object. Every field is optional, but the object needs at least one positive field, such as pattern or kind, that matches only nodes of specific kinds. A regex isnât positive, because it can match a node of any kind, so pair it with pattern or kind. The fields fall into three groups:
- Atomic rules check a single node:
pattern,kindandregex. - Relational rules check the nodes around it:
inside,has,followsandprecedes. Each takes a sub-rule that the surrounding node must match. - Composite rules combine sub-rules:
all,any,not, andmatchesfor a utility rule.
Relational rules and stopBy
A relational rule checks only the nearest surrounding node by default. Set stopBy: end to search all the way up, down or along the syntax tree. This rule matches an await anywhere inside a for...in loop, not only directly under it:
Composite rules
all matches when every sub-rule matches:
any matches when at least one sub-rule matches:
not matches when its sub-rule doesnât match:
Reuse rules as utilities
A utility rule is a rule object with a name. Other rules reference it withmatches.
A local utility rule lives in the utils field of a rule file and works only in that file:
util_dirs:
.coderabbit.yaml
id, a language and a rule:
utils/is-literal.yml
Examples
JavaScript: imports without a file extension
rules/find-import-file.yml
TypeScript: console calls outside catch blocks
The rule flagsconsole.log, console.debug and console.warn everywhere, and console.error outside a catch block.
rules/no-console-except-error.yml
C: method-style calls on structs
Simulating methods with function pointers in a struct adds indirection. This rule flags the pattern and usesfix to suggest a plain function call that takes the struct pointer as its first argument. The fix assumes a function with the same name as the field exists and that the call doesnât already pass the struct, so review each suggestion.
rules/method-receiver.yml
Limits
CodeRabbit runs your rules during automated reviews. Answers in chat donât run them. For the other cases where CodeRabbit skips ast-grep, see Configuration.Links
- ast-grep Documentation
- ast-grep Playground
- Writing ast-grep Rules
- ast-grep Rule Object Properties
- ast-grep Essentials Package
Files
ast-grep runs on changed files with the following extensions:.bash.c.cpp.cs.css.erb(parsed as HTML).ex.exs.go.h.hpp.hs.html.java.js.json.jsx.kt.lua.php.py.rb.rs.scala.sh.sql.swift.ts.tsx.yaml.yml
Whatâs next
Review instructions
Compare ast-grep rules with path instructions, instruction documents and custom checks.
All supported tools
Browse the linters and security analyzers CodeRabbit runs.