Skip to content

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
pattern: console.log($ARG)

๐Ÿงฉ Match a node by code structure. e.g. console.log call with a single $ARG

yaml
pattern:
  context: '{ key: value }'
  selector: pair

๐Ÿงฉ To parse ambiguous patterns, use context and specify selector AST to search.

yaml
kind: if_statement

๐Ÿท๏ธ Match an AST node by its kind name

yaml
regex: ^regex.+$

๐Ÿ” Matches node text content against a Rust regular expression

yaml
nthChild: 1

๐Ÿ”ข Find a node by its 1-based index among its named siblings

yaml
nthChild:
  position: 2
  reverse: true
  ofRule: { kind: argument_list }

๐Ÿ”ข Advanced positional control: position, reverse (count from end), or filter siblings using ofRule

yaml
range:
  start: { line: 0, column: 0 }
  end: { line: 0, column: 13 }

๐ŸŽฏ 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
inside:
  kind: function_declaration

๐Ÿ  Target node must appear inside its parent/ancestor node matching the sub-rule

yaml
has:
  kind: method_definition

๐ŸŒณ Target node must have a child/descendant node matching the sub-rule

yaml
has:
  kind: statement_block
  field: body

๐ŸŒณ field makes has/inside match nodes by their semantic role

yaml
precedes:
  pattern: function $FUNC() { $$ }

โ—€๏ธ Target node must appear before another node matching the sub-rule

yaml
follows:
  pattern: let x = 10;

โ–ถ๏ธ Target node must appear after another node matching the sub-rule.

yaml
inside:
  kind: function_declaration
  stopBy: end

๐Ÿ  stopBy makes relational rules search all the way to the end, not just immediate neighbors.

Composite Rules Cheat Sheet โ€‹

Combine multiple rules using Boolean logic. Crucially, these operations apply to a single target node!

๐Ÿง  Composite Rules
yaml
all:
  - pattern: const $VAR = $VALUE
  - has: { kind: string_literal }

โœ… Node must satisfy ALL the rules in the list.

yaml
any:
  - pattern: let $X = $Y
  - pattern: const $X = $Y

๐Ÿงก Node must satisfy AT LEAST ONE of the rules in the list.

yaml
not:
  pattern: console.log($$)

๐Ÿšซ Node must NOT satisfy the specified sub-rule.

yaml
matches: is-function-call

๐Ÿ”„ 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
rules:
  - id: find-my-pattern
    rule:
      matches: my-local-check
utils:
  my-local-check:
    kind: identifier
    regex: '^my'

๐Ÿก Defined within the utils field of your current config file. Only accessible within that file.

yaml
# In utils/my-global-check.yml
id: my-global-check
language: javascript
rule:
  kind: variable_declarator
  has:
    kind: number_literal

๐ŸŒ Defined in separate YAML files in global utilsDirs folders, accessible across your entire project.

Made with โค๏ธ with Rust