Skip to main content
ast-grep is an open-source static analysis tool that uses abstract syntax trees (AST) to find patterns in your codebase, helping identify security vulnerabilities and enforce code quality standards. CodeRabbit runs ast-grep on supported changed files using the essentials package and any custom rules you configure. CodeRabbit runs ast-grep v0.51.3.

Configuration

boolean
default:true
Enable ast-grep
array of string
default:[]
List of rules directories.
array of string
default:[]
List of utils directories.
boolean
default:true
Use ast-grep essentials package.
array of string
default:[]
Predefined packages to be used.
.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 under packages:
.coderabbit.yaml
A package repository must meet these requirements:
  • 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 rules directory at the package root. Utility rules go in an optional utils directory 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
For each match, CodeRabbit passes the review the rule’s message, the matched code, the replacement from fix when the rule has one, and the rule’s note.

The rule object

The rule 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, kind and regex.
  • Relational rules check the nodes around it: inside, has, follows and precedes. Each takes a sub-rule that the surrounding node must match.
  • Composite rules combine sub-rules: all, any, not, and matches for a utility rule.
The rule object reference lists every field.

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 with matches. A local utility rule lives in the utils field of a rule file and works only in that file:
A global utility rule lives in its own file, so every rule can use it. Put utility files in a separate directory and list it under util_dirs:
.coderabbit.yaml
A global utility file has an id, a language and a rule:
utils/is-literal.yml
See utility rules in the ast-grep documentation.

Examples

JavaScript: imports without a file extension

rules/find-import-file.yml

TypeScript: console calls outside catch blocks

The rule flags console.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 uses fix 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
More rules are in the ast-grep catalog.

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.

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.