GritQL Reference

Grit is a query language for searching and modifying codebases.

The root of a Grit query is a pattern.
A pattern can be a code snippet surrounded in backticks.
Code snippets can contain metavariables prefixed with $.
The anonymous metavariable can be bound to without a name.
$... is a spread metavariable that matches 0 or more nodes.
AST nodes can also be used as patterns.
AST nodes can specify fields, where each field value is bound to a pattern or metavariable.
`console.log($message)` =>
A rewrite is a pattern followed by a => followed by a pattern. Rewrites are usable as patterns.
$x => .

The . is an empty/null pattern, it replaces the matched node with nothing.

Regular expressions can be used as patterns, prefixed with r" and followed by capture variables.
$list = [1, 2, 3]
$one = $list[0]
$three = $list[-1]
Lists can be accessed via index, where negative indices count from the end of the list.
$map = { a: 1, b: 2, c: 3 }
$c = $map.c
Maps can be accessed via dot notation. Map keys must be strings.
or {`console.log`, `console.warn`}
Compound patterns can be combined with and, or, and any clauses.
not `console.log`
not clauses can be used to negate a pattern.
maybe `console.log`
maybe clauses can be used to optionally match against a pattern, without failing the query.
contains `console`
contains clauses can be used to match any node that contains a specific pattern by traversing downwards through the syntax tree.
within `function() { $_ }`
within clauses search up the syntax tree for a matching node.
after `console.warn($_)`

after clauses search for a pattern that occurs after another pattern.

`console.log($message)` as $log
as clauses can assign a pattern to a metavariable.
$statements <: some `console.log($_)`
some matches lists where at least one element matches a pattern.
`console.log($message)` where { ... }
A where clause can be added after a pattern to introduce conditions that restrict when the pattern matches.
`console.log($message)` where {
  $message <: "Hello world"
Inside the where clause, the <: operator is used to match metavariables against a pattern.
`console.log($_)` where {
  $new = "Hello world"
} => `console.log($new)`
The where block can also assign values to metavariables using =. Variables can be lists, code snippets, or strings.
`console.log($message)` where {
  $new = "Hello",
  $new += "world"
} => $new
Strings and lists can be appended to using +=. (ex. )
`function() { $body }` where {
  $body <: contains bubble {
    `console.log($body)` =>
The default scope of a GritQL pattern is global. The bubble clause can be used to create a new scope within which all metavariables are isolated from surrounding code.
`function $name() { $body }` where {
  $body <: contains bubble($name) {
    `console.log($message)` =>
    `console.warn($message, $name)`
The bubble clause optionally accepts arguments, which can be used to "pierce" the bubble by allowing metavariables to preserve the values they had outside the bubble scope.
`console.log` limit 10
The limit clause can be used to limit the number of files returned by a query.
// this is a comment
Comments in GritQL start with // and are ignored by the parser.
sequential {
  `console.log` => `console.error`,
  `console.warn` => ``
sequential clauses can be used to contain multiple patterns that are applied to the same file in order.
function custom_example($name) {
  return `name: $name`
Custom functions can be used to define reusable right-side values.
function mod($value) js {
  return $value % 2
Custom functions be written in JavaScript by adding the js keyword parameters.